// UdK Computational design // Marius Watz 2003 // http://www.evolutionzone.com/udk/ // // UDK03_02_vector // // Introducing the Vec2D library. // Calulate vector between two points, then draw // a line half the distance of that vector. Vec2D v,v2; float x,y,x2,y2; void setup() { size(400,400); background(200,160,0); v=new Vec2D(); v2=new Vec2D(); ellipseMode(CENTER_DIAMETER); } void loop() { x=100; y=100; x2=300; y2=300; v.x=x2-x; v.y=y2-y; v.x=v.x/2; v.y=v.y/2; fill(255); noStroke(); ellipse(x,y, 20,20); ellipse(x2,y2, 20,20); noFill(); stroke(255,0,0); line(x,y, x+v.x, y+v.y); } // General vector class for 2D vectors class Vec2D { float x,y; // Constructor with no parameters Vec2D() { x=0; y=0; } Vec2D(float _x,float _y) { x=_x; y=_y; } Vec2D(Vec2D v) { x=v.x; y=v.y; } void set(float _x,float _y) { x=_x; y=_y; } void set(Vec2D v) { x=v.x; y=v.y; } void add(float _x,float _y) { x+=_x; y+=_y; } void add(Vec2D v) { x+=v.x; y+=v.y; } void sub(float _x,float _y) { x-=_x; y-=_y; } void sub(Vec2D v) { x-=v.x; y-=v.y; } void mult(float m) { x*=m; y*=m; } void div(float m) { x/=m; y/=m; } float length() { return sqrt(x*x+y*y); } float angle() { return atan2(y,x); } void normalise() { float l=length(); if(l!=0) { x/=l; y/=l; } } Vec2D tangent() { return new Vec2D(-y,x); } void rotate(float val) { // Due to float not being precise enough, double is used for the calculations double cosval=Math.cos(val); double sinval=Math.sin(val); double tmpx=x*cosval - y*sinval; double tmpy=x*sinval + y*cosval; x=(float)tmpx; y=(float)tmpy; } }