How to select a line

后端 未结 4 1221
一个人的身影
一个人的身影 2020-12-07 04:40

So I\'m trying to figure out how to implement a method of selecting lines or edges in a drawing area but my math is a bit lacking. This is what I got so far:

  • A
4条回答
  •  粉色の甜心
    2020-12-07 05:26

    Well, first off, since a mathematical line has no width it's going to be very difficult for a user to click exactly ON the line. As such, your best bet is to come up with some reasonable buffer (like 1 or 2 pixels or if your line graphically has a width use that) and calculate the distance from the point of the mouse click to the line. If the distance falls within your buffer then select the line. If you fall within that buffer for multiple lines, select the one that came closest.

    Line maths here:

    http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html

    Shortest distance between a point and a line segment

提交回复
热议问题