Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

py sol for Polygon Area #4867

Merged
merged 4 commits into from
Oct 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions content/5_Plat/Geo_Pri.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ We can use the [Shoelace formula](https://en.wikipedia.org/wiki/Shoelace_formula

## Implementation

**Time Complexity:** $\mathcal{O}(N)$

<LanguageSection>
<CPPSection>

Expand Down Expand Up @@ -247,10 +249,36 @@ int main() {
area +=
(1LL * points[i].x * points[i + 1].y - 1LL * points[i].y * points[i + 1].x);
}
cout << labs(area) << '\n';
}
```

</CPPSection>
<PySection>

```py
class Point:
def __init__(self, x: int = 0, y: int = 0):
self.x = x
self.y = y


n = int(input())
points = []
for _ in range(n):
x, y = map(int, input().split())
points.append(Point(x, y))

points.append(points[0])

area = 0
for i in range(n):
area += points[i].x * points[i + 1].y - points[i].y * points[i + 1].x

print(abs(area))
```

</PySection>
</LanguageSection>

# Point's Location Relative to Polygon
Expand Down
Loading