月份信息二维坐标图绘制---(绘制箭头完美算法)续II
今天中午,大家都在睡觉的时候,本人忙里偷闲找了点资料看了下,箭头算法终于到了最终版本了。非常感谢各位前辈在网上的资料,特别是MFC,JAVA的资料比较多。这里终于也可以告一段落了。
以下是C#的箭头算法,源代码如下:
1 /// <summary>
2 /// 绘制箭头
3 /// </summary>
4 /// <param name="graphics"></param>
5 /// <param name="startPoint">起始点</param>
6 /// <param name="endPoint">终止点</param>
7 private void DrawArrowHead(Graphics graphics, PointF startPoint, PointF endPoint)
8 {
9 double distance = Math.Abs(Math.Sqrt(
10 (startPoint.X - endPoint.X) * (startPoint.X - endPoint.X) +
11 (startPoint.Y - endPoint.Y) * (startPoint.Y - endPoint.Y)));
12
13 if (distance == 0)
14 {
15 return;
16 }
17
18 double xa = endPoint.X + ArrowLength * ((startPoint.X - endPoint.X)
19 + (startPoint.Y - endPoint.Y) / RelativeValue) / distance;
20 double ya = endPoint.Y + ArrowLength * ((startPoint.Y - endPoint.Y)
21 - (startPoint.X - endPoint.X) / RelativeValue) / distance;
22 double xb = endPoint.X + ArrowLength * ((startPoint.X - endPoint.X)
23 - (startPoint.Y - endPoint.Y) / RelativeValue) / distance;
24 double yb = endPoint.Y + ArrowLength * ((startPoint.Y - endPoint.Y)
25 + (startPoint.X - endPoint.X) / RelativeValue) / distance;
26
27 PointF[] polygonPoints ={
28 new PointF(endPoint.X , endPoint.Y),
29 new PointF( (float)xa , (float)ya),
30 new PointF( (float)xb , (float)yb)};
31
32 DrawArrowHead(graphics, polygonPoints);
33 }
2 /// 绘制箭头
3 /// </summary>
4 /// <param name="graphics"></param>
5 /// <param name="startPoint">起始点</param>
6 /// <param name="endPoint">终止点</param>
7 private void DrawArrowHead(Graphics graphics, PointF startPoint, PointF endPoint)
8 {
9 double distance = Math.Abs(Math.Sqrt(
10 (startPoint.X - endPoint.X) * (startPoint.X - endPoint.X) +
11 (startPoint.Y - endPoint.Y) * (startPoint.Y - endPoint.Y)));
12
13 if (distance == 0)
14 {
15 return;
16 }
17
18 double xa = endPoint.X + ArrowLength * ((startPoint.X - endPoint.X)
19 + (startPoint.Y - endPoint.Y) / RelativeValue) / distance;
20 double ya = endPoint.Y + ArrowLength * ((startPoint.Y - endPoint.Y)
21 - (startPoint.X - endPoint.X) / RelativeValue) / distance;
22 double xb = endPoint.X + ArrowLength * ((startPoint.X - endPoint.X)
23 - (startPoint.Y - endPoint.Y) / RelativeValue) / distance;
24 double yb = endPoint.Y + ArrowLength * ((startPoint.Y - endPoint.Y)
25 + (startPoint.X - endPoint.X) / RelativeValue) / distance;
26
27 PointF[] polygonPoints ={
28 new PointF(endPoint.X , endPoint.Y),
29 new PointF( (float)xa , (float)ya),
30 new PointF( (float)xb , (float)yb)};
31
32 DrawArrowHead(graphics, polygonPoints);
33 }
1 private static void DrawArrowHead(Graphics graphics, PointF[] points)
2 {
3 graphics.DrawPolygon(Pens.Red, points);
4 graphics.FillPolygon(new SolidBrush(Color.Red), points);
5 }
2 {
3 graphics.DrawPolygon(Pens.Red, points);
4 graphics.FillPolygon(new SolidBrush(Color.Red), points);
5 }
具体是通过终点Point的坐标,以及计算出的2个角的坐标,然后通过绘制多边形来绘制箭头。
显示效果如下:
这个已经是最终版本的算法,误差几乎可以忽略。上面的方法是可以调用的,大家可以试下,希望能够对各位有所帮助..........
分类:
[21] UML设计
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架