Wednesday, April 24, 2024 | Toby Opferman
 

Vectors

Toby Opferman
http://www.opferman.net
toby@opferman.net

				Vectors

This is not a complete guide, this is an introduction if at all maybe a helpful
guide to people wanting to learn vectors.  Maybe it will help you maybe it will not.
There are no guarentees.  There is some Triginometry involved.


	To best describe what a vector is, let us take a look at what it
is not.


(X, Y) is a point.
(X1, Y1) - (X2, Y2) is a line.
m = (Y2 - Y1)/(X2 - X1) is the slope of a line.
  

  (4, 3) This is a point.


      ^
    4 | 
    3 |      *      
    2 |
    1 |
      +-----------> 
       1 2 3 4






  (4, 3) - (2, 2) This is a line.


      ^
    4 | 
    3 |    **      
    2 |  **
    1 |
      +-----------> 
       1 2 3 4

(Ya, hard to draw with text)



A vector is a direction. And vectors are assoicated with <X, Y, Z>
angle brackets.

<X, Y>


A lot of people get confused and think that a vector is a line or it is a point.
It is neither.  It is an arbitrary direction and length.  We say arbitary
since it doesn't matter where the starting point is when you have the vector.

The standard equation for a vector is

r <cos(theta), sin(theta)>


If you think back to Triginometry, this is the circle.  Theta is the angle you are
going from some central point (Which you can say is the origin, but it doesn't matter).
r is how long you go. If r = 1, then <cos(theta), sin(theta)> is somewhere
on the unit circle.

Theta has to be defined tho.

A vector is a direction and length from any point.


An Example:

  (4, 3) This is a point.

  <3, 3> this is a vector.

If you start at point (4, 3) and you travel the vector <3, 3> you end
up at point (4+3, 3+3) = (7, 6)

      ^
    7 |
    6 |           *
    5 |         .
    4 |       .
    3 |      *      
    2 |
    1 |
      +--------------> 
       1 2 3 4 5 6 7



Vectors are drawn like this:  ------>  a line with an arrow head on the top.

As the above example will show, a vector is NOT a line NOR a point.
It is a direction and a length.


Take this example:

SAME vector, different starting position.  The Origin.
<3,3> from the origin ends up at (3,3)

      ^
    7 |
    6 |     
    5 |      
    4 |      
    3 |    *
    2 |  .
    1 |.
      +--------------> 
       1 2 3 4 5 6 7


Now, you remeber I said taht r <cos(theta), sin(theta)> is a vector.

Well, we can get r.   Squareroot(X^2 + Y^2) = r

3^2 + 3^2 = 9 + 9 = 18.

Squareroot(18) = approx 4.24 is the length traveled (Measurement is arbitarily
                                                     undefined here)


4.24 <cos(theta), sin(theta)>

So, how would we get theta?  Simple trig.

arccos(3/4.24) = arcsin(3/4.24) = approx. 45 Degrees

4.24 <cos(45), sin(45)> = (Approx) <3, 3>

(I have rounded the number 4.24 up so it won't be exactly 3, you have to consider
 how many significant digits you keep)



What are properties of vectors?




If you have two vectors on top of each other like so:
 Vector u
  |
  |
  |    
  |
 \|/
  *-------> Vector v

If you have 2 vectors sitting end to end on top of each other (Order does not matter)
if you add them, you should get a third vector as so:

u+v=w


 Vector u
  |\
  |  \
  |   \   Vector w  
  |     \
 \|/     \
  *-------> Vector v


That should be the straight distance to get to v at the u starting point.

There are other properties of vectors like the cross product and the dot product.

The Dot Product:

u.v = |u||v|cos(theta)


|v|  <-- that is vector notation for length (Sqrt(x^2 + y^2 + z^2))

cos(theta) is the angle between the 2 vectors.

The dot product is:
<Xu, Yu>
<Xv, Yv>

Xu*Xv + Yu*Yv

u = <3, 2>
v = <4, 1>

3*4 + 2*1 = 12 + 2 = 14

If you want to get the angle between them you can:

|u| = sqrt(3^2 + 2^2) = sqrt(9+4) = sqrt(13) = 3.60555
|v| = sqrt(4^2 + 1^2) = sqrt(16 + 1) = sqrt(17) = 4.123

arccos(14/(3.60555*4.123)) = arccos(14/14.86568265)
 = arccos(0.94176637088374814728067667985634) = 19.6496569302975138333393721864567

Approx. 20 degrees angle between the two vectors.



The Unit Vector is a vector of length 1.  To get this, simpley

v/|v|

Divide the vector by it's length.

The Cross product.

It's best to put the cross product into a matrix.
You cannot do a cross product without at least a 3-D Vector.

<X, Y, Z>

u = <Xu, Yu, Zu>
v = <Xv, Yv, Zv>


u X v = <(Yu*Zv) - (Zu*Yv), - ((Xu*Zv) - (Zu*Xv)), (Xu*Yv) - (Yu*Xv)>


This will give you a vector that is perpendicular to the 2 vectors.

This is good in backface removal in 3D graphics.  You can tell which direction
the faces of an object are pointing and if they are visible with this formula.


-----------------------------
This is all for now.  I may add more onto this, as the properties and such, but
I am just tring to get the page up right now.  This isn't meant to be an everything
you want to know about vectors, this is supposed to be a jumpstart, so you can
understand the basics enough to move on.



 
About Toby Opferman

Professional software engineer with over 15 years...

Learn more »
Codeproject Articles

Programming related articles...

Articles »
Resume

Resume »
Contact

Email: codeproject(at)opferman(dot)com