前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C#判断画的图形是不是三角形

C#判断画的图形是不是三角形

作者头像
拾点阳光
发布2018-05-10 18:09:47
1.1K0
发布2018-05-10 18:09:47
举报
文章被收录于专栏:码云1024码云1024

这个源代码写的不是十全十美,只是提供一个

还待完善的地方例如判断是否这个图形是封闭的。得空在解决吧

这只是一个算法上

谁有c#的参考手册网盘分享一份

谢谢

下面请看源码

凑够150个字了,不废话了。

鼠标画图难免会有误差,所以需要容忍一定的误差

代码语言:javascript
复制
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 /// <summary>
 8 /// 判断图形是否是三角形
 9 /// 3点一线
10 /// </summary>
11 
12 
13 namespace ConsoleApplication1
14 {
15     struct Point
16     {
17         public Point(float x, float y)
18         {
19             this.x = x;
20             this.y = y;
21         }
22         public float x;
23         public float y;
24     }
25     class ZJB
26     {
27         const int MaxNum = 100;
28         public Point[] point = new Point[MaxNum];
29         public int AngleNum = 0;//角的个数
30         public int PointNum = 0;//点的个数
31         public float DistanceError = 2;//可以忽略的距离
32         /// <summary>
33         /// 求两点之间的距离
34         /// </summary>
35         /// <param name="p1">坐标点</param>
36         /// <param name="p2">坐标点</param>
37         /// <returns>两点之间的距离</returns>
38         public float PointDistance(Point p1, Point p2)
39         {
40             float Value = (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y);
41             Value = (float)Math.Sqrt(Value);
42             return Value;
43         }
44 
45         /// <summary>
46         /// 判断三角形
47         /// </summary>
48         public void Delta()
49         {
50             //测试用例---------------------------
51             point[0] = new Point(1, 0);
52             point[1] = new Point(0, 10);
53             point[2] = new Point(0, 19);
54             point[3] = new Point(0, 30);
55             point[4] = new Point(10, 30);
56             point[5] = new Point(20, 30);
57             point[6] = new Point(30, 29);
58             point[7] = new Point(20, 20);
59             point[8] = new Point(10, 10);
60             point[9] = new Point(0, 0);
61             PointNum = 10;
62             //-----------------------------------
63 
64 
65             for (int i = 1; i < PointNum - 1; i++)
66             {
67                 float ac = PointDistance(point[i + 1], point[i - 1]);
68                 float ab = PointDistance(point[i], point[i - 1]);
69                 float bc = PointDistance(point[i], point[i + 1]);
70                 Console.WriteLine("(ab + bc) - ac={0}", (ab + bc) - ac);
71                 if ((ab + bc) - ac > DistanceError)
72                 {
73                     AngleNum++;
74                 }
75                 if (i == PointNum - 2)
76                 {
77                     ac = PointDistance(point[i], point[1]);
78                     ab = PointDistance(point[i], point[0]);
79                     bc = PointDistance(point[0], point[1]);
80                     Console.WriteLine("(ab + bc) - ac={0}", (ab + bc) - ac);
81                     if ((ab + bc) - ac > DistanceError)
82                     {
83                         AngleNum++;
84                     }
85                 }
86             }
87 
88             Console.WriteLine("这是一个{0}边形", AngleNum);
89 
90 
91         }
92     }
93 }

 01010101010101010101

 01010101010101010101

 01010101010101010101

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-07-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档