Gets the distance from a point to a line in 3 space in Xna - CSharp Microsoft.Xna.Framework

CSharp examples for Microsoft.Xna.Framework:Xna

Description

Gets the distance from a point to a line in 3 space in Xna

Demo Code


using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/*w  w w  . j a  v a2s .c o m*/

public class Main{
        /// <summary>
        /// Gets the distance from a point to a line in 3 space
        /// Reference: Wolfram Alpha
        /// </summary>
        /// <param name="pt">Point in 3 space</param>
        /// <param name="line">Line in 3 space</param>
        /// <returns>minimum distance from point to the line</returns>
        public static float distanceFromPointToLine(Vector3 pt, Ray line)
        {
            //Let two points in space define a line:
            Vector3 x1 = line.Position;
            Vector3 x2 = line.Position + line.Direction;
            
            return Vector3.Cross((x2 - x1),(x1 - pt)).Length()/(x2-x1).Length();
        }
}

Related Tutorials