Python Tutorials | (back to the list of tutorials) |

After you create a 3 dimensional vector, you can get each value of
x, y and z with the method **x()**, **y()** and
**z()**.

add_library('igeo') size( 480, 360, IG.GL ) vec = IVec(20, 10, 0) xvalue = vec.x()print("x value is " + str(xvalue) ) yvalue = vec.y()print("y value is " + str(yvalue) ) zvalue = vec.z()print("z value is " + str(zvalue) )

To duplicate a vector variable to another variable,
you use **dup()** method or **new IVec()**
with the existing vector in the argument.
You can also use **cp()** method, which is just an alias
of **dup()**.

add_library('igeo') size( 480, 360, IG.GL ) v1 = IVec(0, 10, 20) v2 = v1.dup()# v2 = v1.cp()# cp() is alias of dup() v3 =IVec(v1)print("vector1 = " + str(v1) ) print("vector2 = " + str(v2) ) print("vector3 = " + str(v3) )

You can add 2 vectors by **add()** method.
Note that because **add()** change the value of the
variable itself instead creating a new added value, you have to
duplicate beforehand the vector you add another into, to keep
the original value.

add_library('igeo') size( 480, 360, IG.GL ) v1 = IVec(20, 10, 0) v2 = IVec(10, 10, 10) v3 = v2.dup() v3.add(v1)# visualizing vectors as arrows v1.show().clr(1.,0,0) # red v2.show().clr(0,0,1.) # blue v3.show().clr(1.,0,1.) # magenta print("v1 = " + str(v1) ) print("v2 = " + str(v2) ) print("v3 = " + str(v3) )

Mathematical meaning of vector addition is like the following diagram.

Subtracting vectors is done in the same way with adding.
Please use **sub()** method.

add_library('igeo') size( 480, 360, IG.GL ) v1 = IVec(20, 10, 0) v2 = IVec(10, 10, 10) v3 = v2.dup() v3.sub(v1)# visualizing vectors as arrows v1.show().clr(1.,0,0) # red v2.show().clr(0,0,1.) # blue v3.show().clr(1.,0,1.) # magenta print("v1 = " + str(v1) ) print("v2 = " + str(v2) ) print("v3 = " + str(v3) )

Vector subtraction can be diagrammed like the below.

To multiply a vector with double, please use **mul()** method.

To divide a vector with double, please use**div()** method.

To divide a vector with double, please use

add_library('igeo') size( 480, 360, IG.GL ) v1 = IVec(10, 20, 30) v2 = v1.dup() v2.mul(1.5)v3 = IVec(30, -10, -10) v4 = v3.dup() v4.div(2.0)v1.show().clr(1.,0,0) # red v2.show().clr(1.,1.,0) # yellow v3.show().clr(0,0,1.) # blue v4.show().clr(0,1.,1.) # cyan print("v1 = " + str(v1) ) print("v2 = " + str(v2) ) print("v3 = " + str(v3) ) print("v4 = " + str(v4) )

Vector multiplication can be described like the below. Vector division can be seen as multiplication of inverted scalar number.

Use **flip()** method (it also has aliases of **neg()** and **rev()**)
to reverse the direction of the vector keeping the same length.
You can also get the same result with **mul(-1)**.

add_library('igeo') size( 480, 360, IG.GL ) v1 = IVec(30, 20, 10) v2 = v1.dup() v2.flip()v1.show().clr(1.,0,0) # red v2.show().clr(1.,1.,0) # yellow print("v1 = " + str(v1) ) print("v2 = " + str(v2) )

You can measure the length of the vector by **len()** method.
You can also measure the distance between two vectors by **dist()** method.
To unitize the vector making the length into 1 keeping the same direction,
please use **unit()**.
To set the length of a vector, you can use **len(double)**
method putting a double value as length at the argument .

add_library('igeo') size( 480, 360, IG.GL ) v1 = IVec(10,10,0) length = v1.len()v2 = IVec(10,-10,0) distance = v1.dist(v2)v3 = v1.dup() v3.unit()v4 = v1.dup() v4.len(10)v1.show().clr(1.,0,0) #red v2.show().clr(0,0,1.) #blue v3.show().clr(1.,1.,0).size(0.5) #yellow w head size 0.5 v4.show().clr(1.,.5,0) #orange print("length of v1 = " + str(length) ) print("distance between v1 and v2 = " + str(distance) ) print("v3 = " + str(v3) ) print("v4 = " + str(v4) )

Those operations can also be described as the following.

Dot product is an operation to generate one scalar value out of two vectors.
Please see the diagram below for the definition. The value of
the dot product is related to projection of a vector to another vector and
the angle between two vectors.

In IVec class, you can use **dot()** method with another vector
put in the argument. Please note that
**dot()** method is returning a value in **double**,
not a vector.

add_library('igeo') size( 480, 360, IG.GL ) v1 = IVec(10,20,30) v2 = IVec(10,10,10) dotValue =v1.dot(v2)# result is a floating point number, not a vector print("dot product = " + str(dotValue) )

Cross product is another product of 2 vectors and in this one,
it generates a new vector, not a scalar value.
This vector generated by cross product of two vectors is called
a cross vector.
The most important property of a cross vector is that
it is always perpendicular to both of two input vectors.
Because of this property, cross product is usually used to
produce normal vector of various geometries.
See below for the definition and properties.

In iGeo, you can calculate a cross product by
**cross()** with another vector put in the argument.
Please note that **cross()** generates a new vector,
without changing the contents of itself.
which is different behavior from add/sub/mul/div where
the result of the method is contained inside the one of
input vectors changing its contents.

add_library('igeo') size( 480, 360, IG.GL ) v1 = IVec(10, 20, 30) v2 = IVec(5, 20, 10) v3 =v1.cross(v2)v1.show().clr(1.,0,0) v2.show().clr(0,0,1.) v3.show().clr(1.,1.,0)

Angle of two vectors can be measured by **angle()** method.
There are two different way to put arguments in **angle()** method.
The first is just putting one vector to which the angle is measured and
this method returns a value from 0 to Pi (unit is radian).
The second type of arguments is one vector to which the angle is measured
and another vector just to check direction of angle and this returns
a value from -Pi to Pi.
The second argument doesn't need to be perpendicular to two input vectors
but usually it's some vector close to be perpendicular to the two.

add_library('igeo') size( 480, 360, IG.GL ) v1 = IVec(0, 10, 10) v2 = IVec(0, -5, 5) axisVec = IVec(1, 0, 0) angle1 =v1.angle(v2)angle2 =v1.angle(v2, axisVec)angle3 =v2.angle(v1, axisVec)print("angle1 = " + str(angle1) ) print("angle2 = " + str(angle2) ) print("angle3 = " + str(angle3) )

To rotate a vector, you use **rot()** method.
There are two different types of arguments.
In the first one, you put an axis vector in the first
argument and angle in the second argument
as a double value (the unit is in radian).
The second type of the argument is that
you put a center point of rotation as IVec in the first
argument and the second is an axis vector and
the third is rotation angle.

add_library('igeo') size( 480, 360, IG.GL ) v1 = IVec(10, 10, 10) axis = IVec(0, 0, 10) v2 = v1.dup() v2.rot(axis, PI/2)center = IVec(10,0,0) v3 = v1.dup() v3.rot(center, axis, -PI/2)v1.show().clr(1.,0,0) axis.show().clr(0,0,1.) v2.show().clr(1.,1.,0) centerPt = IPoint(center) axis.show(center).clr(0,0,1.) v3.show().clr(1.,.5,0)

To reflect a vector on 3 dimensional plane, you use
**ref()** method.
To use this method, you put a vector which specify a plane
in space.
If you put one vector in the argument, it's read as
a normal vector of a plane and the plane is going through the origin.
If you put two vectors in the arguments, the first vector is read as
a center and the second is normal of a plane and the plane is going through
the center.

add_library('igeo') size( 480, 360, IG.GL ) v1 = IVec(10,10,10) planeNormal = IVec(10,0,0) v2 = v1.dup() v2.ref(planeNormal)planeCenter = IVec(20,0,0); v3 = v1.dup() v3.ref(planeCenter, planeNormal)v1.show().clr(1.,0,0) planeNormal.show().clr(0,0,1.) v2.show().clr(1.,1.,0) IPoint(planeCenter) planeNormal.show(planeCenter).clr(0,0,1.) v3.show().clr(1.,.5,0)

To compare two vectors to check if they are pointing the same location
(having same x, y and z values),
use **eq()** method with one argument of another vector.
This method returns Boolean value and you can use this inside
if condition.
**eq()** can take another set of arguments adding a double value
at the second argument as resolution of comparison.
In this case, **eq()** returns true when the distance
between two vector is smaller than the resolution.

add_library('igeo') size( 480, 360, IG.GL ) v1 = IVec(70, 20, 90) v2 = IVec(50, 50, 40) for i in range(10) : for j in range(10) : for k in range(10) : v = IVec(i*10, j*10, k*10) ifv.eq(v1): IPoint(v).clr(1.,.8,1.) elifv.eq(v2, 30): IPoint(v).clr(1.,0,0) else : IPoint(v)

Sometimes when you are using vectors as point locations you would
need a difference of two location. In this case,
you can get difference vector by **dif()** method
(or its alias **diff()**)
putting a root location vector in the argument.
You can do same thing with ** sub() ** method.
The difference between **dif()** and ** sub() **
is that **sub()** change the content of the vector itself but
**dif()** creates a new vector without changing the
content of the vectors.
So, if you use **dup()** method (or cp() method)
with **sub()**,
it's exactly same with **dif()** as the following
two lines are exactly same.

IVec v3 = v2.dif(v1);

IVec v3 = v2.dup().sub(v1);

add_library('igeo') size( 480, 360, IG.GL ) v1 = IVec(30,-20,-10) v2 = IVec(20,0,20) v3 = v2.dif(v1)v1.show().clr(1.,0,0) v2.show().clr(0,0,1.) v3.show(v1).clr(1.,1.,0)

Similarly to **dif()** method, there are methods to create
new vectors out of two vectors.
One of those method is **mid()** to create a new vector
of mid point of two vectors.
Another method is **bisect()** to create a new vector
of bisector of two vectors.
Each of **mid()** method and **bisect()** method
is equivalent to the following statements with **dup()** method.

IVec v3 = v1.mid(v2);

IVec v3 = v1.dup().add(v2).div(2);

IVec v4 = v1.bisect(v2);

IVec v4 = v1.dup().unit().add(v2.dup().unit());

add_library('igeo') size( 480, 360, IG.GL ) v1 = IVec(10,0,0) v2 = IVec(10,10,10) v3 = v1.mid(v2)v4 = v1.bisect(v2)v1.show().clr(1.,0,0) v2.show().clr(0,0,1.) v3.show().clr(1.,1.,0) v4.show().clr(1.,.5,0).size(1) # making a head small

Another method to create a new vector without changing the
content of original vectors is summation method **sum()**.
You can put one or more vectors in the argument.

add_library('igeo') size( 480, 360, IG.GL ) v1 = IVec(20,0,-10) v2 = IVec(10,20,30) total1 = v1.sum(v2)# sum of two vectors v3 = IVec(-20,30,-20) v4 = IVec(-30,-10,30) v5 = IVec(-10,-40,-10) total2 = v1.sum(v2,v3,v4,v5)# sum of five vectors v1.show() v2.show() v3.show() v4.show() v5.show() total1.show().clr(1.,0,0) total2.show().clr(0,0,1.)

Weighted sum operation is useful when you
want to put series of points between two points.
There are two different ways to use weighted sum.
The method name is still ** sum() ** and
you can put either one vector and one double value
in the argument or
one vector and two double values in the argument.
These two variations are equivalent to the following
methods using **dup()**.

IVec v3 = v1.sum(v2, 0.2, 0.8);

IVec v3 = v1.dup().mul(0.2).add(v2.dup().mul(0.8));

IVec v4 = v1.sum(v2, 0.8);

IVec v4 = v1.dup().mul(1-0.8).add(v2.dup().mul(0.8));

add_library('igeo') size( 480, 360, IG.GL ) v1 = IVec(50,0,-10) v2 = IVec(30,10,40) u1 = v1.sum(v2, 0.75, 0.25)u2 = v1.sum(v2, 0.5, 0.5)u3 = v1.sum(v2, 0.25, 0.75)v3 = IVec(-20,10,-10) v4 = IVec(-20,10,30) u4 = v3.sum(v4, 0.25)u5 = v3.sum(v4, 0.5)u6 = v3.sum(v4, 0.75)v1.show().clr(1.,0,0) v2.show().clr(0,0,1.) v3.show().clr(1.,1.,0) v4.show().clr(1.,.5,0) u1.show() u2.show() u3.show() u4.show() u5.show() u6.show()