Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to get the nearest distance from a point to another?

7 REPLIES 7
Reply
Message 1 of 8
maikhanhmst
524 Views, 7 Replies

How to get the nearest distance from a point to another?

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.

7 REPLIES 7
Message 2 of 8
_Tharwat
in reply to: maikhanhmst

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 .

Message 3 of 8
Shneuph
in reply to: maikhanhmst

Does  (setq dist (distance pt1 pt2)) work?

---sig---------------------------------------
'(83 104 110 101 117 112 104 64 71 109 97 105 108 46 99 111 109)
Message 4 of 8
Kent1Cooper
in reply to: maikhanhmst


@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.

Kent Cooper, AIA
Message 5 of 8
maikhanhmst
in reply to: Kent1Cooper

Sorry, for late respond and not clear from the question.
You get what I mean. The distance I want to get is different between the X coordiantes (car) or Y coordinates (cadr).
Thank you.
Message 6 of 8
stevor
in reply to: maikhanhmst

Something like

 (mapcar '- P1 P2 )

OR

 (mapcar '(lambda ( a b) (- a b)) P1 P2)

 

S
Message 7 of 8
Shneuph
in reply to: maikhanhmst

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
---sig---------------------------------------
'(83 104 110 101 117 112 104 64 71 109 97 105 108 46 99 111 109)
Message 8 of 8
Kent1Cooper
in reply to: Shneuph


@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))

Kent Cooper, AIA

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost