#include "line.h" // Returns 1 lines cross & 0 if not. // Parallel lines won't be detected, even if they share // the same coordinates int LineCollision(Line a, Line b) { long swapTemp; long ay, by, cy, dy; // Ensure coords are defined in correct order if (a.sx > a.ex) { swapTemp=a.sx; a.sx=a.ex; a.ex=swapTemp; swapTemp=a.sy; a.sy=a.ey; a.ey=swapTemp; } if (b.sx > b.ex) { swapTemp=b.sx; b.sx=b.ex; b.ex=swapTemp; swapTemp=b.sy; b.sy=b.ey; b.ey=swapTemp; } // Clip lines if (a.sx < b.sx) { if (a.ex <= b.sx) return 0; ay = ((a.ey-a.sy)*(b.sx-a.sx)) / (a.ex-a.sx) + a.sy; cy = b.sy; if (b.ex < a.ex) { by = ((a.ey-a.sy)*(b.ex-a.sx)) / (a.ex-a.sx) + a.sy; dy = b.ey; } else { by = a.ey; dy = ((b.ey-b.sy)*(a.ex-b.sx)) / (b.ex-b.sx) + b.sy; } } else { if (b.ex <= a.sx) return 0; ay = a.sy; cy = ((b.ey-b.sy)*(a.sx-b.sx)) / (b.ex-b.sx) + b.sy; if (a.ex < b.ex) { by = a.ey; dy = ((b.ey-b.sy)*(a.ex-b.sx)) / (b.ex-b.sx) + b.sy; } else { by = ((a.ey-a.sy)*(b.ex-a.sx)) / (a.ex-a.sx) + a.sy; dy = b.ey; } } // Check that they cross return (Sgn(cy-ay) != Sgn(dy-by)); }