コード例 #1
0
        /// <summary>
        /// Calculates the squared distance between a given point and a given ray.
        /// </summary>
        /// <param name="point">A <see cref="Vector2F"/> instance.</param>
        /// <param name="ray">A <see cref="Ray"/> instance.</param>
        /// <returns>The squared distance between the point and the ray.</returns>
        public static float SquaredDistance(Vector2F point, Ray ray)
        {
            Vector2F diff = point - ray.Origin;
            float    t    = Vector2F.DotProduct(diff, ray.Direction);

            if (t <= 0.0f)
            {
                t = 0.0f;
            }
            else
            {
                t    /= ray.Direction.GetLengthSquared();
                diff -= t * ray.Direction;
            }

            return(diff.GetLengthSquared());
        }
コード例 #2
0
ファイル: Circle.cs プロジェクト: stenfalp/Sharp3D.Math
        /// <summary>
        /// Find the intersection of a ray and a sphere.
        /// Only works with unit rays (normalized direction)!!!
        /// </summary>
        /// <remarks>
        /// This is the optimized Ray-Sphere intersection algorithms described in "Real-Time Rendering".
        /// </remarks>
        /// <param name="ray">The ray to test.</param>
        /// <param name="t">
        /// If intersection accurs, the function outputs the distance from the ray's origin
        /// to the closest intersection point to this parameter.
        /// </param>
        /// <returns>Returns True if the ray intersects the sphere. otherwise, <see langword="false"/>.</returns>
        public bool FindIntersections(Ray ray, ref float t)
        {
            // Only gives correct result for unit rays.
            //Debug.Assert(MathUtils.ApproxEquals(1.0f, ray.Direction.GetLength()), "Ray direction should be normalized!");

            // Calculates a vector from the ray origin to the sphere center.
            Vector2F diff = this.center - ray.Origin;
            // Compute the projection of diff onto the ray direction
            float d = Vector2F.DotProduct(diff, ray.Direction);

            float diffSquared   = diff.GetLengthSquared();
            float radiusSquared = this.radius * this.radius;

            // First rejection test :
            // if d<0 and the ray origin is outside the sphere than the sphere is behind the ray
            if ((d < 0.0f) && (diffSquared > radiusSquared))
            {
                return(false);
            }

            // Compute the distance from the sphere center to the projection
            float mSquared = diffSquared - d * d;

            // Second rejection test:
            // if mSquared > radiusSquared than the ray misses the sphere
            if (mSquared > radiusSquared)
            {
                return(false);
            }

            float q = (float)System.Math.Sqrt(radiusSquared - mSquared);

            // We are interested only in the first intersection point:
            if (diffSquared > radiusSquared)
            {
                // If the origin is outside the sphere t = d - q is the first intersection point
                t = d - q;
            }
            else
            {
                // If the origin is inside the sphere t = d + q is the first intersection point
                t = d + q;
            }
            return(true);
        }
コード例 #3
0
        /// <summary>
        /// Calculates the squared distance between two points.
        /// </summary>
        /// <param name="point1">A <see cref="Vector2F"/> instance.</param>
        /// <param name="point2">A <see cref="Vector2F"/> instance.</param>
        /// <returns>The squared distance between the two points.</returns>
        public static float SquaredDistance(Vector2F point1, Vector2F point2)
        {
            Vector2F diff = point1 - point2;

            return(diff.GetLengthSquared());
        }