vec origin=new vec(); //origin is 0,0 class vec { float x; float y; vec() {//constructor with no arguments, gives you origin x=0; y=0; } vec(float x,float y) {//constructor with 2 arguments x and y this.x=x; this.y=y; } vec(vec s) {//constructor with vec as input. basically an assignment operator if(s==null) return; this.x=s.x; this.y=s.y; } vec(vec s,vec 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; } vec(vec s,vec 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; } 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) { vec temp=new vec(); temp.disp(ang()+ang,mag()); x=temp.x; y=temp.y; } void rotate(vec v,float ang) { vec cen=new vec(v); vec ori=new vec(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,origin); } float mag() {//returns the distance between this vector and origin return dist(origin,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; } //vector operators void add(vec s) {//addition operator x+=s.x; y+=s.y; } void sub(vec s) {//subtraction operator x-=s.x; y-=s.y; } void mul(vec s) {//multiplication operator x*=s.x; y*=s.y; } void div(vec s) {//division operator. returns 0 when division by zero if(s.x==0||s.y==0) return; x/=s.x; y/=s.y; } } vec newVec(vec v,float ang,float magnitude) { vec temp=new vec(v); temp.disp(ang,magnitude); return temp; } vec newVec(float ang,float magnitude) { ang=radians(ang); float tx=cos(ang)*magnitude; float ty=sin(ang)*magnitude*-1; vec temp=new vec(tx,ty); return temp; } vec nv(float x,float y) { return new vec(x,y); } vec midPoint(vec a,vec b) { vec d=new vec(a,b); d.mul(.5); vec dest=new vec(a); dest.add(d); // d.sub(b); // vec newVec=new vec(a); // newVec.add(d); return dest; } vec avg(vec v[]) { vec total=new vec(); for(int i=0;ia.y) return ang=360+ang; if(b.y==a.y&&b.x>a.x) return ang=0; if(b.y==a.y&&b.xwidth+buff) v=nv(width/2,height/2); if(v.y<0-buff) v=nv(width/2,height/2); if(v.y>height+buff) v=nv(width/2,height/2); return v; } vec constrain(vec v,float buff) { vec temp=new vec(v); temp.x=constrain(temp.x,-buff,width+buff); temp.y=constrain(temp.y,-buff,height+buff); return temp; } vec[] append(vec v[],vec nv) { vec temp[]=new vec[v.length+1]; System.arraycopy(v,0,temp,0,v.length); temp[v.length]=new vec(nv); return temp; } class seg { vec a; vec b; seg() { a=new vec(); b=new vec(); } seg(vec a,vec b) { this.a=new vec(a); this.b=new vec(b); } float mag() { return dist(a,b); } } //Sedgwick's Line Intersection algorithm int CCW(vec p1, vec p2, vec p3) { float dx1, dx2, dy1, dy2; dx1 = p2.x - p1.x; dy1 = p2.y - p1.y; dx2 = p3.x - p1.x; dy2 = p3.y - p1.y; if (dx1*dy2 > dy1*dx2) return +1; if (dx1*dy2 < dy1*dx2) return -1; if ((dx1*dx2 < 0) || (dy1*dy2 < 0)) return -1; if ((dx1*dx1+dy1*dy1) < (dx2*dx2+dy2*dy2)) return +1; return 0; } boolean intersect(vec a1, vec a2, vec b1, vec b2) { return ((CCW(a1, a2, b1) != CCW(a1, a2, b2)) && (CCW(b1, b2, a1) != CCW(b1, b2, a2))); } boolean intersect(seg a,seg b) { if(a==null||b==null) return false; vec a1=a.a; vec a2=a.b; vec b1=b.a; vec b2=b.b; return intersect(a1,a2,b1,b2); } vec intersectPoint(seg a,seg b) { vec n=new vec(); float x1=a.a.x; float x2=a.b.x; float x3=b.a.x; float x4=b.b.x; float y1=a.a.y; float y2=a.b.y; float y3=b.a.y; float y4=b.b.y; float nd=((y4-y3)*(x2-x1))-((x4-x3)*(y2-y1)); if(nd==0) return a.a; float mnx=(((x4-x3)*(y1-y3))-((y4-y3)*(x1-x3))) / nd; // float mny=(((x2-x1)*(y1-y3))-((y2-y1)*(x1-x3))) / nd; float nx=x1+mnx*(x2-x1); float ny=y1+mnx*(y2-y1); n=new vec(nx,ny); return n; } vec intersectPoint2(seg a,seg b) { vec n=new vec(); float A1=a.a.x-a.b.x; float B1=a.a.y-a.b.y; float C1=A1*a.a.x+B1*a.a.y; float A2=b.a.x-b.b.x; float B2=b.a.y-b.b.y; float C2=A2*b.a.x+B2*b.a.y; float det=(A1*B2)-(A2*B1); if(det==0) { return new vec(); } else { n.x=((B2*C1 - B1*C2)/det); n.y=((A1*C2 - A2*C1)/det); } return n; } float normalize(float a) { if(a>360) return a%360; if(a<0) return 360+(a%360); return a; } float wrap(float n,float mini,float maxi) { n=n%maxi; if(nmaxi) return mini+(n-maxi); return n; }