这个源代码写的不是十全十美,只是提供一个
还待完善的地方例如判断是否这个图形是封闭的。得空在解决吧
这只是一个算法上
谁有c#的参考手册网盘分享一份
谢谢
下面请看源码
凑够150个字了,不废话了。
鼠标画图难免会有误差,所以需要容忍一定的误差
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