Coding the Autolisp Function

Line 1 sets up the function "div_line" with three input parameters. The first parameter passed to the
function is the starting point of the line in list format ( x y z). The second parameter is the end point in the
same form. The last parameter is the number of segments to divide the line into (an integer value). We'd
call the function from Autolisp as follows:

(div_line (list 0 0 0) (list 1 1 0) 4)

Which would divide the line from point (0, 0, 0) to the point (1, 1, 0) into four segments and return the
interior division points in a list:

( (0.25 0.25 0.00)  (0.50 0.50 0.00) (0.75 0.75 0.00) )

Lines 2 through 4 calculate the vector coefficients using the input points.

Line 5 calculates the segment length saved in variable
delta.

Line 7 starts the loop for the point calculations, which will be one less than the number of segments

Lines 8 through 10 calculate the division point at percentage "p" along the vector.

Line 11 assembles the list of points using the cons list constructor in Autolisp.

Line 12 increments the percentage "p" by the segment length "delta" getting ready for the next point
calculation.

After the loop has iterated through all the required segment points, line 13 reverses the point list and since
it is the last line of code in the Autolisp function, it returns the list to the calling function or variable.

If you have any comments or questions regarding this technique or code, feel free to e-mail me at:
support@pixelgraphicsinc.com  AutoLisp Function Tutorial: Dividing a 3D Line into Segments
Overview of the Function

You're probably wondering why I'd ever want to create an Autolisp function to divide a 3D line into equal
segments instead of using AutoCAD's "Divide" command. The reason is pretty simple. I wanted to create
an
AutoCAD command that subdivides a 3d face (like is done with subdivision polymodeling) and needed
to write this function to give me the division points along the opposing edges of the face.

You may have seen a my previous
tutorial calculating the 3d midpoint of a line, but in this case I needed a
more general function that would allow me to divide a line into any number of equally spaced segments.

To easily calculate a point along a line, I had to dust off my calculus book. Don't freak out, it's actually
pretty simple. If you don't want or care to know how it's done, here's the code for the completed function. If
you copy and use the function, please respect my work and the time I've put into it by keeping my copyright
info with the function, and giving me credit where it's appropriate. A link from your site would be
appreciated as well.
Jeff Winship - Computer Graphics Consultant                                                                                                                                            April 27, 2008
The Equation of a Line in 3d Space

The equation for a line in 3D spaced expressed in vector notation is:

v = Ai + Bj + Ck

where A, B and C are the vector coefficients X, Y and Z components of the 3D line respectively. The vector
coefficients are calculated using the following equations:

tA = ( x - x0)     where x is the end point x value and x0 is the starting point x value
tB = ( y - y0)     where y is the end point y value and y0 is the starting point y value
tC = ( z - z0)     where x is the end point z value and z0 is the starting point z value

Since a vector extends infinitely in both directions, and since a vector is all the points lying on that line, the
variable
t is a scalar value representing the position along the vector. At t=0, the vector function will yield
the starting point, and at t=1 will yield the ending point that were used to initially calculate the vector
coefficients. So, we calculate the vector coefficients using:

A = ( x - x0)
B = ( y - y0)
C = ( z - z0)

Then rearranging the formulas to solve x, y and z for any position t we get:

x = x0 + tA
y = y0 + tB
z = z0 + tC
 12345678910111213
References: Calculus and Analytic Geometry - Thomas/Finney - Six edition
ISBN 0-201-16290-3