Advertisement
Guest User

Untitled

a guest
Jul 18th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*      Script by Xot <> Modified by Link3000
  2. **  Usage:
  3. **      t = line_intersect(x1,y1,x2,y2,x3,y3,x4,y4,segment);
  4. **      x = xx; <- location of the intersection
  5. **      y = yy;
  6. **
  7. **  Arguments:
  8. **      x1,y1,x2,y2     Coordinates defining first line segment
  9. **      x3,y3,x4,y4     Coordinates defining second line segment
  10. **      segment         If TRUE, intersection test is confined to the given line segments
  11. **                      If FALSE, intersection may occur at any point on the given lines
  12. **
  13. **  Returns:
  14. **      The vector multiplier (t) from the parametric form of the first line.
  15. **      A value of 0 < t <= 1 indicates an intersection within the first line segment.
  16. **      A value of 0 indicates no intersection, other values indicate a possible
  17. **      intersection beyond the endpoints of the first line segment.
  18. **
  19. **  Notes:
  20. **      By substituting the return value (t) into the parametric form of the first line,
  21. **      you can determine the point of intersection, eg. x = x1+t*(x2-x1)
  22. **
  23. **  GMLscripts.com
  24. */
  25. {
  26.     var x1,y1,x2,y2,x3,y3,x4,y4,segment,ud,ua,ub;
  27.     x1 = argument0;
  28.     y1 = argument1;
  29.     x2 = argument2;
  30.     y2 = argument3;
  31.     x3 = argument4;
  32.     y3 = argument5;
  33.     x4 = argument6;
  34.     y4 = argument7;
  35.     segment = argument8;
  36.     ua = 0;
  37.     ud = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);
  38.     if (ud != 0) {
  39.         ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / ud;
  40.         if (segment) {
  41.             ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / ud;
  42.             if (ua < 0 || ua > 1 || ub < 0 || ub > 1) ua = 0;
  43.         }
  44.     }
  45.     xx = x1+ua*(x2-x1)
  46.     yy = y1+ua*(y2-y1)
  47.     return ua;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement