c# - 绘制边缘方向的算法

标签 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);

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

最佳答案

假设边的起点坐标为(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

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

关于c# - 绘制边缘方向的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8554211/

相关文章:

c# - 绕过标记语句

algorithm - Prim 算法与 Fibonacci 堆 : why O(E + V*log(V))?

algorithm - BFS生成的一棵树分析

c - uint64_t 的计算结果错误

math - 如何在 OpenGL 着色器中使用行专业?

c# - C dll向C#返回多个变量

c# - 升级到 ASP.NET Core 2.0 后无法使用单例 IActiveUsersService 中的作用域服务 IMongoDbContext

c# - XAML Windows 通用应用程序的全局命令栏

c++ - 用于检查数字是否在特定范围内的位旋转

python - 在Python中创建曲率动画