Author Topic: Description on a distance  (Read 1486 times)

0 Members and 1 Guest are viewing this topic.

Coder

  • Swamp Rat
  • Posts: 827
Description on a distance
« on: March 06, 2013, 12:45:01 AM »
Hello everybody .

I have seen the below codes posted in this forum and I can't understand the logic thing of it .

Can anyone of you masters describe it for me please ?

Code - Auto/Visual Lisp: [Select]
  1. (distance '(0. 0. 0.) (mapcar '- pt1 pt2))
  2.  

Thanks in advance .  :-)

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: Description on a distance
« Reply #1 on: March 06, 2013, 05:22:08 AM »
Assuming the variables pt1 & pt2 represent 3D points, the expression (mapcar '- pt1 pt2) is returning the vector from pt2 to pt1. Since a vector is simply a displacement, the vector can also be viewed as a 3D point relative to the origin, as shown by the following diagram:



Now (distance '(0.0 0.0 0.0) (mapcar '- pt1 pt2)) will return the distance from the origin to this point, i.e. the length of the vector from pt2 to pt1 (or indeed pt1 to pt2).

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Description on a distance
« Reply #2 on: March 06, 2013, 06:30:07 AM »
The question does beg why such is used though. Would the exact same result not be obtainable from:
Code - Auto/Visual Lisp: [Select]
  1. (distance pt1 pt2)
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: Description on a distance
« Reply #3 on: March 06, 2013, 07:25:37 AM »
The question does beg why such is used though. Would the exact same result not be obtainable from:
Code - Auto/Visual Lisp: [Select]
  1. (distance pt1 pt2)

Exactly.  :-)

Coder

  • Swamp Rat
  • Posts: 827
Re: Description on a distance
« Reply #4 on: March 06, 2013, 08:09:02 AM »
Thank you so much Lee for that clear explanations  :-)

Regards .

Lee Mac

  • Seagull
  • Posts: 12922
  • London, England
Re: Description on a distance
« Reply #5 on: March 06, 2013, 08:19:31 AM »
Thank you so much Lee for that clear explanations  :-)

You're welcome  :-)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Description on a distance
« Reply #6 on: March 08, 2013, 11:24:10 AM »
It also returns the x & y delta values (Vector Difference).
(mapcar '- ' p2 p1)
(mapcar '- '(30 20) '(10 10))
(20 10)

p2 is 30 units greator on the x axis
p2 is 20 units greator on the y axis


and this can be used to get the distance

_$ (mapcar '- '(30 20) '(10 10))
(20 10)
_$ (defun sqr (S) (* S S)) ; needed subroutine
SQR
_$ (mapcar 'sqr (mapcar '- '(30 20) '(10 10)))
(400 100)
_$ (apply '+ (mapcar 'sqr (mapcar '- '(30 20) '(10 10))))
500
_$ (sqrt (apply '+ (mapcar 'sqr (mapcar '- '(30 20) '(10 10)))))
22.3607
_$ (distance '(30 20) '(10 10))
22.3607
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.