improved class Vector – the code

// a class for two-dimensional vectors, similarly as PVector, with extensions for complex numbers
//-------------------------------------------------------------
// 
//  some important values

float vectorSmall=0.0001;
float vectorDiameter=8;
color vectorColor=color(255);     

//  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();returns a copy of a
Vector copy(){
  return new Vector(this);
}
//  methods to show the vector
// -----------------------------------------------------

//  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);
  }


//   methods change the vector....
//-------------------------------------------------------------------------------------
//  basic operations: addition subtraction multiplication division

//  a.add(b);then a will be the sum of a and b, returns a
  Vector add(Vector v){
    x+=v.x;
    y+=v.y;
    return this;
  }
  
//  a.sub(b);  then a will become the difference of a and b
  Vector sub(Vector v){
    x-=v.x;
    y-=v.y;
    return this;
  }

//  a.mult(t); multiplies vector a with factor t
//  for division use: a.mult(1./t);

  Vector mult(float t){
    x*=t;
    y*=t;
    return this;
  }

//   complex multiplication, vectors as complex numbers
//   use for rotations: b=cos(angle)+i*sin(angle);

  Vector mult(Vector b){
    float h=x*b.x-y*b.y;
    y=x*b.y+y*b.x;
    x=h;
    return this;
  }
  
//   complex division, vectors as complex numbers
  Vector div(Vector b){
    float n=1./(b.x*b.x+b.y*b.y);
    float h=(x*b.x+y*b.y)*n;
    y=(y*b.x-x*b.y)*n;
    x=h;
    return this;
  }
  
//  a.normalize() sets magnitude of a equal to one
  Vector normalize(){
    float h=1./sqrt(x*x+y*y);
    y*=h;
    x*=h;  
    return this;
  } 
  
//  a.unitInverse() makes inversion at unit circle
  Vector unitInverse(){
    float h=1./(x*x+y*y);
    y*=h;
    x*=h;  
    return this;
  } 
  
//  a.complexInverse() makes inversion as complex number
  Vector complexInverse(){
    float h=1./(x*x+y*y);
    y*=-h;
    x*=h;  
    return this;
  } 
  
//  a.complexConjugate() makes complex conjugation
  Vector complexConjugate(){
    y=-y;
    return this;
  } 
  
//  a.interpolate(t,b) makes the vector (1-t)*a+t*b
  Vector interpolate(float t,Vector b){
    x=(1-t)*x+t*b.x;
    y=(1-t)*y+t*b.y;
    return this;
  }
  
//  more complex functions that modify the vector
//-------------------------------------------------------------------

//   a.power(n)  makes the n-th power
  Vector power(int n){
    float h,thisX,thisY;
    thisX=x;
    thisY=y;
    for (int i=2;i<=n;i++){
      h=x*thisX-y*thisY;
      y=y*thisX+x*thisY;
      x=h;
    }
    return this;
  }
  
//   a.cLog()  gives the natural logarithm of a
  Vector cLog(){
    float h=0.5*log(x*x+y*y);
    y=atan2(y,x);
    x=h;
    return this;
  }
  
//   a.cExp()  gives the exponential of a
  Vector cExp(){
    float r=exp(x);
    x=r*cos(y);
    y=r*sin(y);
    return this;
  }
  
// functions essentially, that do not modify the vector
//---------------------------------------------------------------

//  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.rsq()  returns the square of the length of a
  float rsq(){
    return x*x+y*y;
  }
  
//  a.r()  returns the length of a
  float r(){
    return sqrt(x*x+y*y);
  }
  
// a.phi() returns the angle of a in radians, going from -PI to +PI
  float phi(){
    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;
  }  
}
This entry was posted in programming, Uncategorized and tagged , , . Bookmark the permalink.

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