Hi everyone,
Could someone help me how to get the nearest distance from a point to a point?
I could use
(- (car pt1) (car pt2))
to get the distance. Is there another way to do it?
My code
(defun c:test() (setq pt1 (getpoint "\nPoint 1")) (setq pt2 (getpoint "\nPoint 2")) (setq dist (- (car pt2) (car pt1))) ); defun
Thanks.
There must be one point to get the other one which is the nearest , explain in more details with an example would help a lot .
Does (setq dist (distance pt1 pt2)) work?
@maikhanhmst wrote:
Hi everyone,
Could someone help me how to get the nearest distance from a point to a point?
I could use
(- (car pt1) (car pt2))to get the distance. Is there another way to do it?
....
That will get the difference between the X coordinates of the points, which will be the distance between them only when they share the same YZ coordinates. The (distance) function suggested by Shneuph is what you want. Unlike the above, which will sometimes return a negative value, (distance) always returns a positive [absolute] value. And it accounts for the 3rd dimension if both are 3D points with different Z coordinates.
Something like
(mapcar '- P1 P2 )
OR
(mapcar '(lambda ( a b) (- a b)) P1 P2)
Your original code gets the difference between the x coordinates of the 2 points. I don't know of an easier way.
You can check for negative values (as Kent said you will possibly get) and make it positive with an if statement. I don't think LISP has an absolute value function.
(if (< (- (car pt1) (car pt2)) 0)
(* -1 (- (car pt1) (car pt2)))
(- (car pt1) (car pt2))
);if
@Shneuph wrote:
.... I don't think LISP has an absolute value function. ...
Yes, it does, called, logically enough, (abs). If you want the difference in location between two points as a list of absolute-value differences in X and Y [and Z, for that matter], as a pure XYZ distance without direction:
(mapcar 'abs (mapcar '- p1 p2))