Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def L1 = [Y2:10, Y1:0, X2:10, X1:0]
- def L2 = [Y2:10, Y1:0, X2:5, X1:10]
- boolean linesCross(params){
- // Denominator for ua and ub are the same, so store this calculation
- def L1 = params.L1
- def L2 = params.L2
- //Groovy bastard thing..
- def ptIntersection = new Expando()
- double d = ((L2.Y2 - L2.Y1) * (L1.X2 - L1.X1)) - ((L2.X2 - L2.X1) * (L1.Y2 - L1.Y1));
- //n_a and n_b are calculated as seperate values for readability
- double n_a = ((L2.X2 - L2.X1) * (L1.Y1 - L2.Y1)) - ((L2.Y2 - L2.Y1) * (L1.X1 - L2.X1));
- double n_b = ((L1.X2 - L1.X1) * (L1.Y1 - L2.Y1)) - ((L1.Y2 - L1.Y1) * (L1.X1 - L2.X1));
- // Make sure there is not a division by zero - this also indicates that
- // the lines are parallel.
- // If n_a and n_b were both equal to zero the lines would be on top of each
- // other (coincidental). This check is not done because it is not
- // necessary for this implementation (the parallel check accounts for this).
- if (d == 0)
- return false;
- // Calculate the intermediate fractional point that the lines potentially intersect.
- double ua = n_a / d;
- double ub = n_b / d;
- // The fractional point will be between 0 and 1 inclusive if the lines
- // intersect. If the fractional calculation is larger than 1 or smaller
- // than 0 the lines would need to be longer to intersect.
- if (ua >= 0d && ua <= 1d && ub >= 0d && ub <= 1d)
- {
- ptIntersection.X = L1.X1 + (ua * (L1.X2 - L1.X1));
- ptIntersection.Y = L1.Y1 + (ua * (L1.Y2 - L1.Y1));
- println ptIntersection.Y
- return true;
- }
- return false;
- }
- println linesCross([L1:L1, L2:L2])
Add Comment
Please, Sign In to add comment