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
Pixel Graphics, Inc. - Litigation & Court Graphics
LITIGATION GRAPHICS - COMPUTER ANIMATION - TECHNICAL ILLUSTRATION - CAD DRAFTING - AUTOLISP PROGRAMMING
3423 Rivers Edge Trail
Houston, Texas 77339
281.359.1187
Copyright 2008 - Pixel Graphics, Inc. All rights reserved.
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.
;========================================================================
;=  Function:  div_line ( pt1 pt2 segs)
;=    Arguments:
;=        "pt1" is the starting point of the line ( x y z)
;=        "pt2" is the end point of the line ( x y z)
;=        "segs" is the number of segments to divide the line into
;=
;=    Divides a 3D line into equal length segments and returns the
;=    list of interior division points not including the end points.
;=
;=         Copyright 2008 Jeff Winship. All rights reserved.
;===============================================================4/27/2008

(defun div_line (pt1 pt2 segs / A B C delta p x y z pt_list)
    ;--Calculate the vector coefficients
    (setq A (- (car pt2) (car pt1)))
    (setq B (- (cadr pt2) (cadr pt1)))
    (setq C (- (caddr pt2) (caddr pt1)))

    ;-- "delta" is the segment length expressed as a percentage from 0 to 1
    ;-- for use with the vector function
    (setq delta (/ 1.0 segs))

    ;--Set the initial percentage along the line
    (setq p delta)

    ;--Loop through the segments
    (repeat (- segs 1)

    ;-- Calculate the division point at percentage "p"
    (setq x (+ (car pt1) (* p A) ))
    (setq y (+ (cadr pt1) (* p B) ))
    (setq z (+ (caddr pt1) (* p C) ))

    ;-- Add the point to the output point list
    (setq pt_list (cons (list x y z) pt_list))

    ;-- Increment p to the next division point
    (setq p (+ p delta))
    )

    ;-- Reverse and return the point list
    (setq pt_list(reverse pt_list))
)
Jeff Winship - Computer Graphics Consultant                                                                                                                                                    April 27, 2008
HOME  |  3D ANIMATION  |  DIGITAL IMAGES  |  AUTOCAD DRAFTING  |  AUTOLISP PROGRAMMING  |  CONTACT & OTHER INFO  |  F.A.Q.
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
1

2
3
4



5


6


7


8
9
10


11


12



13
References: Calculus and Analytic Geometry - Thomas/Finney - Six edition
ISBN 0-201-16290-3