Author Topic: passing vectors to a function  (Read 10441 times)

0 Members and 1 Guest are viewing this topic.

Shay Gaghe

  • Newt
  • Posts: 89
passing vectors to a function
« 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

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
Re: passing vectors to a function
« Reply #1 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)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Shay Gaghe

  • Newt
  • Posts: 89
Re: passing vectors to a function
« Reply #2 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?

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: passing vectors to a function
« Reply #3 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
« Last Edit: July 08, 2014, 01:04:36 PM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: passing vectors to a function
« Reply #4 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))

Shay Gaghe

  • Newt
  • Posts: 89
Re: passing vectors to a function
« Reply #5 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?

WILL HATCH

  • Bull Frog
  • Posts: 450
Re: passing vectors to a function
« Reply #6 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

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: passing vectors to a function
« Reply #7 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.
« Last Edit: July 08, 2014, 09:47:28 PM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

hanhphuc

  • Newt
  • Posts: 64
Re: passing vectors to a function
« Reply #8 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?
( apply 'equal "hp" "happy" "hạnh phúc" "ハッピー" "幸福" "행복" ) ; error: too many arguments

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: passing vectors to a function
« Reply #9 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).
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: passing vectors to a function
« Reply #10 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.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Shay Gaghe

  • Newt
  • Posts: 89
Re: passing vectors to a function
« Reply #11 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

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: passing vectors to a function
« Reply #12 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.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Shay Gaghe

  • Newt
  • Posts: 89
Re: passing vectors to a function
« Reply #13 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?

ronjonp

  • Needs a day job
  • Posts: 7527
Re: passing vectors to a function
« Reply #14 on: July 10, 2014, 12:14:26 PM »
It's not perpendicular

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC