Skip to content

Commit 46e1bc0

Browse files
authored
Merge pull request AllAlgorithms#45 from polettix/patch-area-of-polygon
Add content for "Area of polygon"
2 parents 4a30f38 + 765bcb3 commit 46e1bc0

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

docs/area-of-polygon.md

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,66 @@ title: Area of polygon
44
sidebar_label: Area of polygon
55
---
66

7-
[Open a pull request](https://github.com/AllAlgorithms/algorithms/tree/master/docs/area-of-polygon.md) to add the content for this algorithm.
7+
A polygon is a closed sequence of segments in the plane. It is often
8+
convenient to represent it as a sequence of vertices *P<sub>1</sub>,
9+
P<sub>2</sub>, ..., P<sub>n</sub>*, with the convention that any pair of
10+
adjacent vertices in the sequence define a segment in the polygon, and
11+
that the first and last vertices are the same. Each point *P<sub>i</sub>*
12+
is assumed to have coordinates in the plane *(x<sub>i</sub>,
13+
y<sub>i</sub>)*.
14+
15+
## Simple polygons
16+
17+
A [simple polygon][] is a non-self-intersecting one, i.e. no pair of
18+
segments intersects each other.
19+
20+
### Formula
21+
22+
The *signed* area of the polygon is given by the following formula:
23+
24+
<img
25+
src="https://wikimedia.org/api/rest_v1/media/math/render/svg/d09d3414b2e985a26205bc932010c62d3ffd668d"
26+
alt="area of a polygon" />
27+
28+
The area is *signed* because going in reverse order means having a result
29+
with the opposite sign. To get the area it's necessary to take the
30+
absolute value.
31+
32+
### Algorithm
33+
34+
The following algorithm translates the formula in pseudocode:
35+
36+
```
37+
/*
38+
Vs is an array of vertices. Each vertex is supposed to be
39+
an array itself, with X values in index 0 and Y values in
40+
index 1. It is also assumed that the last element in Vs is
41+
the same as the first one.
42+
*/
43+
polygon_area_simple (Vs) {
44+
N = Vs.size; # number of elements in Vs
45+
sum = 0;
46+
for i in 0 .. (N - 1):
47+
sum = sum + Vs[i][0] * Vs[i+1][1] - Vs[i+1][0] * Vs[i][1];
48+
return abs(sum) / 2;
49+
}
50+
```
51+
52+
## Performance
53+
54+
The algorithm for [simple polygons][simple polygon] is linear with respect
55+
to the number of vertices (O(N)).
56+
57+
## Implementation
58+
59+
| | Language | Link |
60+
|:-: | :-: | :-: |
61+
| | | |
62+
63+
## Helpful links
64+
65+
- [Polygon][]
66+
67+
68+
[simple polygon]: https://en.wikipedia.org/wiki/Simple_polygon
69+
[Polygon]: https://en.wikipedia.org/wiki/Polygon

0 commit comments

Comments
 (0)