class BVector extends PVector { BVector(float x, float y, float z) { super(x, y, z); } BVector(float x, float y) { super(x, y); } void rotZ(float theta) { float new_x = this.x*cos(theta) - this.y*sin(theta); float new_y = this.x*sin(theta) + this.y*cos(theta); this.set(new_x,new_y,this.z); } void rotX(float theta) { float new_y = this.y*cos(theta) - this.z*sin(theta); float new_z = this.y*sin(theta) + this.z*cos(theta); this.set(this.x,new_y,new_z); } void rotY(float theta) { float new_z = this.z*cos(theta) - this.x*sin(theta); float new_x = this.z*sin(theta) + this.x*cos(theta); this.set(new_x,this.y,new_z); } } PVector intersect(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) { float bx = x2 - x1; float by = y2 - y1; float dx = x4 - x3; float dy = y4 - y3; float b_dot_d_perp = bx * dy - by * dx; if(b_dot_d_perp == 0) { return null; } float cx = x3 - x1; float cy = y3 - y1; float t = (cx * dy - cy * dx) / b_dot_d_perp; if(t <= 0.0001 || t >= 0.9999) { return null; } float u = (cx * by - cy * bx) / b_dot_d_perp; if(u <= 0.0001 || u >= 0.9999) { return null; } return new PVector(x1+t*bx, y1+t*by); } PVector closest(float x1, float y1, float x2, float y2, float x3, float y3) { float u; u = (x3 - x1)*(x2 - x1) + (y3 - y1)*(y2 - y1); // This is all derived from the dot product of the line endpoint vectors and the tested point being 0 u = u/pow(dist(x1,y1,x2,y2),2); float x = x1 + u*(x2-x1); float y = y1 + u*(y2-y1); if ((u < 1) && (u > 0)) { return new PVector(x,y); } else { return null; } }