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

``` 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
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

80 篇文章30 人订阅

0 条评论

## 相关文章

4115

2219

3225

671

2922

1662

4886

38612

### 51 Nod 1028 大数乘法 V2【Java大数乱搞】

1028 大数乘法 V2 基准时间限制：2 秒 空间限制：131072 KB 分值: 80 难度：5级算法题 给出2个大整数A,B，计算A*B的结果。 Inpu...

2704

### 对vector等STL标准容器进行排序操作

STL几乎封装了所有的数据结构中的算法，从链表到队列，从向量到堆栈，对hash到二叉树，从搜索到排序，从增加到删除......可以说，如果你理解了STL，你会...

2612