平面多边形面积计算

算法描述

在这里实现了对任意平面多边形的面积计算,不适用于有重叠或边互相交叉的情况。

实现

窗体设计

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace area
{
public partial class Main : Form
{
private List<Point> list = new List<Point>();
public Main()
{
InitializeComponent();
}

private void btnAdd_Click(object sender, EventArgs e)
{
if (txtX.Text != "" && txtY.Text != "")
{
//插入点
if (Int32.TryParse(txtX.Text, out int x) && Int32.TryParse(txtY.Text, out int y))
{
listPoint.BeginUpdate();
Point point = new Point(x, y);
list.Add(point);
listPoint.Items.Add(point);
listPoint.EndUpdate();

txtX.Text = "";
txtY.Text = "";
txtX.Focus();
}
else
{
MessageBox.Show("请输入数字!");
txtX.Text = "";
txtY.Text = "";
txtX.Focus();
}
}
}

private void btnCalcu_Click(object sender, EventArgs e)
{
if (list.Count >= 3)
{
//将线首加入列表,保证多边形闭合
list.Add(list[0]);

//计算面积
double area = 0;

//根据公式进行计算
for (int i = 0; i < list.Count - 1; i++)
{
area += (list[i].X +list[i+1].X) * (list[i + 1].Y - list[i].Y);
}

//保证面积为正
area = Math.Abs(area / 2);
txtResult.Text = "面积:" + area.ToString("0.0");
btnAdd.Enabled = false;
}
else
{
MessageBox.Show("请添加至少3个点!");
}
}

private void btnClear_Click(object sender, EventArgs e)
{
//清空所有点
list.Clear();
listPoint.Items.Clear();
txtX.Text = "";
txtY.Text = "";
txtResult.Text = "面积:";
btnAdd.Enabled = true;
txtX.Focus();
}
}
}