public static bool Intersects(Ray2BR a, Segment2BR b) { if (a.direction.IsZero) { return(b.Contains(a.origin)); } Vector2BR v = new Vector2BR(-a.Direction.Y, a.Direction.X); if (Vector2BR.DotProduct(b.Start - a.Origin, v).IsPositive == Vector2BR.DotProduct(b.End - a.Origin, v).IsPositive) { return(false); } if (b.Start == b.End) { return(a.Contains(b.Start)); } Vector2BR delta = b.GetDelta(); BigRational bigRational1 = a.Direction.X * delta.Y - a.Direction.Y * delta.X; if (bigRational1.IsZero) { return(false); } Vector2BR u = b.Start - a.Origin; BigRational bigRational2 = Vector2BR.CrossProduct(u, delta); if (bigRational1.IsPositive) { if (bigRational2.IsNegative) { return(false); } } else if (bigRational2.IsPositive) { return(false); } BigRational bigRational3 = Vector2BR.CrossProduct(u, a.Direction); if (bigRational1.IsPositive) { if (bigRational3.IsNegative || bigRational3 > bigRational1) { return(false); } } else if (bigRational3.IsPositive || bigRational3 < bigRational1) { return(false); } BigRational bigRational4 = bigRational3 / bigRational1; BigRational bigRational5 = bigRational2 / bigRational1; return(true); }
public static bool GetIntersectionCoefficients( Ray2BR a, Segment2BR b, out BigRational?p, out BigRational?q, out bool areParallel) { if (a.direction.IsZero) { areParallel = false; p = new BigRational?(); bool flag; if (flag = b.Contains(a.origin)) { Vector2BR delta = b.GetDelta(); q = new BigRational?(Vector2BR.DotProduct(a.origin - b.Start, delta) / delta.GetLengthSquared()); } else { q = new BigRational?(); } return(flag); } Vector2BR v = new Vector2BR(-a.direction.Y, a.direction.X); Vector2BR u = b.Start - a.Origin; BigRational bigRational1 = Vector2BR.DotProduct(u, v); BigRational bigRational2 = Vector2BR.DotProduct(b.End - a.Origin, v); if (bigRational1.IsPositive && bigRational2.IsPositive || bigRational1.IsNegative && bigRational2.IsNegative) { p = new BigRational?(); q = new BigRational?(); areParallel = false; return(false); } Vector2BR delta1 = b.GetDelta(); if (delta1.IsZero) { areParallel = false; p = new BigRational?(Vector2BR.DotProduct(u, a.direction) / a.direction.GetLengthSquared()); q = new BigRational?(BigRational.Zero); return(true); } BigRational bigRational3 = a.Direction.X * delta1.Y - a.Direction.Y * delta1.X; if (bigRational3.IsZero) { p = new BigRational?(); q = new BigRational?(); areParallel = true; return(true); } areParallel = false; BigRational bigRational4 = Vector2BR.CrossProduct(u, delta1); if (bigRational3.IsPositive) { if (bigRational4.IsNegative) { p = new BigRational?(); q = new BigRational?(); return(false); } } else if (bigRational4.IsPositive) { p = new BigRational?(); q = new BigRational?(); return(false); } BigRational bigRational5 = Vector2BR.CrossProduct(u, a.Direction); if (bigRational3.IsPositive) { if (bigRational5.IsNegative || bigRational5 > bigRational3) { p = new BigRational?(); q = new BigRational?(); return(false); } } else if (bigRational5.IsPositive || bigRational5 < bigRational3) { p = new BigRational?(); q = new BigRational?(); return(false); } p = new BigRational?(bigRational4 / bigRational3); q = new BigRational?(bigRational5 / bigRational3); return(true); }
public static bool GetIntersectionParameters( Segment2BR a, Segment2BR b, out BigRational[] pArray, out BigRational[] qArray) { Vector2BR delta1 = a.GetDelta(); Vector2BR delta2 = b.GetDelta(); BigRational bigRational1 = delta1.X * delta2.Y - delta1.Y * delta2.X; if (bigRational1.IsZero) { Segment2BR.Struct6 struct6_1 = new Segment2BR.Struct6(a.point2BR_0, a.point2BR_1, delta1); Segment2BR.Struct6 struct6_2 = new Segment2BR.Struct6(b.point2BR_0, b.point2BR_1, delta2); if ((struct6_1.bigRational_0 > struct6_2.bigRational_0 ? (!struct6_1.method_0(b.point2BR_0) ? 0 : (struct6_1.method_0(b.End) ? 1 : 0)) : (struct6_2.bigRational_0.IsZero || !struct6_2.method_0(a.point2BR_0) ? 0 : (struct6_2.method_0(a.point2BR_1) ? 1 : 0))) != 0) { if (struct6_1.method_1(b.point2BR_0)) { if (struct6_1.method_1(b.point2BR_1)) { pArray = new BigRational[2] { a.GetNormalizedProjection(b.point2BR_0), a.GetNormalizedProjection(b.point2BR_1) }; qArray = new BigRational[2] { BigRational.Zero, BigRational.One }; return(true); } if (struct6_2.method_1(a.point2BR_0)) { pArray = new BigRational[2] { BigRational.Zero, a.GetNormalizedProjection(b.point2BR_0) }; qArray = new BigRational[2] { b.GetNormalizedProjection(a.point2BR_0), BigRational.Zero }; return(true); } if (struct6_2.method_1(a.End)) { pArray = new BigRational[2] { a.GetNormalizedProjection(b.point2BR_0), BigRational.One }; qArray = new BigRational[2] { BigRational.Zero, b.GetNormalizedProjection(a.point2BR_1) }; return(true); } } else if (struct6_1.method_1(b.point2BR_1)) { if (struct6_2.method_1(a.point2BR_0)) { if (struct6_2.method_1(a.End)) { pArray = new BigRational[2] { BigRational.Zero, BigRational.One }; qArray = new BigRational[2] { b.GetNormalizedProjection(a.point2BR_0), b.GetNormalizedProjection(a.point2BR_1) }; } else { pArray = new BigRational[2] { BigRational.Zero, a.GetNormalizedProjection(b.point2BR_1) }; qArray = new BigRational[2] { b.GetNormalizedProjection(a.point2BR_0), BigRational.One }; } return(true); } if (struct6_2.method_1(a.End)) { pArray = new BigRational[2] { BigRational.One, a.GetNormalizedProjection(b.point2BR_1) }; qArray = new BigRational[2] { b.GetNormalizedProjection(a.point2BR_1), BigRational.One }; return(true); } } else if (struct6_2.method_1(a.point2BR_0)) { pArray = new BigRational[2] { BigRational.Zero, BigRational.One }; qArray = new BigRational[2] { b.GetNormalizedProjection(a.point2BR_0), b.GetNormalizedProjection(a.point2BR_1) }; return(true); } } pArray = (BigRational[])null; qArray = (BigRational[])null; return(false); } BigRational bigRational2 = (b.Start.X - a.Start.X) * delta2.Y - (b.Start.Y - a.Start.Y) * delta2.X; if (bigRational1.IsPositive) { if (bigRational2.IsNegative || bigRational2 > bigRational1) { pArray = (BigRational[])null; qArray = (BigRational[])null; return(false); } } else if (bigRational2.IsPositive || bigRational2 < bigRational1) { pArray = (BigRational[])null; qArray = (BigRational[])null; return(false); } BigRational bigRational3 = (b.Start.X - a.Start.X) * delta1.Y - (b.Start.Y - a.Start.Y) * delta1.X; if (bigRational1.IsPositive) { if (bigRational3.IsNegative || bigRational3 > bigRational1) { pArray = (BigRational[])null; qArray = (BigRational[])null; return(false); } } else if (bigRational3.IsPositive || bigRational3 < bigRational1) { pArray = (BigRational[])null; qArray = (BigRational[])null; return(false); } BigRational bigRational4 = bigRational2 / bigRational1; BigRational bigRational5 = bigRational3 / bigRational1; pArray = new BigRational[1] { bigRational4 }; qArray = new BigRational[1] { bigRational5 }; return(true); }