|
Author |
Topic: Vectors and coordinate tools (Read 1007 times) |
|
mflux
|
Vectors and coordinate tools
« on: Oct 29th, 2003, 9:27am » |
|
EXAMPLE: http://www.design.ucla.edu/~mflux/p5/vectors1/applet/ I don't know how useful this will be for people but I'm putting it up anyways. I've been using it non-stop for the last couple of projects. It's a simple coordinate system / vectors toolkit. This is sort of the first time I wrote something like this. I wrote it for myself, because I have a hard time figuring out other people's code and I just don't bother using code and disecting them anymore. If you see ways to improve this PLEASE LET ME KNOW!! I know for a fact the system I wrote is far from perfect and I'm dying for suggestions on how to improve it. I'm still a beginner so there's a lot I don't know how to optimize, or just don't see. Feel free to take any of this for your own usage. It's useful for me for a variety of simulation type things such as physics, and what have you. As a last note, everything here works in angles. I have a strange hatred for PI-angle calculations. float conversionArc=180/PI; //180/PI used for many calculations class position //position class that stores 2 values x and y { float x,y; position(){} position(float tx,float ty) { x=(tx); y=(ty); } position(position p) { x=p.x; y=p.y; } position displace(float angle,float magnitude) //returns the position displaced by an angle and distance { position newP=new position(x,y); newP.x+=(cos(radians(angle))*magnitude); newP.y-=(sin(radians(angle))*magnitude); return newP; } void set(float tx,float ty) //sets a new coordinate { x=(tx); y=(ty); } } class vector //vector class that stores a position, angle, and magnitude { float a,m; position p; vector(){} vector(float tx,float ty,float angle,float magnitude) { p=new position(tx,ty); a=angle; m=magnitude; } vector(position pos,float angle,float magnitude) { p=new position(pos); a=angle; m=magnitude; } vector(vector v) { p=new position(v.p); a=v.a; m=v.m; } position endPoint() //returns the endpoint of the vector as type position { return displace(p,a,m); } vector add(vector v) //returns a vector addition { position newEnd=new position(endPoint().displace(v.a,v.m)); float newAngle=getHeading(p,newEnd); vector newVector=new vector(p,newAngle,dist(p.x,p.y,newEnd.x,newEnd.y)); return newVector; } vector add(float angle,float magnitude) //returns a vector addition { vector v=new vector(p,angle,magnitude); position newEnd=new position(endPoint().displace(v.a,v.m)); float newAngle=getHeading(p,newEnd); vector newVector=new vector(p,newAngle,dist(p.x,p.y,newEnd.x,newEnd.y)); return newVector; } vector sub(vector v) //returns a vector subration (unimplemented) { return v; } void setStart(position np) //sets a new starting point for the vector { p=np; } void setEnd(position np) //sets a new ending point for the vector { a=getHeading(p,np); m=dist(p.x,p.y,np.x,np.y); } void setEnd(float px,float py) //sets a new ending point for the vector { position np=new position(px,py); a=getHeading(p,np); m=dist(p.x,p.y,np.x,np.y); } } position displace(position p,float angle,float magnitude) //displaces a position by an angle and a magnitude, then returns it { position newP=new position(p); // angle=atan2(my-y,mx-x); // angle=-1*angle*180/PI; // if(my>y) // angle=360-(angle*-1); newP.x+=(cos(radians(angle))*magnitude); newP.y-=(sin(radians(angle))*magnitude); return newP; } float getHeading(position p1,position p2) //gets the absolute heading between one position and another relative to the first { float angle=atan2(p2.y-p1.y,p2.x-p1.x); angle=-1*angle*conversionArc; if(p2.y>=p1.y) angle=360-(angle*-1); return angle; } float normalizeHeading(float ang) //normalizes a heading between 0 and 180 { while(ang >= 360)ang -= 360; while(ang <= 0)ang += 360; return ang; } void drawVector(vector v) //draws a vector in white { colorMode(RGB,255,255,255); stroke(255); noFill(); ellipseMode(CENTER_DIAMETER); ellipse(v.p.x,v.p.y,5,5); position e=v.endPoint(); line(v.p.x,v.p.y,e.x,e.y); } void drawVector(vector v,int s) //draws a vector with a longer line for representation in white { colorMode(RGB,255,255,255); stroke(255); noFill(); ellipseMode(CENTER_DIAMETER); ellipse(v.p.x,v.p.y,5,5); v.m*=s; position e=v.endPoint(); line(v.p.x,v.p.y,e.x,e.y); } void drawVector(vector v,int r,int g,int b) //draws a vector in RGB { colorMode(RGB,255,255,255); stroke(r,g,b); noFill(); ellipseMode(CENTER_DIAMETER); ellipse(v.p.x,v.p.y,5,5); position e=v.endPoint(); line(v.p.x,v.p.y,e.x,e.y); }
|
« Last Edit: Oct 29th, 2003, 10:43am by mflux » |
|
|
|
|
mflux
|
Re: Vectors and coordinate tools
« Reply #2 on: Oct 29th, 2003, 10:31am » |
|
These are a bit complicated for demonstrating this but I'll put these up for now. I linked it in exhibition earlier today. A prime example of using this is simulating moving objects with velocity and acceleration. http://classes.design.ucla.edu/Fall03/157A/cursos/00/index_visor.php?id= 8&ejercicio_id=5&persona_id=20 A falling ball consists of it's own velocity vector and is influenced by the gravity vector. Every iteration the ball's velocity vector is modified by the gravity vector through vector math. The result is a fluid motion. This is useful to define forces where things have an angle and magnitude. Another way of using this is demonstrated in the autonomous road homework: http://classes.design.ucla.edu/Fall03/157A/cursos/00/index_visor.php?id= 8&ejercicio_id=6&persona_id=20 Each sub-road is a vector. It is simply visualized, instead of applied. It's a handy way of organizing things when your objects are determined by properties of positions, angles, and magnitudes. I'll write some clearer examples later.
|
|
|
|
mflux
|
Re: Vectors and coordinate tools
« Reply #3 on: Oct 29th, 2003, 10:43am » |
|
Here is a quick example of its utility: http://www.design.ucla.edu/~mflux/p5/vectors1/applet/ vector m,s; void setup() { size(400,400); m=new vector(new position(width/2,height/2),40,100); s=new vector(m.endPoint(),20,60); } void loop() { background(0); m.a--; s.a+=2; s.p=m.endPoint(); drawVector(m); drawVector(s); drawVector(m.add(s)); fill(255); ellipseMode(CENTER_DIAMETER); ellipse(m.endPoint().x,m.endPoint().y,10,10); ellipse(s.endPoint().x,s.endPoint().y,20,20); } Basically... I created 2 vectors, M and S. S's position is attatched to M's ending position. M's angle is being subracted while S's angle is being increased. A third vector is displayed as their resultant vector addition (line from the beginning of the first to the end of the second).
|
« Last Edit: Oct 29th, 2003, 1:00pm by mflux » |
|
|
|
|
|