Tutorials | (back to the list of tutorials) |
import processing.opengl.*; import igeo.*; size( 480, 360, IG.GL ); IVec vec = new IVec(20, 10, 0); double xvalue = vec.x(); IG.p("x value is "+xvalue); double yvalue = vec.y(); IG.p("y value is "+yvalue); double zvalue = vec.z(); IG.p("z value is "+zvalue);
import processing.opengl.*; import igeo.*; size( 480, 360, IG.GL ); IVec v1 = new IVec(0, 10, 20); IVec v2 = v1.dup(); IVec v3 = new IVec(v1); IG.p("vector1 = "+v1); IG.p("vector2 = "+v2); IG.p("vector3 = "+v3);
import processing.opengl.*; import igeo.*; size( 480, 360, IG.GL ); IVec v1 = new IVec(20, 10, 0); IVec v2 = new IVec(10, 10, 10); IVec 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 IG.p("v1 = "+v1); IG.p("v2 = "+v2); IG.p("v3 = "+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.
import processing.opengl.*; import igeo.*; size( 480, 360, IG.GL ); IVec v1 = new IVec(20, 10, 0); IVec v2 = new IVec(10, 10, 10); IVec 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 IG.p("v1 = "+v1); IG.p("v2 = "+v2); IG.p("v3 = "+v3);
Vector subtraction can be diagrammed like the below.
import processing.opengl.*; import igeo.*; size( 480, 360, IG.GL ); IVec v1 = new IVec(10, 20, 30); IVec v2 = v1.dup(); v2.mul(1.5); IVec v3 = new IVec(30, -10, -10); IVec 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 IG.p("v1 = " + v1); IG.p("v2 = " + v2); IG.p("v3 = " + v3); IG.p("v4 = " + v4);
Vector multiplication can be described like the below. Vector division can be seen as multiplication of inverted scalar number.
import processing.opengl.*; import igeo.*; size( 480, 360, IG.GL ); IVec v1 = new IVec(30, 20, 10); IVec v2 = v1.dup(); v2.flip(); v1.show().clr(1.,0,0); // red v2.show().clr(1.,1.,0); // yellow IG.p("v1 = " + v1); IG.p("v2 = " + v2);
import processing.opengl.*; import igeo.*; size( 480, 360, IG.GL ); IVec v1 = new IVec(10,10,0); double length = v1.len(); IVec v2 = new IVec(10,-10,0); double distance = v1.dist(v2); IVec v3 = v1.dup(); v3.unit(); IVec 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 IG.p("length of v1 = " + length); IG.p("distance between v1 and v2 = " + distance); IG.p("v3 = " + v3); IG.p("v4 = " + v4);
Those operations can also be described as the following.
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.
import processing.opengl.*; import igeo.*; size( 480, 360, IG.GL ); IVec v1 = new IVec(10,20,30); IVec v2 = new IVec(10,10,10); double dotValue = v1.dot(v2); IG.p("dot product = "+dotValue);
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.
import processing.opengl.*; import igeo.*; size( 480, 360, IG.GL ); IVec v1 = new IVec(10, 20, 30); IVec v2 = new IVec(5, 20, 10); IVec v3 = v1.cross(v2); v1.show().clr(1.,0,0); v2.show().clr(0,0,1.); v3.show().clr(1.,1.,0);
import processing.opengl.*; import igeo.*; size( 480, 360, IG.GL ); IVec v1 = new IVec(0, 10, 10); IVec v2 = new IVec(0, -5, 5); IVec axisVec = new IVec(1, 0, 0); double angle1 = v1.angle(v2); double angle2 = v1.angle(v2, axisVec); double angle3 = v2.angle(v1, axisVec); IG.p("angle1 = "+angle1); IG.p("angle2 = "+angle2); IG.p("angle3 = "+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.
import processing.opengl.*; import igeo.*; size( 480, 360, IG.GL ); IVec v1 = new IVec(10, 10, 10); IVec axis = new IVec(0, 0, 10); IVec v2 = v1.dup(); v2.rot(axis, PI/2); IVec center = new IVec(10,0,0); IVec 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); IPoint centerPt = new IPoint(center); axis.show(center).clr(0,0,1.); v3.show().clr(1.,.5,0);
import processing.opengl.*; import igeo.*; size( 480, 360, IG.GL ); IVec v1 = new IVec(10,10,10); IVec planeNormal = new IVec(10,0,0); IVec v2 = v1.dup(); v2.ref(planeNormal); IVec planeCenter = new IVec(20,0,0); IVec 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); new IPoint(planeCenter); planeNormal.show(planeCenter).clr(0,0,1.); v3.show().clr(1.,.5,0);
import processing.opengl.*; import igeo.*; size( 480, 360, IG.GL ); IVec v1 = new IVec(70, 20, 90); IVec v2 = new IVec(50, 50, 40); for(int i=0; i < 10; i++){ for(int j=0; j < 10; j++){ for(int k=0; k < 10; k++){ IVec v = new IVec(i*10, j*10, k*10); if( v.eq(v1) ){ new IPoint(v).clr(1.,.8,1.); } else if( v.eq(v2, 30) ){ new IPoint(v).clr(1.,0,0); } else{ new IPoint(v); } } } }
IVec v3 = v2.dif(v1);
IVec v3 = v2.dup().sub(v1);
import processing.opengl.*; import igeo.*; size( 480, 360, IG.GL ); IVec v1 = new IVec(30,-20,-10); IVec v2 = new IVec(20,0,20); IVec v3 = v2.dif(v1); v1.show().clr(1.,0,0); v2.show().clr(0,0,1.); v3.show(v1).clr(1.,1.,0);
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());
import processing.opengl.*; import igeo.*; size( 480, 360, IG.GL ); IVec v1 = new IVec(10,0,0); IVec v2 = new IVec(10,10,10); IVec v3 = v1.mid(v2); IVec 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
import processing.opengl.*; import igeo.*; size( 480, 360, IG.GL ); IVec v1 = new IVec(20,0,-10); IVec v2 = new IVec(10,20,30); IVec total1 = v1.sum(v2); // sum of two vectors IVec v3 = new IVec(-20,30,-20); IVec v4 = new IVec(-30,-10,30); IVec v5 = new IVec(-10,-40,-10); IVec 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.);
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));
import processing.opengl.*; import igeo.*; size( 480, 360, IG.GL ); IVec v1 = new IVec(50,0,-10); IVec v2 = new IVec(30,10,40); IVec u1 = v1.sum(v2, 0.75, 0.25); IVec u2 = v1.sum(v2, 0.5, 0.5); IVec u3 = v1.sum(v2, 0.25, 0.75); IVec v3 = new IVec(-20,10,-10); IVec v4 = new IVec(-20,10,30); IVec u4 = v3.sum(v4, 0.25); IVec u5 = v3.sum(v4, 0.5); IVec 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();