Here's mine with a bit bloated set of tools
I've written this a while back and have been adding/changing it every time I learn something new about coding. This is only a snippet of my usual vector lib.
In general I dislike using n-dimensional arrays to do lists of vectors, but it's a bit faster if you do. Also, when making libraries, it's good to use array vectors since you don't want many versions of your Vector class.
I sometimes make lists of these Vec2 objects in a java Vector() list (not to be confused with mathematical vectors like below). This is again slower, but easier to add things into the array, delete things, insert things, etc. The only drawback to that method is that you must type-cast it back into a Vec2 every time you use it.
Note that some of these methods abuse
Code: new Object()
and can cause memory thrashing if you have say a hundred thousand objects.
Code:
class Vec2
{
float x;
float y;
Vec2()
{//constructor with no arguments, gives you origin
x=0;
y=0;
}
Vec2(float x,float y)
{//constructor with 2 arguments x and y
this.x=x;
this.y=y;
}
Vec2(Vec2 s)
{//constructor with Vec as input. basically an assignment operator
if(s==null)
return;
this.x=s.x;
this.y=s.y;
}
Vec2(Vec2 s,Vec2 t)
{//constructs a Vector from the difference of two Vectors
float dx=t.x-s.x;
float dy=t.y-s.y;
this.x=dx;
this.y=dy;
}
Vec2(Vec2 s,Vec2 t,float k)
{//constructs a Vector from the difference of two Vectors, then modifies it by k
float dx=t.x-s.x;
float dy=t.y-s.y;
this.x=dx*k;
this.y=dy*k;
}
//--------------BEGINNING OF VECTOR METHODS--------------
void disp(float ang,float magnitude)
{//displacement. will offset this Vector by an angle and distance
ang=radians(ang);
x+=cos(ang)*magnitude;
y-=sin(ang)*magnitude;
}
void rotate(float ang)
{
Vec2 temp=new Vec2();
temp.disp(ang()+ang,mag());
x=temp.x;
y=temp.y;
}
void rotate(Vec2 v,float ang)
{
Vec2 cen=new Vec2(v);
Vec2 ori=new Vec2(v,this);
ori.rotate(180+ang);
cen.add(ori);
this.x=cen.x;
this.y=cen.y;
return;
}
float ang()
{//returns the angle between this Vector and origin
return getAng(this,origin2);
}
float mag()
{//returns the distance between this Vector and origin
return dist(origin2,this);
}
//scalar operations
void add(float s)
{//addition operator
x+=s;
y+=s;
}
void sub(float s)
{//subtraction operator
x-=s;
y-=s;
}
void mul(float s)
{//multiplication operator
x*=s;
y*=s;
}
void div(float s)
{//division operator. returns 0 when division by zero
if(s==0)
return;
x/=s;
y/=s;
}
void add(float x,float y)
{//addition operator
this.x+=x;
this.y+=y;
}
void sub(float x,float y)
{//subtraction operator
this.x-=x;
this.y-=y;
}
void mul(float x,float y)
{//multiplication operator
this.x*=x;
this.y*=y;
}
void div(float x,float y)
{//division operator. returns 0 when division by zero
if(x==0||y==0)
return;
this.x/=x;
this.y/=y;
}
//Vector operators
void add(Vec2 s)
{//addition operator
x+=s.x;
y+=s.y;
}
void sub(Vec2 s)
{//subtraction operator
x-=s.x;
y-=s.y;
}
void mul(Vec2 s)
{//multiplication operator
x*=s.x;
y*=s.y;
}
void div(Vec2 s)
{//division operator. returns 0 when division by zero
if(s.x==0||s.y==0)
return;
x/=s.x;
y/=s.y;
}
}