Skip to content

Commit 3d3e728

Browse files
Meto TrajkovskiMeto Trajkovski
authored andcommitted
Added check_if_point_inside_polygon and one more resources
1 parent 7aa26c1 commit 3d3e728

2 files changed

Lines changed: 73 additions & 2 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
'''
2+
Check if Point is Inside Polygon
3+
4+
Given a polygon (created by counterclockwise ordered points, more than 2 points) and a point "p", find if "p" lies inside the polygon or not.
5+
The points lying on the border are considered inside.
6+
7+
Input: [(0, 0), (3, 0), (3, 2), (0, 2)], (1, 1)
8+
Output: True
9+
Output explanation: In this example the polygon is a rectangle parallel with the X axis.
10+
11+
=========================================
12+
To check if a point is inside a polygon you'll need to draw a straight line (in any of the 4 directions: up, right, down, left),
13+
and count the number of times the line intersects with polygon edges. If the number of intersections is odd then the point
14+
is inside or lies on an edge of the polygon, otherwise the point is outside.
15+
Time Complexity: O(N)
16+
Space Complexity: O(1)
17+
'''
18+
19+
20+
############
21+
# Solution #
22+
############
23+
24+
def check_if_point_inside_polygon(polygon, p):
25+
n = len(polygon)
26+
# add the first point as last to avoid checking, using modulo (polygon[(i + 1) % n]) or duplicate code for the last point
27+
polygon.append(polygon[0])
28+
is_inside = False # or you can use counter and return (counter % 2) == 1
29+
30+
for i in range(n):
31+
if intersect(polygon[i], polygon[i + 1], p):
32+
is_inside = not is_inside
33+
34+
return is_inside
35+
36+
def intersect(a, b, p):
37+
# Y coordinate of p should be between Y coordinates
38+
# this can be written like (a[1] > p[1]) != (b[1] > p[1])
39+
if p[1] < max(a[1], b[1]) and p[1] >= min(a[1], b[1]):
40+
'''
41+
Equation of line:
42+
y = (x - x0) * ((y1 - y0) / (x1 - x0)) + y0
43+
This formula is computed using the gradients (slopes, changes in the coordinates)
44+
Modify this formula to find X instead Y (because you already have Y)
45+
'''
46+
x_intersect = (p[1] - a[1]) * ((b[0] - a[0]) / (b[1] - a[1])) + a[0]
47+
48+
# check if the point is on the left of the intersection (because in this case you're drawing a line to the right)
49+
return x_intersect <= p[1]
50+
51+
52+
###########
53+
# Testing #
54+
###########
55+
56+
# Test 1
57+
# Correct result => True
58+
print(check_if_point_inside_polygon([(0, 0), (3, 0), (3, 2), (0, 2)], (1, 1)))
59+
60+
# Test 2
61+
# Correct result => True
62+
print(check_if_point_inside_polygon([(0, 0), (3, 0), (3, 2), (0, 2)], (1, 0)))
63+
64+
# Test 3
65+
# Correct result => True
66+
print(check_if_point_inside_polygon([(0, 0), (3, 0), (3, 2), (0, 2)], (3, 1)))
67+
68+
# Test 3
69+
# Correct result => False
70+
print(check_if_point_inside_polygon([(0, 0), (3, 0), (3, 2), (0, 2)], (3, 3)))

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ If the problems from [LeetCode](https://leetcode.com/) are not enough and you ne
200200

201201
1. [Geeks For Geeks](https://www.geeksforgeeks.org/) - The site which **all** interested in algorithms (no matter if beginners or experts) should know!
202202
2. [The Algorithms - Python](https://github.com/TheAlgorithms/Python) - Great GitHub repo with many algorithms written in Python ([Link](https://github.com/TheAlgorithms) from the same repo written in other programming languages).
203-
3. [KhanAcademy - Algorithms](https://www.khanacademy.org/computing/computer-science/algorithms) - Good explanations for some basic algorithms.
204-
4. HackerRank - YouTube tutorials
203+
3. [CP Algorithms](http://cp-algorithms.com/) - Great page with excellent explanations for various algorithms.
204+
4. [KhanAcademy - Algorithms](https://www.khanacademy.org/computing/computer-science/algorithms) - Good explanations for some basic algorithms.
205+
5. HackerRank - YouTube tutorials
205206
- [Algorithms](https://www.youtube.com/playlist?list=PLI1t_8YX-ApvMthLj56t1Rf-Buio5Y8KL)
206207
- [Data Structures](https://www.youtube.com/playlist?list=PLI1t_8YX-Apv-UiRlnZwqqrRT8D1RhriX)

0 commit comments

Comments
 (0)