From fac0d7dba8f2e1b60940798c4f38a937e435a868 Mon Sep 17 00:00:00 2001 From: siminsimin Date: Wed, 16 Oct 2024 22:02:13 +0800 Subject: [PATCH 1/3] Compute the area of a polygon. --- geometry/shoelace.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 geometry/shoelace.py diff --git a/geometry/shoelace.py b/geometry/shoelace.py new file mode 100644 index 000000000000..a00f116b47ce --- /dev/null +++ b/geometry/shoelace.py @@ -0,0 +1,30 @@ +def area_of_polygon(xs: list[float], ys: list[float]) -> float: + """ + Compute the area of a polygon. The polygon has to be planar and simple + (not self-intersecting). The vertices have to be ordered in the + counter-clockwise direction. + https://en.wikipedia.org/wiki/Shoelace_formula + + Args: + xs: list of x coordinates of the polygon vertices in counter-clockwise order + ys: list of y coordinates of the polygon vertices in counter-clockwise order + Returns: + area of the polygon + + >>> from math import isclose + >>> xs = [1, 3, 7, 4, 8] + >>> ys = [6, 1, 2, 4, 5] + >>> isclose(area_of_polygon(xs, ys), 16.5) + True + """ + + return 0.5 * sum( + (ys[i] + ys[(i + 1) % len(ys)]) * (xs[i] - xs[(i + 1) % len(xs)]) + for i in range(len(xs)) + ) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From cb2ad8522305f39f77ea58ee55cff787341e6c50 Mon Sep 17 00:00:00 2001 From: simin75simin Date: Wed, 16 Oct 2024 14:02:34 +0000 Subject: [PATCH 2/3] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index f0a34a553946..c8b9a9d3b465 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -457,6 +457,7 @@ ## Geometry * [Geometry](geometry/geometry.py) + * [Shoelace](geometry/shoelace.py) ## Graphics * [Bezier Curve](graphics/bezier_curve.py) From 569441e0a9df9504e7a82fd61347d3621a844461 Mon Sep 17 00:00:00 2001 From: cclauss Date: Mon, 14 Sep 2026 04:33:07 +0000 Subject: [PATCH 3/3] updating DIRECTORY.md --- DIRECTORY.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 9a47d9b9a606..29183f42a94e 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -543,13 +543,13 @@ ## [Geometry](geometry) * [Geometry](geometry/geometry.py) - * [Shoelace](geometry/shoelace.py) * [Graham Scan](geometry/graham_scan.py) * [Jarvis March](geometry/jarvis_march.py) * [Monotone Chain](geometry/monotone_chain.py) * [Ramer Douglas Peucker](geometry/ramer_douglas_peucker.py) * [Rotating Calipers](geometry/rotating_calipers.py) * [Segment Intersection](geometry/segment_intersection.py) + * [Shoelace](geometry/shoelace.py) * Tests * [Test Graham Scan](geometry/tests/test_graham_scan.py) * [Test Jarvis March](geometry/tests/test_jarvis_march.py) @@ -818,6 +818,7 @@ * [Monte Carlo](maths/monte_carlo.py) * [Monte Carlo Dice](maths/monte_carlo_dice.py) * [Ncr Combinations](maths/ncr_combinations.py) + * [Next Prime Number](maths/next_prime_number.py) * [Number Of Digits](maths/number_of_digits.py) * Numerical Analysis * [Adams Bashforth](maths/numerical_analysis/adams_bashforth.py)