/// <summary> /// Initializes a new instance of the <see cref="Ray"/> class using given ray. /// </summary> /// <param name="ray">A <see cref="Ray"/> instance to assign values from.</param> public Ray(Ray ray) { _origin = ray.Origin; _direction = ray.Direction; }
/// <summary> /// Calculates the distance between two rays. /// </summary> /// <param name="r0">A <see cref="Ray"/> instance.</param> /// <param name="r1">A <see cref="Ray"/> instance.</param> /// <returns>Returns the distance between two rays.</returns> public static float Distance(Ray r0, Ray r1) { throw new NotImplementedException(); }
/// <summary> /// Calculates the squared distance between a given point and a given ray. /// </summary> /// <param name="point">A <see cref="Vector2D"/> instance.</param> /// <param name="ray">A <see cref="Ray"/> instance.</param> /// <returns>The squared distance between the point and the ray.</returns> public static double SquaredDistance(Vector2D point, Ray ray) { Vector2D diff = point - ray.Origin; double t = Vector2D.DotProduct(diff, ray.Direction); if (t <= 0.0f) { return diff.GetLengthSquared(); } else { t = (t * t) / ray.Direction.GetLengthSquared(); return diff.GetLengthSquared() - t; } }
/// <summary> /// Calculates the distance between a given point and a given ray. /// </summary> /// <param name="point">A <see cref="Vector2D"/> instance.</param> /// <param name="ray">A <see cref="Ray"/> instance.</param> /// <returns>The distance between the point and the ray.</returns> public static float Distance(Vector2D point, Ray ray) { return (float)System.Math.Sqrt(SquaredDistance(point, ray)); }
public static bool Intersect(Segment seg, Ray ray, out Intersection2D interObj) { // (Ay-Cy)(Dx-Cx)-(Ax-Cx)(Dy-Cy) //r = ----------------------------- (eqn 1) // (Bx-Ax)(Dy-Cy)-(By-Ay)(Dx-Cx) // (Ay-Cy)(Bx-Ax)-(Ax-Cx)(By-Ay) //s = ----------------------------- (eqn 2) // (Bx-Ax)(Dy-Cy)-(By-Ay)(Dx-Cx) double den = (seg.P1.X - seg.P0.X) * ray.Direction.Y - (seg.P1.Y - seg.P0.Y) * ray.Direction.X; double r = (seg.P0.Y - ray.Origin.Y) * ((ray.Origin + ray.Direction).X - ray.Origin.X) - (seg.P0.X - ray.Origin.X) * ((ray.Origin + ray.Direction).Y - ray.Origin.Y); // If the denominator in eqn 1 is zero, AB & CD are parallel if (System.Math.Abs(den) < MathFunctions.EpsilonF) { // If the numerator in eqn 1 is also zero, AB & CD are collinear. if (System.Math.Abs(r) < MathFunctions.EpsilonF) { interObj = new Intersection2D(new Segment(seg)); return true; } else { interObj = new Intersection2D(); return false; } } else { r /= den; // Let P be the position vector of the intersection point, then // P=A+r(B-A) if (0 <= r && r <= 1) { interObj = new Intersection2D(new Vector2D(seg.P0 + r * (seg.P1 - seg.P0))); return true; } } interObj = new Intersection2D(); return false; }