用一条线连接两个WPF画布元素,而不使用锚点?

前端之家收集整理的这篇文章主要介绍了用一条线连接两个WPF画布元素,而不使用锚点?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个用于绘图的画布,并希望通过有向线(箭头末端)连接图中的节点.
我尝试了锚点方法,其中行只附加在节点上的特定点,但这对我不起作用,它看起来像垃圾.

我只想要从每个对象的中心到另一个对象的线,并在节点的边缘停止线,以便正确显示箭头.但是,找到测试交叉点的画布元素的边缘已经证明是困难的.

有任何想法吗?

解决方法

我有一个方法使用元素的边界框.它并不完美,因为我的元素不是完美的矩形,但看起来还不错.

基本上我在Canvas坐标中找到元素的边界框:

  1. private static Rect GetBounds(FrameworkElement element,UIElement visual)
  2. {
  3. return new Rect(
  4. element.TranslatePoint(new Point(0,0),visual),element.TranslatePoint(new Point(element.ActualWidth,element.ActualHeight),visual));
  5. }

然后,我找到中心线与边界框四边的每一边的交点,并使用该交点通过线形连接这两个元素.

我在Third Party Ninjas找到了交叉点代码
http://thirdpartyninjas.com/blog/2008/10/07/line-segment-intersection/

  1. private void ProcessIntersection()
  2. {
  3. float ua = (point4.X - point3.X) * (point1.Y - point3.Y) - (point4.Y - point3.Y) * (point1.X - point3.X);
  4. float ub = (point2.X - point1.X) * (point1.Y - point3.Y) - (point2.Y - point1.Y) * (point1.X - point3.X);
  5. float denominator = (point4.Y - point3.Y) * (point2.X - point1.X) - (point4.X - point3.X) * (point2.Y - point1.Y);
  6.  
  7. intersection = coincident = false;
  8.  
  9. if (Math.Abs(denominator) <= 0.00001f)
  10. {
  11. if (Math.Abs(ua) <= 0.00001f && Math.Abs(ub) <= 0.00001f)
  12. {
  13. intersection = coincident = true;
  14. intersectionPoint = (point1 + point2) / 2;
  15. }
  16. }
  17. else
  18. {
  19. ua /= denominator;
  20. ub /= denominator;
  21.  
  22. if (ua >= 0 && ua <= 1 && ub >= 0 && ub <= 1)
  23. {
  24. intersection = true;
  25. intersectionPoint.X = point1.X + ua * (point2.X - point1.X);
  26. intersectionPoint.Y = point1.Y + ua * (point2.Y - point1.Y);
  27. }
  28. }
  29. }

瞧!现在绘制线条,就好像它们从每个节点的中心到另一个节点的中心,但大致停在节点的边缘,因此箭头结束是可见的.

这种方法的一个改进是测试节点本身的实际边缘,例如椭圆节点,但我还没有找到一个WPF方法,它为我提供了我可以测试的几何或路径.

猜你在找的MsSQL相关文章