Find if point lays on line segment

前端 未结 12 1324
抹茶落季
抹茶落季 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:36

    Or let the dotnet do the heavy lifting for you if using visual studio use a GraphicsPath

    this will also allow you to add tolerances for if just clicked outside the line.

    using (Drawing2D.GraphicsPath gp = new Drawing2D.GraphicsPath())
    {
        gp.AddLine(new Point(x1, y1), new Point(x2, y2));
    
        // Make the line as wide as needed (make this larger to allow clicking slightly outside the line) 
        using (Pen objPen = new Pen(Color.Black, 6))
        {
            gp.Widen(objPen);
        }
    
        if (gp.IsVisible(Mouse.x, Mouse.y))
        {
            // The line was clicked
        }
    }
    

提交回复
热议问题