Find if point lays on line segment

前端 未结 12 1326
抹茶落季
抹茶落季 2020-11-30 04:01

I have line segment defined by two points A(x1,y1,z1) and B(x2,y2,z2) and point p(x,y,z). How can I check if the point lays on the line segment?

12条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-30 04:43

    I use this to calculate the distance AB between points a and b.

    static void Main(string[] args)
    {
            double AB = segment(0, 1, 0, 4);
            Console.WriteLine("Length of segment AB: {0}",AB);
    }
    
    static double segment (int ax,int ay, int bx, int by)
    {
        Vector a = new Vector(ax,ay);
        Vector b = new Vector(bx,by);
        Vector c = (a & b);
        return Math.Sqrt(c.X + c.Y);
    }
    
    struct Vector
    {
        public readonly float X;
        public readonly float Y;
    
        public Vector(float x, float y)
        {
            this.X = x;
            this.Y = y;
        }
        public static Vector operator &(Vector a, Vector b)  
        {
            return new Vector((b.X - a.X) * (b.X - a.X), (b.Y - a.Y) * (b.Y - a.Y));
        }
    }
    

    based on Calculate a point along the line A-B at a given distance from A

提交回复
热议问题