File tree 1 file changed +30
-0
lines changed 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change
1
+ def area_of_polygon (xs : list [float ], ys : list [float ]) -> float :
2
+ """
3
+ Compute the area of a polygon. The polygon has to be planar and simple
4
+ (not self-intersecting). The vertices have to be ordered in the
5
+ counter-clockwise direction.
6
+ https://en.wikipedia.org/wiki/Shoelace_formula
7
+
8
+ Args:
9
+ xs: list of x coordinates of the polygon vertices in counter-clockwise order
10
+ ys: list of y coordinates of the polygon vertices in counter-clockwise order
11
+ Returns:
12
+ area of the polygon
13
+
14
+ >>> from math import isclose
15
+ >>> xs = [1, 3, 7, 4, 8]
16
+ >>> ys = [6, 1, 2, 4, 5]
17
+ >>> isclose(area_of_polygon(xs, ys), 16.5)
18
+ True
19
+ """
20
+
21
+ return 0.5 * sum (
22
+ (ys [i ] + ys [(i + 1 ) % len (ys )]) * (xs [i ] - xs [(i + 1 ) % len (xs )])
23
+ for i in range (len (xs ))
24
+ )
25
+
26
+
27
+ if __name__ == "__main__" :
28
+ import doctest
29
+
30
+ doctest .testmod ()
You can’t perform that action at this time.
0 commit comments