public static bool Intersects(Ray2LR a, Segment2LR b) { if (a.direction.IsZero) { return(b.Contains(a.origin)); } Vector2LR v = new Vector2LR(-a.Direction.Y, a.Direction.X); if (Vector2LR.DotProduct(b.Start - a.Origin, v).IsPositive == Vector2LR.DotProduct(b.End - a.Origin, v).IsPositive) { return(false); } if (b.Start == b.End) { return(a.Contains(b.Start)); } Vector2LR delta = b.GetDelta(); LongRational longRational1 = a.Direction.X * delta.Y - a.Direction.Y * delta.X; if (longRational1.IsZero) { return(false); } Vector2LR u = b.Start - a.Origin; LongRational longRational2 = Vector2LR.CrossProduct(u, delta); if (longRational1.IsPositive) { if (longRational2.IsNegative) { return(false); } } else if (longRational2.IsPositive) { return(false); } LongRational longRational3 = Vector2LR.CrossProduct(u, a.Direction); if (longRational1.IsPositive) { if (longRational3.IsNegative || longRational3 > longRational1) { return(false); } } else if (longRational3.IsPositive || longRational3 < longRational1) { return(false); } LongRational longRational4 = longRational3 / longRational1; LongRational longRational5 = longRational2 / longRational1; return(true); }
public static bool GetIntersectionCoefficients( Ray2LR a, Segment2LR b, out LongRational?p, out LongRational?q, out bool areParallel) { if (a.direction.IsZero) { areParallel = false; p = new LongRational?(); bool flag; if (flag = b.Contains(a.origin)) { Vector2LR delta = b.GetDelta(); q = new LongRational?(Vector2LR.DotProduct(a.origin - b.Start, delta) / delta.GetLengthSquared()); } else { q = new LongRational?(); } return(flag); } Vector2LR v = new Vector2LR(-a.direction.Y, a.direction.X); Vector2LR u = b.Start - a.Origin; LongRational longRational1 = Vector2LR.DotProduct(u, v); LongRational longRational2 = Vector2LR.DotProduct(b.End - a.Origin, v); if (longRational1.IsPositive && longRational2.IsPositive || longRational1.IsNegative && longRational2.IsNegative) { p = new LongRational?(); q = new LongRational?(); areParallel = false; return(false); } Vector2LR delta1 = b.GetDelta(); if (delta1.IsZero) { areParallel = false; p = new LongRational?(Vector2LR.DotProduct(u, a.direction) / a.direction.GetLengthSquared()); q = new LongRational?(LongRational.Zero); return(true); } LongRational longRational3 = a.Direction.X * delta1.Y - a.Direction.Y * delta1.X; if (longRational3.IsZero) { p = new LongRational?(); q = new LongRational?(); areParallel = true; return(true); } areParallel = false; LongRational longRational4 = Vector2LR.CrossProduct(u, delta1); if (longRational3.IsPositive) { if (longRational4.IsNegative) { p = new LongRational?(); q = new LongRational?(); return(false); } } else if (longRational4.IsPositive) { p = new LongRational?(); q = new LongRational?(); return(false); } LongRational longRational5 = Vector2LR.CrossProduct(u, a.Direction); if (longRational3.IsPositive) { if (longRational5.IsNegative || longRational5 > longRational3) { p = new LongRational?(); q = new LongRational?(); return(false); } } else if (longRational5.IsPositive || longRational5 < longRational3) { p = new LongRational?(); q = new LongRational?(); return(false); } p = new LongRational?(longRational4 / longRational3); q = new LongRational?(longRational5 / longRational3); return(true); }