-
Notifications
You must be signed in to change notification settings - Fork 163
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
PKG: Add convexhull computation package #1840
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .lpconvexhull_main import convex_hull |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from lpython import i32 | ||
from .utils import min, distance | ||
|
||
def orientation(p: tuple[i32, i32], q: tuple[i32, i32], r: tuple[i32, i32]) -> i32: | ||
# Function to find the orientation of triplet (p, q, r) | ||
# Returns the following values: | ||
# 0: Colinear | ||
# 1: Clockwise | ||
# 2: Counterclockwise | ||
value: i32 = (q[1] - p[1]) * (r[0] - q[0]) - (q[0] - p[0]) * (r[1] - q[1]) | ||
if value == 0: | ||
return 0 # Colinear | ||
elif value > 0: | ||
return 1 # Clockwise | ||
else: | ||
return 2 # Counterclockwise | ||
|
||
|
||
def convex_hull(points: list[tuple[i32, i32]]) -> list[tuple[i32, i32]]: | ||
"""Finds the convex hull of a set of points. | ||
|
||
Args: | ||
points: A list of points. | ||
|
||
Returns: | ||
A list of points that form the convex hull. | ||
""" | ||
|
||
n: i32 = len(points) | ||
if n < 3: | ||
return [(-1, -1)] # Convex hull not possible | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems that we do not support returning empty lists (#1841). Therefore, returning a list with single item with negative/invalid coordinates. |
||
|
||
# Find the leftmost point | ||
leftmost: tuple[i32, i32] = min(points) | ||
hull: list[tuple[i32, i32]] = [] | ||
|
||
p: tuple[i32, i32] = leftmost | ||
|
||
while True: | ||
hull.append(p) | ||
q: tuple[i32, i32] = points[0] | ||
|
||
r: tuple[i32, i32] | ||
for r in points: | ||
if r == p or r == q: | ||
continue | ||
direction: i32 = orientation(p, q, r) | ||
if direction == 1 or (direction == 0 and distance(p, r) > distance(p, q)): | ||
q = r | ||
|
||
p = q | ||
|
||
if p == leftmost: | ||
break | ||
|
||
return hull |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from lpython import i32, f64 | ||
|
||
def min(points: list[tuple[i32, i32]]) -> tuple[i32, i32]: | ||
"""Finds the left-most point in a list of points. | ||
|
||
Args: | ||
points: A list of points. | ||
|
||
Returns: | ||
The left-most point in the list. | ||
""" | ||
|
||
left_most_point: tuple[i32, i32] = points[0] | ||
point: tuple[i32, i32] | ||
for point in points: | ||
if point[0] < left_most_point[0]: | ||
left_most_point = point | ||
|
||
return left_most_point | ||
|
||
|
||
def distance(p: tuple[i32, i32], q: tuple[i32, i32]) -> f64: | ||
# Function to calculate the Euclidean distance between two points | ||
x1: i32; y1: i32 | ||
x2: i32; y2: i32 | ||
|
||
x1, y1 = p | ||
x2, y2 = q | ||
return f64((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from lpython import Const, i32, f64 | ||
|
||
from lpdraw import Line, Circle, Clear, Display | ||
from lpconvexhull import convex_hull | ||
from numpy import empty, int32 | ||
|
||
def plot_graph(polygon: list[tuple[i32, i32]], points: list[tuple[i32, i32]]): | ||
Width: Const[i32] = 500 # x-axis limits [0, 499] | ||
Height: Const[i32] = 500 # y-axis limits [0, 499] | ||
Screen: i32[Height, Width] = empty((Height, Width), dtype=int32) | ||
Clear(Height, Width, Screen) | ||
|
||
i: i32 | ||
n: i32 = len(polygon) | ||
for i in range(n): | ||
Line(Height, Width, Screen, polygon[i][0], polygon[i][1], polygon[(i + 1) % n][0], polygon[(i + 1) % n][1]) | ||
|
||
point_size: i32 = 5 | ||
for i in range(len(points)): | ||
Circle(Height, Width, Screen, points[i][0], points[i][1], f64(point_size)) | ||
|
||
Display(Height, Width, Screen) | ||
|
||
def main0(): | ||
points: list[tuple[i32, i32]] = [(445, 193), (138, 28), (418, 279), (62, 438), (168, 345), (435, 325), (293, 440), (158, 94), (403, 288), (136, 278), (141, 243), (287, 313), (338, 492), (172, 78), (29, 404), (79, 377), (184, 91), (69, 324), (408, 72), (494, 1)] | ||
convex_hull_points: list[tuple[i32, i32]] = convex_hull(points) | ||
# print(convex_hull_points) | ||
plot_graph(convex_hull_points, points) | ||
|
||
assert convex_hull_points == [(29, 404), (138, 28), (494, 1), (435, 325), (338, 492), (62, 438)] | ||
|
||
main0() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it work with llvm?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does not work with LLVM yet. I will create an issue for it soon.