Guest User

Untitled

a guest
Sep 15th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 1.86 KB | None | 0 0
  1. def L1 = [Y2:10, Y1:0, X2:10, X1:0]        
  2. def L2 = [Y2:10, Y1:0, X2:5, X1:10]        
  3.  
  4.        
  5. boolean linesCross(params){        
  6.          // Denominator for ua and ub are the same, so store this calculation
  7.          def L1 = params.L1
  8.          def L2 = params.L2
  9.          
  10.          //Groovy bastard thing..
  11.          def ptIntersection = new Expando()
  12.          
  13.          double d = ((L2.Y2 - L2.Y1) * (L1.X2 - L1.X1)) - ((L2.X2 - L2.X1) * (L1.Y2 - L1.Y1));
  14.  
  15.          //n_a and n_b are calculated as seperate values for readability
  16.          double n_a = ((L2.X2 - L2.X1) * (L1.Y1 - L2.Y1)) - ((L2.Y2 - L2.Y1) * (L1.X1 - L2.X1));
  17.  
  18.          double n_b = ((L1.X2 - L1.X1) * (L1.Y1 - L2.Y1)) - ((L1.Y2 - L1.Y1) * (L1.X1 - L2.X1));
  19.  
  20.          // Make sure there is not a division by zero - this also indicates that
  21.          // the lines are parallel.  
  22.          // If n_a and n_b were both equal to zero the lines would be on top of each
  23.          // other (coincidental).  This check is not done because it is not
  24.          // necessary for this implementation (the parallel check accounts for this).
  25.          if (d == 0)
  26.             return false;
  27.  
  28.          // Calculate the intermediate fractional point that the lines potentially intersect.
  29.          double ua = n_a / d;
  30.          double ub = n_b / d;
  31.  
  32.          // The fractional point will be between 0 and 1 inclusive if the lines
  33.          // intersect.  If the fractional calculation is larger than 1 or smaller
  34.          // than 0 the lines would need to be longer to intersect.
  35.          if (ua >= 0d && ua <= 1d && ub >= 0d && ub <= 1d)
  36.          {
  37.             ptIntersection.X = L1.X1 + (ua * (L1.X2 - L1.X1));
  38.             ptIntersection.Y = L1.Y1 + (ua * (L1.Y2 - L1.Y1));
  39.             println ptIntersection.Y
  40.             return true;
  41.          }
  42.          return false;
  43. }
  44.  
  45. println linesCross([L1:L1, L2:L2])
Add Comment
Please, Sign In to add comment