TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Shay Gaghe on July 08, 2014, 11:48:39 AM

Title: passing vectors to a function
Post by: Shay Gaghe on July 08, 2014, 11:48:39 AM
Hi

i want to pass 2 vectors to test if they are perpendicular, i wanna use  Lee Mac function for this, cant figure out how.

Code - Auto/Visual Lisp: [Select]
  1. ;; Perpendicular-p  -  Lee Mac
  2. ;; Returns T if vectors v1,v2 are perpendicular
  3.  
  4. (defun LM:Perpendicular-p ( v1 v2 )
  5.     (equal 0.0 (vxv v1 v2) 1e-8)
  6. )
  7.  
  8. ;; Vector Dot Product  -  Lee Mac
  9. ;; Args: u,v - vectors in R^n
  10.  
  11. (defun vxv ( u v )
  12.     (apply '+ (mapcar '* u v))
  13. )
  14.  

Thanks
Shay
Title: Re: passing vectors to a function
Post by: JohnK on July 08, 2014, 12:00:23 PM
The syntax of Lee Mac's code is (call or use his function like this):
Code - Auto/Visual Lisp: [Select]
  1. (LM:Perpendicular-p vector1 vector2)
Title: Re: passing vectors to a function
Post by: Shay Gaghe on July 08, 2014, 12:31:34 PM
The syntax of Lee Mac's code is (call or use his function like this):
Code - Auto/Visual Lisp: [Select]
  1. (LM:Perpendicular-p vector1 vector2)

but what are those vectors? we always passing points...i never thought that vectors are something you can refer to in Autolisp.
can anyone post a simple example on how to use that particular function?
Title: Re: passing vectors to a function
Post by: Kerry on July 08, 2014, 12:50:31 PM
Yes, a vector can be expressed as a point.
A vector which originates at 0,0,0 has its direction and magnitude expressed by the point it 'points' to.

This has a section on vectors
https://www.khanacademy.org/math/precalculus
Title: Re: passing vectors to a function
Post by: Lee Mac on July 08, 2014, 01:15:51 PM
The syntax of Lee Mac's code is (call or use his function like this):
Code - Auto/Visual Lisp: [Select]
  1. (LM:Perpendicular-p vector1 vector2)

but what are those vectors? we always passing points...i never thought that vectors are something you can refer to in Autolisp.
can anyone post a simple example on how to use that particular function?

As others have explained, a vector is simply a displacement by a given amount in a given direction, usually expressed relative to the origin; this is equivalent to the coordinates of a point given by that same displacement from the origin - e.g. the vector (5,3) defines a displacement of 5 units in the x-direction and 3 units in the y-direction, however, this same definition applies to the point (5,3) which is also 5 units in the x-direction & 3 units in the y-direction from the origin.

Here is an example, calling my function with two 2D vectors:
Code - Auto/Visual Lisp: [Select]
  1. (LM:perpendicular-p '(1 2) '(-2 1))
Title: Re: passing vectors to a function
Post by: Shay Gaghe on July 08, 2014, 02:59:02 PM
The syntax of Lee Mac's code is (call or use his function like this):
Code - Auto/Visual Lisp: [Select]
  1. (LM:Perpendicular-p vector1 vector2)

but what are those vectors? we always passing points...i never thought that vectors are something you can refer to in Autolisp.
can anyone post a simple example on how to use that particular function?

As others have explained, a vector is simply a displacement by a given amount in a given direction, usually expressed relative to the origin; this is equivalent to the coordinates of a point given by that same displacement from the origin - e.g. the vector (5,3) defines a displacement of 5 units in the x-direction and 3 units in the y-direction, however, this same definition applies to the point (5,3) which is also 5 units in the x-direction & 3 units in the y-direction from the origin.

Here is an example, calling my function with two 2D vectors:
Code - Auto/Visual Lisp: [Select]
  1. (LM:perpendicular-p '(1 2) '(-2 1))

ok, if i understand correctly (see attachment) , you gave the function only 2 points, (1,2) and (1,-2) ,
how come the function knows the origin?
Title: Re: passing vectors to a function
Post by: WILL HATCH on July 08, 2014, 03:56:15 PM
Somebody needs a good lesson on vectors.  A point and a vector from the origin are the same thing for most intents and purposes (avoiding units etc.) as a vector is the difference between 2 points p1 and p2

Vector V1-2 from p1 to p2
i.e. V1-2 = p2 - p1
i.e vector = head point - tail point      where the arrow is at the head

In this case p1 = {0,0}  and V1-2 = p2
Title: Re: passing vectors to a function
Post by: Kerry on July 08, 2014, 09:43:27 PM
< .. >

ok, if i understand correctly (see attachment) , you gave the function only 2 points, (1,2) and (1,-2) ,
how come the function knows the origin?

I take it that you haven't looked at the link I posted.

The parameters are NOT necessarily actual points. They are a list ( each with 2 or 3 elements)  representing the magnitude of the vector.
Title: Re: passing vectors to a function
Post by: hanhphuc on July 08, 2014, 11:59:45 PM
ok, if i understand correctly (see attachment) , you gave the function only 2 points, (1,2) and (1,-2) ,
how come the function knows the origin?
The idea of vector can be delta of 2 points,
Example: we should know 45⁰ degrees is 1:1,

Code: [Select]

;if 1st vector with 45⁰ :
;delta X= (cos (* pi 0.25)) , delta Y= (sin (* pi 0.25))
;ie: delta X= +0.707107 ; delta Y=+0.707107  <--- 1:1

;if 2nd vector with 135⁰ :
;delta X=(cos (* pi 0.75)) , delta Y= (sin (* pi 0.75))
;ie: delta X= -0.707107 ; delta Y=+0.707107  <---- -1:1

(VxV '(+0.707107 +0.707107 ) ; 45⁰
'( -0.707107 +0.707107 )); 135⁰

;matrix
(mapcar '*
'(+0.707107 +0.707107 ) ; 45⁰
'(-0.707107 +0.707107 )); 135⁰
= '(-0.5 0.5) ;

-0.5 + 0.5 = 0.0
; returns zerop, ie: per-p T
similar thread in cadtutor recently?
Title: Re: passing vectors to a function
Post by: irneb on July 09, 2014, 01:56:15 AM
To make it clearer, a point is the XYZ in a Cartesian coordinate system. A vector is the difference between 2 points, either expressed as the XYZ differences (coordinate vector) or the angle and distance (polar vector).

As an example, see attached. A line (vector) from point A to point B.

The Vx is the calculation of the coordinate vector (referred to as the Delta of a line segment), while the Vp is the calculation of the polar vector (the length and angle of a line segment).
Title: Re: passing vectors to a function
Post by: irneb on July 09, 2014, 02:07:08 AM
how come the function knows the origin?
You're kidding right? The "origin" is always 0,0,0 ... that's what "origin" in Cartesian coordinates mean, even when referring to a relative origin (which is what's going on here).

Think of it as a coordinate vector being made by moving both points at the same time such that the 1st point is at 0,0,0 then reading what the 2nd point's coordinates are. That IS a coordinate vector - i.e. how much difference between the XYZ values of the 2 points.
Title: Re: passing vectors to a function
Post by: Shay Gaghe on July 09, 2014, 11:56:24 AM
< .. >

ok, if i understand correctly (see attachment) , you gave the function only 2 points, (1,2) and (1,-2) ,
how come the function knows the origin?

I take it that you haven't looked at the link I posted.

The parameters are NOT necessarily actual points. They are a list ( each with 2 or 3 elements)  representing the magnitude of the vector.

of course i did watch the link you post,
i just missed the most important part which is that vectors are all about their origin,
it took me time to understand how 2 vectors can express 3 points,
now i got it
Thanks you
Shay
Title: Re: passing vectors to a function
Post by: Kerry on July 09, 2014, 07:21:42 PM
Quote
now i got it

Great to hear. It's an important concept in graphics and math.
Title: Re: passing vectors to a function
Post by: Shay Gaghe on July 10, 2014, 11:59:54 AM
(LM:perpendicular-p '(1 2) '(-2 1))

(-4x-4) + (4x4) = 0

but when i tried this on some other points the same math wont work, why?
Title: Re: passing vectors to a function
Post by: ronjonp on July 10, 2014, 12:14:26 PM
It's not perpendicular
Title: Re: passing vectors to a function
Post by: Shay Gaghe on July 10, 2014, 12:46:02 PM
It's not perpendicular

thanks

i update the shape , what math is to be applied here in order to get answer 0?
Title: Re: passing vectors to a function
Post by: Lee Mac on July 10, 2014, 01:49:16 PM
Firstly, note that you would need to supply the function with the vectors (-1.1 5.6) & (5.6 1.2), since you are looking for the vectors emanating from the point (8.4 -2.3):
Code - Auto/Visual Lisp: [Select]
  1. _$ (mapcar '- '(7.3 3.3) '(8.4 -2.3))
  2. (-1.1 5.6)
  3. _$ (mapcar '- '(14.0 -1.1) '(8.4 -2.3))
  4. (5.6 1.2)

However, these vectors are still not perpendicular (89.0183 degrees apart):
Code - Auto/Visual Lisp: [Select]
  1. _$ (rtd (acos (/ (vxv '(-1.1 5.6) '(5.6 1.2)) (|v| '(-1.1 5.6)) (|v| '(5.6 1.2)))))
  2. 89.0183
Title: Re: passing vectors to a function
Post by: Shay Gaghe on July 10, 2014, 02:19:35 PM
Firstly, note that you would need to supply the function with the vectors (-1.1 5.6) & (5.6 1.2), since you are looking for the vectors emanating from the point (8.4 -2.3):
Code - Auto/Visual Lisp: [Select]
  1. _$ (mapcar '- '(7.3 3.3) '(8.4 -2.3))
  2. (-1.1 5.6)
  3. _$ (mapcar '- '(14.0 -1.1) '(8.4 -2.3))
  4. (5.6 1.2)

However, these vectors are still not perpendicular (89.0183 degrees apart):
Code - Auto/Visual Lisp: [Select]
  1. _$ (rtd (acos (/ (vxv '(-1.1 5.6) '(5.6 1.2)) (|v| '(-1.1 5.6)) (|v| '(5.6 1.2)))))
  2. 89.0183
Where I can find all those math functions like vxv , v etc?
Title: Re: passing vectors to a function
Post by: LE3 on July 10, 2014, 02:26:55 PM
You need to take a trip/visit to Lee's site:
http://www.lee-mac.com/index.html

And spend some time reading all the GREAT STUFF, he have in there for many to simple use OOB or to learn (<- I recommend)... AFAIK some of those function names are Lee's... HTH
Title: Re: passing vectors to a function
Post by: Lee Mac on July 10, 2014, 02:28:01 PM
Where I can find all those math functions like vxv , v etc?

I think I've already given you the link in your thread over at CADTutor, but if not: Mathematical Functions (http://bit.ly/1eCIRD6)
Title: Re: passing vectors to a function
Post by: Lee Mac on July 10, 2014, 02:28:28 PM
You need to take a trip/visit to Lee's site:
http://www.lee-mac.com/index.html

And spend some time reading all the GREAT STUFF, he have in there for many to simple use OOB or to learn (<- I recommend)... AFAIK some of those function names are Lee's... HTH

Many thanks Luis - that's very kind of you  :-)
Title: Re: passing vectors to a function
Post by: Shay Gaghe on July 10, 2014, 02:38:44 PM
Where I can find all those math functions like vxv , v etc?

I think I've already given you the link in your thread over at CADTutor, but if not: Mathematical Functions (http://bit.ly/1eCIRD6)

Yes you did, I find it now under the scary  word "metrix" so no wonder how I've missed it.
Is there any mathematical explination about those functions?
Title: Re: passing vectors to a function
Post by: Lee Mac on July 10, 2014, 02:43:44 PM
Is there any mathematical explination about those functions?

Certainly, simply type the function names (e.g. 'Vector Dot Product (http://en.wikipedia.org/wiki/Vector_dot_product)' or 'Matrix Determinant (http://en.wikipedia.org/wiki/Matrix_determinant)') into Wikipedia (http://en.wikipedia.org/wiki/Main_Page).
Title: Re: passing vectors to a function
Post by: Tharwat on July 10, 2014, 05:07:39 PM
Lee . Won't the following would be equivalent at anyway ?

Code - Auto/Visual Lisp: [Select]
  1. (defun Perpendicular-p (p1 org p2 / v)
  2.   ;;    Tharwat 11.July.2014    ;;
  3.   (or (equal (setq v (abs (- (angle p1 org) (angle p2 org))))
  4.              4.71239
  5.              1e-4
  6.       )
  7.       (equal v 1.5708 1e-4)
  8.   )
  9. )
  10.  
Title: Re: passing vectors to a function
Post by: Lee Mac on July 10, 2014, 05:17:40 PM
Lee . Won't the following would be equivalent at anyway ?

Yes, however the use of the angle function will mean that 3D vectors will be dependent on the UCS.
Title: Re: passing vectors to a function
Post by: Tharwat on July 10, 2014, 06:01:26 PM
Lee . Won't the following would be equivalent at anyway ?

Yes, however the use of the angle function will mean that 3D vectors will be dependent on the UCS.

Excellent - very precise :wink:
Title: Re: passing vectors to a function
Post by: Shay Gaghe on July 11, 2014, 01:06:51 AM
hi

this code apply Lee's code on a selected entity ,

Code: [Select]
(defun entsel-pre-p (/ ent vertex vertexList u v vos)
  (if (setq ent (car (entsel "\nSelect an object")))
    (if (setq ent (entget ent))
      (progn

(foreach vertex ent
  (if (= (car vertex) 10)
    (setq vertexList (cons (cdr vertex) vertexList))
  )
)


(setq
  v (car vertexList)
  u (car (reverse vertexList))
)

(setq vos (apply '+ (mapcar '* u v)))

(equal 0.0 vos 1e-8)
      )
    )
  )
)


if the selected poly origin is on 0,0,0 the function returns T , but if i move the poly and execute the code again it returns nil.
what i missing here?

Shay
Title: Re: passing vectors to a function
Post by: Kerry on July 11, 2014, 01:10:18 AM
Shay Gaghe,

If you're travelling in space and you are located at 10, 25,13 and you want to travel to Uranus which is at 12,21,15 ; what vector will Sulu need to
use to navigate to your destination?

(distances in parsecs).
Title: Re: passing vectors to a function
Post by: Kerry on July 11, 2014, 01:17:42 AM
Regarding the piccy you posted,
What is the vector for each of the arms in the left grid .. and in the right grid.?


ie:
What is it's Length, Delta X and Delta Y dimensions ?
Title: Re: passing vectors to a function
Post by: Shay Gaghe on July 11, 2014, 01:27:36 AM
Regarding the piccy you posted,
What is the vector for each of the arms in the left grid .. and in the right grid.?


ie:
What is it's Length, Delta X and Delta Y dimensions ?

i must know the location of Sulu
Regarding the piccy you posted,
What is the vector for each of the arms in the left grid .. and in the right grid.?


ie:
What is it's Length, Delta X and Delta Y dimensions ?

Title: Re: passing vectors to a function
Post by: Shay Gaghe on July 11, 2014, 01:29:58 AM
with dims
Title: Re: passing vectors to a function
Post by: Kerry on July 11, 2014, 01:41:21 AM
If you're travelling in space and you are located at 10, 25,13 and you want to travel to Uranus which is at 12,21,15 ; what vector will Sulu need to
use to navigate to your destination?

(distances in parsecs).

i must know the location of Sulu
Sulu is on the bridge .. he is the navigator.
http://en.wikipedia.org/wiki/Hikaru_Sulu


Regarding the piccy you posted,
What is the vector for each of the arms in the left grid .. and in the right grid.?


ie:
What is it's Length, Delta X and Delta Y dimensions ?
< ... piccys >

You still don't understand what constitutes a vector.
What is the Delta X and Delta Y of the arms ??
Title: Re: passing vectors to a function
Post by: Kerry on July 11, 2014, 01:54:32 AM
(http://www.sternenwind.ch/mathproj/scans/stoffplaene/3g_lin_fkt_steigungsdreieck.jpg)

piccy Source: http://math.stackexchange.com/questions/25286/2d-coordinates-of-a-point-along-a-line-based-on-d-and-m-where-am-i-messing
Title: Re: passing vectors to a function
Post by: Kerry on July 11, 2014, 04:56:09 AM
Shay Gaghe,
I hate it when I'm forced to have discussions with myself.

Try to answer the questions I asked because the answer you want is in my questions.

Title: Re: passing vectors to a function
Post by: Shay Gaghe on July 11, 2014, 05:15:19 AM
sorry i didnt see star trek

but let me express my gesture for you
Title: Re: passing vectors to a function
Post by: Kerry on July 11, 2014, 06:15:02 AM
Pretty close :-D:

Try this
(http://i62.tinypic.com/2a97dac.jpg)


Now that we can get to our destination, try to change your code to use the Vector definition (DeltaX, DeltaY, DeltaZ) of each line rather than the endpoints in space.


added: Excellent piccy you made !!
Title: Re: passing vectors to a function
Post by: Shay Gaghe on July 11, 2014, 07:36:20 AM
Pretty close :-D:

Try this
(http://i62.tinypic.com/2a97dac.jpg)


Now that we can get to our destination, try to change your code to use the Vector definition (DeltaX, DeltaY, DeltaZ) of each line rather than the endpoints in space.


added: Excellent piccy you made !!



in order to get back to where istarted i would inverse the operator to + rather than -

except that i cant go anywhere without knowing the xyz of home

or i didnt understand the task well
Title: Re: passing vectors to a function
Post by: Kerry on July 11, 2014, 07:39:34 AM
Lets say Home is at (-18, 18, 18)

Title: Re: passing vectors to a function
Post by: Shay Gaghe on July 11, 2014, 07:56:38 AM
i wanna go home
Title: Re: passing vectors to a function
Post by: Kerry on July 11, 2014, 08:05:14 AM
i wanna go home


Most excellent Navigator Shay.
Title: Re: passing vectors to a function
Post by: Kerry on July 11, 2014, 08:06:11 AM
Here's something to read on the trip.

Code - Auto/Visual Lisp: [Select]
  1. ;;
  2. ;;;-------------------------------------------------------------
  3. ;;;-------------------------------------------------------------
  4. ;;; Returns the single unit vector from p1 to p2
  5. (defun kdub:vec2pts (p1 p2)
  6.   (if (not (equal p1 p2 1e-009))
  7.     (mapcar '(lambda (x) (/ x (distance p1 p2))) (mapcar '- p2 p1))
  8.   )
  9. )
  10. ;;
  11. ;;;-------------------------------------------------------------
  12. ;;;-------------------------------------------------------------
  13. ;;
  14. (defun kdub:normal_from_3pts (ip p1 p2 / norm)
  15.   (cond ((inters ip p1 ip p2)
  16.          (foreach p '(p1 p2) (set p (mapcar '- (eval p) ip)))
  17.          (setq norm (list (- (* (cadr p1) (caddr p2)) (* (caddr p1) (cadr p2)))
  18.                           (- (* (caddr p1) (car p2)) (* (car p1) (caddr p2)))
  19.                           (- (* (car p1) (cadr p2)) (* (cadr p1) (car p2)))
  20.                     )
  21.                norm (mapcar '(lambda (x) (* x (/ 1 (distance '(0 0 0) norm)))) norm)
  22.          )
  23.         )
  24.   )
  25. )
  26. ;;
  27. ;;;-------------------------------------------------------------
  28. ;;;-------------------------------------------------------------
  29. ;;
  30. ;;;  Returns the angle (in radians) defined by its summit and two points
  31. ;;; Returned angle is always positive and less than or equal to pi radians.
  32.  
  33. ;; Using vector calculus
  34. (defun kdub:enclosedangle_3pts (ip p1 p2 / v1 v2)
  35.   (if (and (setq v1 (kdub:vec2pts ip p1)) (setq v2 (kdub:vec2pts ip p2)))
  36.     (cond ((equal v1 v2 1e-009) 0.0)
  37.           ((equal v1 (mapcar '- v2) 1e-009) pi)
  38.           (t (* 2 (kdub:asin (/ (distance v1 v2) 2))))
  39.     )
  40.   )
  41. )
  42.  
  43. ;;
  44. ;;;-------------------------------------------------------------
  45. ;;;-------------------------------------------------------------
  46. (defun KDUB:GetEnclosedApexAngle ( apex p1 p2 )
  47.   (- (+ pi pi) (rem (+ pi pi (- (angle apex p1) (angle apex p2))) (+ pi pi)))
  48. )
  49. ;;
  50. ;;;-------------------------------------------------------------
  51. ;;;-------------------------------------------------------------
  52. ;;
  53. (defun KDUB:GetApexAngle<180 ( apex p1 p2 )
  54.   (min (setq a (rem (+ pi pi (- (angle apex p1) (angle apex p2))) (+ pi pi))) (- (+ pi pi) a))
  55. )
  56.  
  57. ;;
  58. ;;;-------------------------------------------------------------
  59. ;;;-------------------------------------------------------------
  60. ;;
  61. ;;; Returns the elevation of point pt from the plane defined by p1 p2 p3
  62.  
  63. (defun kdub:elevabovePlane (pt p1 p2 p3)
  64.   (* (cos
  65.        (kdub:enclosedangle_3pts p1 (mapcar '+ p1 (kdub:normal_from_3pts p1 p2 p3)) pt)
  66.      )
  67.      (distance p1 pt)
  68.   )
  69. )
  70. ;;
  71. ;;;-------------------------------------------------------------
  72. ;;;-------------------------------------------------------------
  73. ;;; Returns the coordinates of the projection of point pt
  74. ;;; on the plane defined by the points p1 p2 p3.
  75.  
  76. (defun kdub:project_pt (pt p1 p2 p3)
  77.   (mapcar '-
  78.           pt
  79.           (mapcar '(lambda (x) (* x (kdub:elevabovePlane pt p1 p2 p3)))
  80.                   (kdub:normal_from_3pts p1 p2 p3)
  81.           )
  82.   )
  83. )
  84. ;;;-------------------------------------------------------------
  85. ;;;-------------------------------------------------------------
  86. ;;
  87. (defun kdub:vec:points->vector (3dPt1 3dPt2) (mapcar '- 3dPt2 3dPt1))
  88. ;;
  89. ;;;-------------------------------------------------------------
  90. ;;;-------------------------------------------------------------
  91. ;;
  92. ;; VXS Multiply a vector by a scalar
  93. ;;
  94. ;; Arguments : a vector and a real
  95. ;;
  96. (defun vxs (v s) (mapcar (function (lambda (x) (* x s))) v))
  97. ;;
  98. ;;;-------------------------------------------------------------
  99. ;;;-------------------------------------------------------------
  100. ;;; -- Function GetPathVector
  101. ;;; Returns the vector at start point from a given object.
  102. ;;; Arguments [Type]:
  103. ;;;   Obj = Object [VLA-OBJECT]
  104. ;;; Return [Type]:
  105. ;;;   > Vector [LIST]
  106. ;;; Notes:
  107. ;;;   None
  108. ;;
  109. (defun kdub:vec:getpathvector (vlaObj)
  110.   (kdub:vec:normalize
  111.   )
  112. )
  113. ;;
  114. ;;;-------------------------------------------------------------
  115. ;;;-------------------------------------------------------------
  116. ;;; -- Function KDUB:VecNormalize
  117. ;;; Normalizes Vec to unit length (1.0).
  118. ;;; Arguments [Type]:
  119. ;;;   Vec = Vector [LIST]
  120. ;;; Return [Type]:
  121. ;;;   > Normalized vector [LIST]
  122. ;;; Notes:
  123. ;;;   None
  124. ;;
  125. (defun kdub:vec:normalize (Vector / Coefficient)
  126.   (setq Coefficient (/ 1.0 (kdub:vec:calculatelength Vector)))
  127.   (kdub:vec:zeroaxisfuzz (mapcar '* Vector (list Coefficient Coefficient Coefficient))
  128.   )
  129. )
  130. ;;
  131. ;;;-------------------------------------------------------------
  132. ;;;-------------------------------------------------------------
  133. ;;
  134. ;;
  135. ;;; -- Function KDUB:eVecLen
  136. ;;; Calculates length of a vector.
  137. ;;; Arguments [Type]:
  138. ;;;   Vec = Vector [LIST]
  139. ;;; Return [Type]:
  140. ;;;   > Length of the vector [REAL]
  141. ;;; Notes:
  142. ;;;   None
  143. ;;
  144. (defun kdub:vec:calculatelength (Vector)
  145.   (car (kdub:vec:zeroaxisfuzz
  146.          (list (sqrt (apply '+ (mapcar '(lambda (a) (* a a)) Vector))))
  147.        )
  148.   )
  149. )
  150. ;;
  151. ;;;-------------------------------------------------------------
  152. ;;;-------------------------------------------------------------
  153. ;;
  154. ;;; Return the cross product, vec1 cross vec2
  155. (defun kdub:vec:crossproduct (vec1 vec2)
  156.   (list (- (* (cadr vec1) (caddr vec2)) (* (caddr vec1) (cadr vec2)))
  157.         (- (* (caddr vec1) (car vec2)) (* (car vec1) (caddr vec2)))
  158.         (- (* (car vec1) (cadr vec2)) (* (cadr vec1) (car vec2)))
  159.   )
  160. )
  161.  
  162.  
  163. ;;
  164. ;;;-------------------------------------------------------------
  165. ;;;-------------------------------------------------------------
  166. ;;
  167. (defun kdub:vec:vla3dpoint->list (vla3dPoint)
  168.   (kdub:vec:zeroaxisfuzz (vlax-safearray->list (vlax-variant-value vla3dPoint)))
  169. )
  170. ;;;------------------------------------------------------------------
  171. ;;;------------------------------------------------------------------
  172. ;;; arcsine (inverse sine) accepts an argument in the range
  173. ;;; -1.0 to 1.0 inclusive, and returns an angle in radians in
  174. ;;; the range -pi/2 to pi/2 inclusive.
  175. (defun kdub:asin (num)
  176.   (cond
  177.     ((> (abs num) 1.0)
  178.      (alert (strcat " Arc-sine error in (KDUB:asin ." "\n Spitting the dummy"))
  179.      (exit)
  180.     )
  181.     ((zerop num) 0.0)
  182.     ((= num 1.0) kglobal:rad90)
  183.     ((= num -1.0) (- kglobal:rad90))
  184.     (t (atan num (sqrt (- 1.0 (* num num)))))
  185.   )
  186. )
  187. ;;;------------------------------------------------------------------
  188. ;;;------------------------------------------------------------------
  189. ;;; arccosine (inverse cosine)
  190. ;;; argument in the range -1.0 to 1.0 inclusive
  191. ;;; returns an angle in radians in the range pi to 0 inclusive
  192. ;;;
  193. (defun kdub:acos (num)
  194.   (if (zerop num)
  195.     (* pi 0.5)
  196.     (atan (sqrt (- 1.0 (* num num))) num)
  197.   )
  198. )
  199. ;;;------------------------------------------------------------------
  200. ;;;------------------------------------------------------------------
  201. ;;;
  202. ;;; (KDUB:defineconstant '*max_Real* 1.797693134862315e+308)
  203. ;; pass num as radian.
  204. (defun kdub:tan (num / cosnum)
  205.   (cond ((zerop (rem num pi)) 0.0)
  206.         ((zerop (rem num (* pi 0.5))) *max_real*)
  207.         ((zerop (setq cosnum (cos num))) *max_real*)
  208.         (t (/ (sin num) cosnum))
  209.   )
  210. )
  211. ;;;------------------------------------------------------------------
  212. ;;;------------------------------------------------------------------
  213. ;;;
  214.  
  215.  
  216.  
  217.  
Title: Re: passing vectors to a function
Post by: Kerry on July 11, 2014, 08:14:42 AM
And these helpers

Code - Auto/Visual Lisp: [Select]
  1. ;;;------------------------------------------------------------------
  2. ;;;------------------------------------------------------------------
  3. ;;;
  4. (defun kdub:protect-assign (symbollist)
  5.   (eval (list 'pragma (list 'quote (list (cons 'protect-assign symbollist)))))
  6. )
  7. ;;;------------------------------------------------------------------
  8. ;;;------------------------------------------------------------------
  9. ;;;
  10. (defun kdub:unprotect-assign (symbollist)
  11.   (eval (list 'pragma (list 'quote (list (cons 'unprotect-assign symbollist)))))
  12. )
  13. ;;;------------------------------------------------------------------
  14. ;;;------------------------------------------------------------------
  15. ;;;
  16. (defun kdub:defineconstant (symbolname symbolvalue)
  17.   (kdub:unprotect-assign (list symbolname))
  18.   (set symbolname symbolvalue)
  19.   (kdub:protect-assign (list symbolname))
  20.   (princ)
  21. )
  22.  
  23. (kdub:defineconstant 'kglobal:pix2 (* pi 2))
  24. (kdub:defineconstant 'kglobal:pi/2 (* pi 0.5))
  25. (kdub:defineconstant 'kglobal:pi/4 (* pi 0.25))
  26. (kdub:defineconstant 'kglobal:pi/8 (* kglobal:pi/4 0.5))
  27.  
  28. (kdub:defineconstant 'kglobal:epsilon 1e-14)
  29. (kdub:defineconstant 'kglobal:max-real 1.797693134862315e+308)
  30.                                              ;double, 64-bit
  31. (kdub:defineconstant 'kglobal:min-real 2.2250738585072014e-308)
  32. (kdub:defineconstant 'kglobal:max-integer 2147483647)
  33.                                              ; signed longint, 32-bit
  34.  
  35. (kdub:defineconstant 'kglobal:rad00 0.00)
  36. (kdub:defineconstant 'kglobal:rad15 (* pi (/ 15 180.0)))
  37. (kdub:defineconstant 'kglobal:rad30 (* pi (/ 30 180.0)))
  38. (kdub:defineconstant 'kglobal:rad45 (* pi 0.25))
  39. (kdub:defineconstant 'kglobal:rad60 (* pi (/ 60 180.0)))
  40. (kdub:defineconstant 'kglobal:rad90 (* pi 0.50))
  41. (kdub:defineconstant 'kglobal:rad135 (* pi 0.75))
  42. (kdub:defineconstant 'kglobal:rad180 pi)
  43. (kdub:defineconstant 'kglobal:rad225 (* pi 1.25))
  44. (kdub:defineconstant 'kglobal:rad270 (* pi 1.50))
  45. (kdub:defineconstant 'kglobal:rad315 (* pi 1.75))
  46. (kdub:defineconstant 'kglobal:rad360 (* pi 2.00))
  47.  
Title: Re: passing vectors to a function
Post by: JohnK on July 11, 2014, 08:21:42 AM
Excellent thread Kerry!
Title: Re: passing vectors to a function
Post by: Shay Gaghe on July 11, 2014, 08:25:49 AM
Thanks
it will be enjoyable trip!    :mrgreen:
Title: Re: passing vectors to a function
Post by: Kerry on July 11, 2014, 08:27:35 AM
Thanks
it will be enjoyable trip!    :mrgreen:

You're welcome. Thanks for playing along.

//------------
Excellent thread Kerry!

Thanks John ... it ended up well, we got home, and no animals were injured.
Title: Re: passing vectors to a function
Post by: CAB on July 11, 2014, 10:07:12 AM
I would like to book passage on the next trip please. 8)

Great job Kerry.
 
Title: Re: passing vectors to a function
Post by: Shay Gaghe on July 12, 2014, 05:52:20 AM
hi

is there any reason why i would express location and angles with vectors rather than coordinates?

and i notice the Line command is represented only by vectors , why?
Title: Re: passing vectors to a function
Post by: Kerry on July 12, 2014, 07:45:17 AM
yes.
UCS.
Adding vectors is quicker than the polar function.
Title: Re: passing vectors to a function
Post by: irneb on July 14, 2014, 07:19:33 AM
hi

is there any reason why i would express location and angles with vectors rather than coordinates?

and i notice the Line command is represented only by vectors , why?
90% (or even more) you'd draw by using vectors. It's extremely seldom that you'd calculate the exact coordinate on all the points of linework. At worst you'd think of the start position of a line (ITO the full coordinate), then the vector for its 2nd point (i.e. how long and in what direction for polar vectors, and how much X and Y and Z to "move" for coordinate vector). Only for things where the coordinates are already calculated for you would you prefer to fill in the XYZ as a full coordinate (e.g. the X/Y's of property lines).

Think of this as using the @ prefix to the line command as opposed to using the # prefix when typing in the X,Y,Z values. I.e. how far from the previous point instead of at which global point irrespective of what's gone before it.
Title: Re: passing vectors to a function
Post by: Kerry on July 28, 2014, 10:23:40 PM
I would like to book passage on the next trip please. 8)

Great job Kerry.

Speaking of Vector Trips, today I was reminded of this:

Vectors in Linear Algebra
1.1 Opening Remarks
1.1.1 Take Off
Co-Pilot Roger Murdock (to Captain Clarence Oveur):  We have clearance, Clarence.
Captain Oveur:                                                             Roger, Roger. What’s our vector, Victor?”

https://www.youtube.com/watch?v=fVq4_HhBK8Y

From Airplane. Dir. David Zucker, Jim Abrahams, and Jerry Zucker. Perf. Robert
Hays, Julie Hagerty, Leslie Nielsen, Robert Stack, Lloyd Bridges, Peter Graves,
Kareem Abdul-Jabbar, and Lorna Patterson. Paramount Pictures, 1980. Film.

From the same movie source, for perspective:
Rumack:      I won't deceive you, Mr. Striker. We're running out of time.
Ted Striker: Surely there must be something you can do.
Rumack:      I'm doing everything I can... and stop calling me Shirley!


Operator: [Captain Oveur is on the phone with the Mayo Clinic] Excuse me, Captain Oveur, but I have an emergency call on line five from a Mr. Hamm.
Captain Oveur: Alright, give me a Hamm on five, hold the Mayo.