Exemplo n.º 1
0
        /// <summary>
        /// Calculates the minimum distance between a point and a line.
        /// </summary>
        /// <param name="p">A point.</param>
        /// <param name="origin">Line origin point.</param>
        /// <param name="dir">Line direction.</param>
        /// <returns>The minimum distance between the point and the line.</returns>
        public static double PointLineDistance(Vector3 p, Vector3 origin, Vector3 dir)
        {
            double  t               = Vector3.DotProduct(dir, p - origin);
            Vector3 pPrime          = origin + t * dir;
            Vector3 vec             = p - pPrime;
            double  distanceSquared = Vector3.DotProduct(vec, vec);

            return(Math.Sqrt(distanceSquared));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Build the reflection matrix of a mirror plane that passes through a point.
        /// </summary>
        /// <param name="normal">Mirror plane normal vector.</param>
        /// <param name="point">A point on the mirror plane.</param>
        /// <returns>A mirror plane reflection matrix that passes through a point.</returns>
        public static Matrix4 Reflection(Vector3 normal, Vector3 point)
        {
            // plane equation that passes through a point ax + by + cz + d = 0 where d = -point·normal
            Vector3 n = Vector3.Normalize(normal);
            double  a = n.X;
            double  b = n.Y;
            double  c = n.Z;
            double  d = -Vector3.DotProduct(point, n);

            return(new Matrix4(1 - 2 * a * a, -2 * a * b, -2 * a * c, -2 * a * d,
                               -2 * a * b, 1 - 2 * b * b, -2 * b * c, -2 * b * d,
                               -2 * a * c, -2 * b * c, 1 - 2 * c * c, -2 * c * d,
                               0, 0, 0, 1.0));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Checks if a point is inside a line segment.
        /// </summary>
        /// <param name="p">A point.</param>
        /// <param name="start">Segment start point.</param>
        /// <param name="end">Segment end point.</param>
        /// <returns>Zero if the point is inside the segment, 1 if the point is after the end point, and -1 if the point is before the start point.</returns>
        public static int PointInSegment(Vector3 p, Vector3 start, Vector3 end)
        {
            Vector3 dir    = end - start;
            Vector3 pPrime = p - start;
            double  t      = Vector3.DotProduct(dir, pPrime);

            if (t <= 0)
            {
                return(-1);
            }
            double dot = Vector3.DotProduct(dir, dir);

            if (t >= dot)
            {
                return(1);
            }
            return(0);
        }