Advertisement
gmlscripts

lines_intersect

Jun 26th, 2014
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// lines_intersect(x1,y1,x2,y2,x3,y3,x4,y4,segment)
  2. //
  3. //  Returns a vector multiplier (t) for the first line segment.
  4. //  A value of (0 < t <= 1) indicates an intersection within the
  5. //  first line segment, a value of 0 indicates no intersection,
  6. //  other values indicate an intersection beyond the endpoints.
  7. //
  8. //      x1,y1,x2,y2     1st line segment
  9. //      x3,y3,x4,y4     2nd line segment
  10. //      segment         If true, confine the test to the line segments.
  11. //
  12. //  By substituting the return value (t) into the parametric form
  13. //  of the first line, the point of intersection can be determined.
  14. //  eg. x = x1 + t * (x2 - x1)
  15. //      y = y1 + t * (y2 - y1)
  16. //
  17. /// GMLscripts.com/license
  18. {
  19.     var ua, ub, ud, ux, uy, vx, vy, wx, wy;
  20.     ua = 0;
  21.     ux = argument2 - argument0;
  22.     uy = argument3 - argument1;
  23.     vx = argument6 - argument4;
  24.     vy = argument7 - argument5;
  25.     wx = argument0 - argument4;
  26.     wy = argument1 - argument5;
  27.     ud = vy * ux - vx * uy;
  28.     if (ud != 0)
  29.     {
  30.         ua = (vx * wy - vy * wx) / ud;
  31.         if (argument8)
  32.         {
  33.             ub = (ux * wy - uy * wx) / ud;
  34.             if (ua < 0 || ua > 1 || ub < 0 || ub > 1) ua = 0;
  35.         }
  36.     }
  37.     return ua;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement