Advertisement
Guest User

line-line intersection

a guest
Jan 25th, 2013
853
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* lines_intersect:  AUTHOR: Mukesh Prasad
  2. *
  3. *   This function computes whether two line segments,
  4. *   respectively joining the input points (x1,y1) -- (x2,y2)
  5. *   and the input points (x3,y3) -- (x4,y4) intersect.
  6. *   If the lines intersect, the output variables x, y are
  7. *   set to coordinates of the point of intersection.
  8. *
  9. *   All values are in integers.  The returned value is rounded
  10. *   to the nearest integer point.
  11. *
  12. *   If non-integral grid points are relevant, the function
  13. *   can easily be transformed by substituting floating point
  14. *   calculations instead of integer calculations.
  15. *
  16. *   Entry
  17. *        x1, y1,  x2, y2   Coordinates of endpoints of one segment.
  18. *        x3, y3,  x4, y4   Coordinates of endpoints of other segment.
  19. *
  20. *   Exit
  21. *        x, y              Coordinates of intersection point.
  22. *
  23. *   The value returned by the function is one of:
  24. *
  25. *        DONT_INTERSECT    0
  26. *        DO_INTERSECT      1
  27. *        COLLINEAR         2
  28. *
  29. * Error conditions:
  30. *
  31. *     Depending upon the possible ranges, and particularly on 16-bit
  32. *     computers, care should be taken to protect from overflow.
  33. *
  34. *     In the following code, 'long' values have been used for this
  35. *     purpose, instead of 'int'.
  36. *
  37. */
  38.  
  39. package  {
  40.     import flash.geom.Point;
  41.     public class xlines {
  42.         static public const DONT_INTERSECT:int = 0;
  43.         static public const DO_INTERSECT:int = 1;
  44.         static public const COLLINEAR:int = 2;
  45.         static public function lines_intersect( x1:Number, y1:Number,   /* First line segment */
  46.                                                 x2:Number, y2:Number,
  47.                                                 x3:Number, y3:Number,   /* Second line segment */
  48.                                                 x4:Number, y4:Number,
  49.                                                 out:Point   /* Output value: */
  50.                                                 ):int {     /* point of intersection */
  51.             /* Coefficients of line eqns. */
  52.             var a1:Number, a2:Number, b1:Number, b2:Number, c1:Number, c2:Number;
  53.             /* 'Sign' values */
  54.             var r1:Number, r2:Number, r3:Number, r4:Number;
  55.             /* Intermediate values */
  56.             var denom:Number, offset:Number, num:Number;
  57.  
  58.             /* Compute a1, b1, c1, where line joining points 1 and 2
  59.              * is "a1 x  +  b1 y  +  c1  =  0".
  60.              */
  61.  
  62.             a1 = y2 - y1;
  63.             b1 = x1 - x2;
  64.             c1 = x2 * y1 - x1 * y2;
  65.  
  66.             /* Compute r3 and r4.
  67.              */
  68.  
  69.  
  70.             r3 = a1 * x3 + b1 * y3 + c1;
  71.             r4 = a1 * x4 + b1 * y4 + c1;
  72.  
  73.             /* Check signs of r3 and r4.  If both point 3 and point 4 lie on
  74.              * same side of line 1, the line segments do not intersect.
  75.              */
  76.  
  77.             if ( r3 != 0 &&
  78.                  r4 != 0 &&
  79.                  SAME_SIGNS( r3, r4 ))
  80.                 return ( DONT_INTERSECT );
  81.  
  82.             /* Compute a2, b2, c2 */
  83.  
  84.             a2 = y4 - y3;
  85.             b2 = x3 - x4;
  86.             c2 = x4 * y3 - x3 * y4;
  87.  
  88.             /* Compute r1 and r2 */
  89.  
  90.             r1 = a2 * x1 + b2 * y1 + c2;
  91.             r2 = a2 * x2 + b2 * y2 + c2;
  92.  
  93.             /* Check signs of r1 and r2.  If both point 1 and point 2 lie
  94.              * on same side of second line segment, the line segments do
  95.              * not intersect.
  96.              */
  97.  
  98.             if ( r1 != 0 &&
  99.                  r2 != 0 &&
  100.                  SAME_SIGNS( r1, r2 ))
  101.                 return ( DONT_INTERSECT );
  102.  
  103.             /* Line segments intersect: compute intersection point.
  104.              */
  105.  
  106.             denom = a1 * b2 - a2 * b1;
  107.             if ( denom == 0 )
  108.                 return ( COLLINEAR );
  109.             offset = denom < 0 ? - denom / 2 : denom / 2;
  110.  
  111.             /* The denom/2 is to get rounding instead of truncating.  It
  112.              * is added or subtracted to the numerator, depending upon the
  113.              * sign of the numerator.
  114.              */
  115.  
  116.             num = b1 * c2 - b2 * c1;
  117.             out.x = ( num < 0 ? num - offset : num + offset ) / denom;
  118.  
  119.             num = a2 * c1 - a1 * c2;
  120.             out.y = ( num < 0 ? num - offset : num + offset ) / denom;
  121.  
  122.             return ( DO_INTERSECT );
  123.         } /* lines_intersect */
  124.        
  125.         static private function SAME_SIGNS(x:Number, y:Number):Boolean {
  126.             return (x < 0 ? -1 : 1) == (y < 0 ? -1 : 1);
  127.         }
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement