//-----------------------------------------VECTOR/COORDINATE LIBRARY //Written by Michael Chang //last revision 03/03/05 static float conversionArc=180.0/PI; //180/PI used for many calculations boolean useRK=false; //boolean to use Runge-Kutta for all displacements float rkIterations=2; //how many iterations to use Runge-Kutta 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) { if(p!=null) { x=p.x; y=p.y; } } position displace(float angle,float magnitude) //returns the position displaced by an angle and distance { float ra=radians(angle); position newP=new position(x,y); newP.x+=(cos(ra)*magnitude); newP.y-=(sin(ra)*magnitude); return newP; } position displace(vector v) { if(useRK) return displaceRK(v); else { position newP=new position(x,y); newP=newP.displace(v.a,v.m); return newP; } } position displaceRK(vector v) { position newP=new position(x,y); position k1=newP; position k2=k1.displace(v.a,v.m/2); position k3=k2.displace(v.a,v.m/2); position k4=k3.displace(v.a,v.m/2); // position k3 float kx=(k1.x+2*k2.x+2*k3.x+k4.x)/6; float ky=(k1.y+2*k2.y+2*k3.y+k4.y)/6; vector vec=new vector(newP,new position(kx,ky)); // vec.m*=4; /* position k[]=new position[int(rkIterations)]; k[0]=new position(newP); for(int i=1;i0) // { if(s.y>testY) return 1; else return -1; /* } else { if(s.y>testY) return -1; else return 1; }*/ } } 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); float ra=radians(angle); newP.x+=(cos(ra)*magnitude); newP.y-=(sin(ra)*magnitude); return newP; } //position displaceRK(position p,float angle,float magnitude) //{ //} float getHeading(position p1,position p2) //gets the absolute heading between one position and another relative to the first { if(p1.x==p2.x&&p1.y==p2.y) return 0; float xd=p2.x-p1.x; float yd=p2.y-p1.y; float angle=atan(yd/xd); // angle=degrees(angle); angle=angle*conversionArc; if(xd>0&&yd<0) { angle=-1*angle; } else if(xd<0&&yd<0) angle=180-angle; else if(xd<0&&yd>0) angle=180-angle; else if(xd<0&&yd==0) angle=180; else if(xd==0&&yd<0) angle=90; else angle=360-angle; return angle; } float dist(position p1,position p2) { return dist(p1.x,p1.y,p2.x,p2.y); } float normalizeHeading(float ang) //normalizes a heading between 0 and 360 { 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); 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,float s) //draws a vector with a longer line for representation in white { colorMode(RGB,255,255,255); stroke(255); noFill(); ellipseMode(CENTER); ellipse(v.p.x,v.p.y,5,5); vector temp=new vector(v); temp.m*=s; position e=temp.endPoint(); line(temp.p.x,temp.p.y,e.x,e.y); } void drawVector(vector v,int r,int g,int b,float s) //draws a vector with a longer line for representation in white { colorMode(RGB,255,255,255); stroke(r,g,b); noFill(); ellipseMode(CENTER); ellipse(v.p.x,v.p.y,5,5); vector temp=new vector(v); temp.m*=s; position e=temp.endPoint(); line(temp.p.x,temp.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); ellipse(v.p.x,v.p.y,5,5); position e=v.endPoint(); line(v.p.x,v.p.y,e.x,e.y); } boolean clIntersect(position c,float r,position s,position e) { r=r/2; float x0 = c.x; float y0 = c.y; float x1 = s.x; float y1 = s.y; float x2 = e.x; float y2 = e.y; float n = abs((x2-x1)*(y1-y0)-(x1-x0)*(y2-y1)); float d = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)); float dist = n/d; if(dist > r)return false; float d1 = sqrt((x0-x1)*(x0-x1)+(y0-y1)*(y0-y1)); if((d1-r) > d)return false; float d2 = sqrt((x0-x2)*(x0-x2)+(y0-y2)*(y0-y2)); if((d2-r) > d)return false; return true; }