绘制边缘方向的算法

时间:2011-12-18 19:29:55

标签: c# algorithm math

我正在开发图形应用程序。在应用程序窗口中,有边缘连接的顶点。用户能够移动顶点,并且边缘也会移动。我有问题找到模式来绘制箭头表示边缘方向取决于两个顶点位置。

这是一个例子。

假设顶点有width/height = 20px;边缘从Vertex1的中心绘制到Vertex2的中心。

Vertex1.position = new Point(0,0);
Vertex2.position = new Point(100,0);
Edge.point1 = new Point(10,10);
Edge.point2 = new Point(110,10);
//Arrow representing direction from Vertex1 to Vertex2
Arrow.point1 = new Point(100,10);
Arrow.point2 = new Point(90,20);
Arrow.point3 = new Point(90,0);

问题是:知道边缘起点/终点的位置,如何计算箭头点?

2 个答案:

答案 0 :(得分:3)

假设边缘的起点有坐标(ax, ay),终点(bx, by),顶点有半径w,你的箭头有指针l的长度和角度在箭头边alpha之间 然后是伪代码:

ex := (bx - ax) 
ey := (by - ay) 
ex := ex / sqrt(ex^2 + ey^2)
ey := ey / sqrt(ex^2 + ey^2)

箭头的第一点:

a1x := bx - w * ex
a1y := by - w * ey

箭头的第二点:

a2x := bx - (w + l) * ex + l * tg(alpha/2) * ey
a2y := by - (w + l) * ey - l * tg(alpha/2) * ex

箭头的第三点:

a3x := bx - (w + l) * ex - l * tg(alpha/2) * ey
a3y := by - (w + l) * ey + l * tg(alpha/2) * ex

抱歉格式不佳,我不知道如何在这里使用数学标记。我希望我在计算中没有犯任何错误。

答案 1 :(得分:0)

编辑:您没有指出您的应用类型。如果您使用WinForms this可能会有用。 Here是关于LineCap的MSDN文章。