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 bool Intersects(Ray2LR other, out Point2LR?intersection, out bool parallel) { if (this.direction.IsZero) { parallel = false; bool flag; if (flag = other.Contains(this.origin)) { intersection = new Point2LR?(this.origin); } else { intersection = new Point2LR?(); } return(flag); } if (other.Direction.IsZero) { parallel = false; bool flag; if (flag = this.Contains(other.origin)) { intersection = new Point2LR?(other.origin); } else { intersection = new Point2LR?(); } return(flag); } LongRational longRational1 = this.direction.X * other.direction.Y - this.direction.Y * other.direction.X; if (longRational1.IsZero) { parallel = true; bool flag; if (flag = other.Contains(this.origin)) { intersection = new Point2LR?(this.origin); } else { intersection = new Point2LR?(); } return(flag); } parallel = false; Vector2LR u = other.origin - this.origin; LongRational longRational2 = Vector2LR.CrossProduct(u, other.direction); if (longRational1.IsPositive) { if (longRational2.IsNegative) { intersection = new Point2LR?(); return(false); } } else if (longRational2.IsPositive) { intersection = new Point2LR?(); return(false); } LongRational longRational3 = Vector2LR.CrossProduct(u, this.direction); if (longRational1.IsPositive) { if (longRational3.IsNegative) { intersection = new Point2LR?(); return(false); } } else if (longRational3.IsPositive) { intersection = new Point2LR?(); return(false); } LongRational longRational4 = longRational2 / longRational1; intersection = new Point2LR?(this.origin + longRational4 * this.direction); 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); }
public static bool GetIntersectionCoefficients( Ray2LR a, Ray2LR b, out LongRational?p, out LongRational?q, out bool parallel) { if (a.direction.IsZero) { if (b.direction.IsZero) { bool flag = a.origin == b.origin; parallel = false; if (flag) { p = new LongRational?(LongRational.Zero); q = new LongRational?(LongRational.Zero); } else { p = new LongRational?(); q = new LongRational?(); } return(flag); } parallel = false; p = new LongRational?(); bool flag1; if (flag1 = b.Contains(a.origin)) { q = new LongRational?(Vector2LR.DotProduct(a.origin - b.origin, b.direction) / b.direction.GetLengthSquared()); } else { q = new LongRational?(); } return(flag1); } if (b.Direction.IsZero) { parallel = false; q = new LongRational?(); bool flag; if (flag = a.Contains(b.origin)) { p = new LongRational?(Vector2LR.DotProduct(b.origin - a.origin, a.direction) / a.direction.GetLengthSquared()); } else { p = new LongRational?(); } return(flag); } LongRational longRational1 = a.Direction.X * b.Direction.Y - a.Direction.Y * b.Direction.X; if (longRational1.IsZero) { parallel = true; if (!a.Contains(b.origin)) { b.Contains(a.origin); } p = new LongRational?(); q = new LongRational?(); return(true); } parallel = false; Vector2LR u = b.origin - a.origin; LongRational longRational2 = Vector2LR.CrossProduct(u, b.direction); if (longRational1.IsPositive) { if (longRational2.IsNegative) { p = new LongRational?(); q = new LongRational?(); return(false); } } else if (longRational2.IsPositive) { p = new LongRational?(); q = new LongRational?(); return(false); } LongRational longRational3 = Vector2LR.CrossProduct(u, a.direction); if (longRational1.IsPositive) { if (longRational3.IsNegative) { p = new LongRational?(); q = new LongRational?(); return(false); } } else if (longRational3.IsPositive) { p = new LongRational?(); q = new LongRational?(); return(false); } p = new LongRational?(longRational2 / longRational1); q = new LongRational?(longRational3 / longRational1); return(true); }