开发者

How to do hit testing inside a closed shape composed only of lines (no fill)

How to do it? The figure is an instance of DrawingVisual and DrawingContext.DrawLine is used to draw closed shapes.

As a makeshift solution, i draw 开发者_高级运维a semi transparent line, almost invisible, of the thickness equal to that of the entire shape. This way it responds to hit testing. Is there a better way to do it?


If your shape is convex, then for each segment (A ; B) of you shape you must do the following :

  • build the 3d vector vector MA (M is the click position and A the first point of the current segment).
  • build the 3d vector BA (B is the second point of the current segment and A the first point of the current segment)
  • calculate the dot product MA + BA, if the direction of the resulting vector are always the same for all the segments your point is in the shape.

If your shape is not convex you must split it into convex shape first then apply the previous method on each convex shape.


This comment is the right answer:

I was going to recommend using an enclosing border and use hit testing over that. But you have kind of implemented the same thing. (AngelWPF)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜