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 Intersects(Segment2BR segment1, Segment2BR segment2) { if (segment1.point2BR_0 == segment1.point2BR_1) { return(segment2.Contains(segment1.point2BR_0)); } Vector2BR v1 = segment1.point2BR_1 - segment1.point2BR_0; Vector2BR u1 = segment2.Start - segment1.point2BR_0; Vector2BR u2 = segment2.End - segment1.point2BR_0; Vector2BR v2 = new Vector2BR(-v1.Y, v1.X); BigRational bigRational1 = Vector2BR.DotProduct(u1, v2); BigRational bigRational2 = Vector2BR.DotProduct(u2, v2); if (bigRational1.IsPositive && bigRational2.IsPositive || bigRational1.IsNegative && bigRational2.IsNegative) { return(false); } BigRational a = Vector2BR.DotProduct(u1, v1); BigRational b = Vector2BR.DotProduct(u2, v1); BigRational bigRational3 = b - a; if (bigRational3.IsZero) { BigRational bigRational4 = a; if (bigRational4.IsNegative) { return(false); } return(bigRational4.Square() <= v1.GetLengthSquared()); } BigRational bigRational5 = bigRational2 - bigRational1; if (bigRational5.IsZero) { if (BigMath.Max(a, b).IsNegative) { return(false); } return(BigMath.Min(a, b).Square() <= v1.GetLengthSquared()); } BigRational bigRational6 = bigRational3 / bigRational5; BigRational bigRational7 = a - bigRational1 * bigRational6; if (bigRational7.IsNegative) { return(false); } return(bigRational7.Square() < v1.GetLengthSquared()); }
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); }