Point Lists

AutoCAD AutoLISP & Visual LISP

 
Point Lists
 
 
 

AutoLISP observes the following conventions for handling graphics coordinates. Points are expressed as lists of two or three numbers surrounded by parentheses.

2D points

Expressed as lists of two real numbers (X and Y, respectively), as in

(3.4 7.52)

3D points

Expressed as lists of three real numbers (X, Y, and Z, respectively), as in

(3.4 7.52 1.0)

You can use the list function to form point lists, as shown in the following examples:

_$  (list 3.875
1.23)
(3.875 1.23) 
_$  (list 88.0 14.77 3.14)
(88.0 14.77 3.14) 

To assign particular coordinates to a point variable, you can use one of the following expressions:

_$  (setq pt1 (list 3.875
1.23))
(3.875 1.23) 
_$  (setq pt2 (list 88.0
14.77 3.14))
(88.0 14.77 3.14) 
_$  (setq abc 3.45)
3.45
_$  (setq pt3 (list abc
1.23))
(3.45 1.23) 

The latter uses the value of variable abc as the X component of the point.

If all members of a list are constant values, you can use the quote function to explicitly define the list, rather than the list function. The quote function returns an expression without evaluation, as follows:

_$ (setq pt1 (quote
(4.5 7.5)))
(4.5 7.5)

The single quotation mark (') can be used as shorthand for the quote function. The following code produces the same result as the preceding code.

_$ (setq pt1 '(4.5 7.5))
(4.5 7.5)

You can refer to X, Y, and Z components of a point individually, using three additional built-in functions called car, cadr, and caddr. The following examples show how to extract the X, Y, and Z coordinates from a 3D point list. The pt variable is set to the point (1.5 3.2 2.0):

_$ (setq pt '(1.5 3.2 2.0))
(1.5 3.2 2.0)

The car function returns the first member of a list. In this example it returns the X value of point pt to the x_val variable.

_$ (setq x_val (car
pt))
1.5 

The cadr function returns the second member of a list. In this example it returns the Y value of the pt point to the y_val variable.

_$ (setq y_val (cadr
pt))
3.2 

The caddr function returns the third member of a list. In this example it returns the Z value of point pt to the variable z_val.

_$ (setq z_val (caddr
pt))
2.0 

You can use the following code to define the lower-left and upper-right (pt1 and pt2) corners of a rectangle, as follows:

_$ (setq pt1 '(1.0 2.0)
pt2 ' (3.0 4.0))
(3.0 4.0) 

You can use the car and cadr functions to set the pt3 variable to the upper-left corner of the rectangle, by extracting the X component of pt1 and the Y component of pt2, as follows:

_$ (setq pt3 (list (car
pt1) (cadr pt2)))
(1.0 4.0) 

The preceding expression sets pt3 equal to point (1.0,4.0).

AutoLISP supports concatenations of car and cdr up to four levels deep. The following are valid functions:

caaaar

cadaar

cdaaar

cddaar

caaadr

cadadr

cdaadr

cddadr

caaar

cadar

cdaar

cddar

caadar

caddar

cdadar

cdddar

caaddr

cadddr

cdaddr

cddddr

caadr

caddr

cdadr

cdddr

caar

cadr

cdar

cddr

These concatenations are the equivalent of nested calls to car and cdr. Each a represents a call to car, and each d represents a call to cdr. For example:

(caar x)    is equivalent
to  (car (car x)) 
(cdar x)    is equivalent
to  (cdr (car x)) 
(cadar x)   is equivalent
to  (car (cdr (car x)))
(cadr x)    is equivalent
to  (car (cdr x)) 
(cddr x)    is equivalent
to  (cdr (cdr x)) 
(caddr x)   is equivalent to  (car (cdr (cdr x)))