class Vector – the code

// a class for two-dimensional vectors, similarly as PVector
//-------------------------------------------------------------
// 
//  some important values

float vectorSmall=0.0001,vectorSqrt05=sqrt(0.5);
float vectorDiameter=8;
color vectorColor=color(255);     //rgb mode, for green

//  and here's the class

class Vector{
  float x,y;
  
  // create a vector from known components
  Vector(float xp,float yp){
    x=xp;
    y=yp;
  }

//  create a new vector as a copy of another vector  
  Vector(Vector v){
    x=v.x;
    y=v.y;
  }
  
// a.set(u,v);then a will have components u and v
  void set(float u,float v){
    x=u;
    y=v;
  }
  
//a.copy(b);then a will be put equal to b
void copy(Vector b){
  x=b.x;
  y=b.y;
}

//  a.add(b);then a will be the sum of a and b
  void add(Vector v){
    x+=v.x;
    y+=v.y;
  }

//  a.add(u,v);  then a will become the sum of a and the vector with components (u,v)
  void add(float u,float v){
    x+=u;
    y+=v;
  }
  
//  a.sub(b);  then a will become the difference of a and b
  void sub(Vector v){
    x-=v.x;
    y-=v.y;
  }

//  a.sub(u,v);  then a will become the difference of a and the vector with components (u,v)
  void sub(float u,float v){
    x-=u;
    y-=v;
  }
  
//  a.mult(t); multiplies vector a with factor t
//  for division use: a.mult(1./t);

  void mult(float t){
    x*=t;
    y*=t;
  }
  
//  a.normalize() sets magnitude of a equal to one
  void normalize(){
    mult(1./theLength());
  }
  
// a.rotate90(); rotates a by 90 degrees
  void rotate90(){
    float h=x;
    x=-y;
    y=h;
  } 
  
// a.rotate45(); rotates a by 45 degrees
  void rotate45(){
    float xx=x,yy=y;
    x=vectorSqrt05*(xx-yy);
    y=vectorSqrt05*(xx+yy);
  }

// a.equal(b) returns true if vectors a and b are equal
boolean equal(Vector b){
  return (abs(x-b.x)<vectorSmall)&&(abs(y-b.y)<vectorSmall);
}
//  a.theLength()  returns the length of a
float theLength(){
  return mag(x,y);
}

// angle(a) returns the angle of a in radians
float theAngle(){
  return atan2(y,x);
}

// a.scalar(b) returns the scalar product of a and b
float scalar(Vector b){
  return x*b.x+y*b.y;
}

// a.cross(b) returns the z-component of the cross-product of a and b
float cross(Vector b){
  return x*b.y-y*b.x;
}


//  println("a="+a.asText());   to log vector a
  String asText(){
    return "("+x+","+y+")";
  }

//  a.show(); to display the point a in green or
//  a.show(someColor);
  void show(color someColor){
  ellipseMode(CENTER);
  fill(someColor);
  ellipse(x,y,vectorDiameter,vectorDiameter);
  }
  
  void show(){
    show(vectorColor);
  }
  
//  a.showLine(b); to draw a line between a and b
  void showLine(Vector b){
    line(x,y,b.x,b.y);
  }

}

//  here are some functions for Vector-objects that return vectors



//add(a,b) returns the sum of vectors a and b
Vector add(Vector a,Vector b){
  return new Vector(a.x+b.x,a.y+b.y);
}

//sub(a,b) returns the difference of vectors a and b
Vector sub(Vector a,Vector b){
  return new Vector(a.x-b.x,a.y-b.y);
}

// linearcombination(t,a,s,b) returns  t*a+s*b

Vector linearcombination(float t,Vector a,float s,Vector b){
  return new Vector(t*a.x+s*b.x,t*a.y+s*b.y);
}

// interpolate(t,a,b) returns the vector (1-t)*a+t*b
Vector interpolate(float t, Vector a,Vector b){
  return linearcombination(1-t,a,t,b);
}
This entry was posted in programming and tagged , , . Bookmark the permalink.

2 Responses to class Vector – the code

  1. Eric says:

    This is great! I look forward to seeing which tilings you implement!

    Bug report: I believe a.equal(b)’s single line of code was accidentally truncated. It reads
    return (abs(x-b.x)<vectorSmall)&&(abs(y-b.y)
    and I suppose it should read
    return (abs(x-b.x)<vectorSmall)&&(abs(y-b.y)<vectorSmall);

    • Peter Stampfli says:

      Thanks ! You are correct about the bug. I simply copied and pasted the code and I have no idea why this piece got lost. It is corrected now.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s