01 Bool InRange(F64 low,F64 x,F64 high) { 02 if(low<high) 03 return low-.01<=x<=high+.01; 04 return high-.01<=x<=low+.01; 05 } 06 07 Bool PlaneIntersect(CD2 *dst,CD2 *a,CD2 *b,CD2 *a2,CD2 *b2,Bool check=TRUE) { 08 /* 09 * Nroot here,Heres the deal,I make a system of 2 linear equations and solve for an intersect 10 * I solve for the intersect and check if they are in bounds of points 11 12 Check for intersection basically. 13 14 / 15 ----*------ 16 / 17 / 18 / 19 */ 20 F64 slope1,slope2,off1,off2,x,x2,y,y2; 21 F64 tolerance=.0001; 22 CD2 dummy; 23 if(!dst) dst=&dummy; 24 //If the line points straight up,we can check to see if the other 25 //line goes through the y position,if so,it is a hit 26 if(Abs(b->x-a->x)<tolerance||Abs(b2->x-a2->x)<tolerance) { 27 if(ToI64(a->x)==ToI64(b->x)&&ToI64(a2->x)==ToI64(b2->x)) { 28 if(ToI64(a->x)==ToI64(a2->x)) { 29 dst->x=a->x; 30 dst->y=a->y; 31 goto fin; 32 } else 33 return FALSE; 34 } 35 if(ToI64(a->x)==ToI64(b->x)) { 36 dst->x=a->x; 37 slope2=(b2->y-a2->y)/(b2->x-a2->x); 38 dst->y=slope2*(dst->x-a2->x)+a2->y; 39 goto fin; 40 } 41 if(ToI64(a2->x)==ToI64(b2->x)) { 42 dst->x=a2->x; 43 slope1=(b->y-a->y)/(b->x-a->x); 44 dst->y=slope1*(dst->x-a->x)+a->y; 45 goto fin; 46 } 47 goto fin; 48 } 49 //https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection 50 slope2=(b2->y-a2->y)/(b2->x-a2->x); 51 slope1=(b->y-a->y)/(b->x-a->x); 52 off1=slope1*-a->x+a->y; 53 off2=slope2*-a2->x+a2->y; 54 if(slope1==slope2) return FALSE; 55 dst->x=((off2-off1)/(slope1-slope2)); 56 dst->y=slope1*(dst->x)+off1; 57 fin: 58 if(!check) return TRUE; 59 if(a->x>b->x) 60 x=b->x-tolerance,x2=a->x+tolerance; 61 else 62 x=a->x-tolerance,x2=b->x+tolerance; 63 if(a->y>b->y) 64 y=b->y-tolerance,y2=a->y+tolerance; 65 else 66 y=a->y-tolerance,y2=b->y+tolerance; 67 68 if(!(x<=dst->x<=x2&&y<=dst->y<=y2)) 69 return FALSE; 70 71 if(a2->x>b2->x) 72 x=b2->x-tolerance,x2=a2->x+tolerance; 73 else 74 x=a2->x-tolerance,x2=b2->x+tolerance; 75 if(a2->y>b2->y) 76 y=b2->y-tolerance,y2=a2->y+tolerance; 77 else 78 y=a2->y-tolerance,y2=b2->y+tolerance; 79 if(!(x<=dst->x<=x2&&y<=dst->y<=y2)) 80 return FALSE; 81 82 return TRUE; 83 }