通过一条线连接两个 WPF 画布元素,而不使用锚点?
我有一个用于绘图的画布,并且想通过有向线(箭头末端)连接图中的节点。 我尝试了锚点方法,其中线条仅附加在节点上的特定点,但这对我不起作用,它看起来像垃圾。
我只是想要一条从每个对象的中心到另一个对象的线,并将线停在节点的边缘,以便箭头末端正确显示。但事实证明,找到画布元素的边缘来测试交叉点很困难。
有什么想法吗?
I have a canvas for diagramming, and want to join nodes in the diagram by directed lines (arrow ends).
I tried the anchor approach, where lines only attach at specific points on the nodes but that did not work for me, it looked like crap.
I simply want a line from the centre of each object to the other, and stop the line at the nodes' edge in order for the arrow end to show properly. But finding the edge of a canvas element to test intersections against has proven difficult.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我有一个使用元素边界框的方法。它并不完美,因为我的元素不是完美的矩形,但看起来还不错。
基本上,我通过以下方式找到 Canvas 坐标中元素的边界框:
然后,我找到中心到中心线与边界框四个边中每一个的交点,并使用该交点通过 a 连接两个元素线条形状。
我在Third Party Ninjas找到了交叉点代码:
http://thirdpartyninjas.com/blog/2008/10/07/线段交点/
瞧!现在绘制的线就好像它们从每个节点的中心到另一个节点一样,但大约停在节点的边缘,因此箭头末端可见。
此方法的改进是针对节点本身的实际边缘(例如椭圆节点)进行测试,但我尚未找到一种 WPF 方法,该方法可为我提供可以测试的几何图形或路径。
I got a method working using the bounding box of the element. It is not perfect, since my elements are not perfectly rectangular, but it looks OK.
Basically I find the bounding box of the element in Canvas coordinates by:
Then I find the intersection of the centre-to-centre line against each of the four sides of the bounding box, and use that intersection point to connect the two elements by a Line shape.
I found the intersection code at Third Party Ninjas:
http://thirdpartyninjas.com/blog/2008/10/07/line-segment-intersection/
And voilá! The lines are now drawn as if they go from the centre of each node to the other, but stops approximately at the node's edge so the arrow end is visible.
An improvement of this method would be to test against the actual edge of the node itself, such as for elliptical nodes, but I have yet to find a WPF method that provides me with a Geometry or Path that I can test against.