Skip to content

Commit f726048

Browse files
committed
PKG: Add package lpconvexhull
1 parent 2aacbb6 commit f726048

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .lpconvexhull_main import convex_hull
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from lpython import i32
2+
from .utils import min, distance
3+
4+
def orientation(p: tuple[i32, i32], q: tuple[i32, i32], r: tuple[i32, i32]) -> i32:
5+
# Function to find the orientation of triplet (p, q, r)
6+
# Returns the following values:
7+
# 0: Colinear
8+
# 1: Clockwise
9+
# 2: Counterclockwise
10+
value: i32 = (q[1] - p[1]) * (r[0] - q[0]) - (q[0] - p[0]) * (r[1] - q[1])
11+
if value == 0:
12+
return 0 # Colinear
13+
elif value > 0:
14+
return 1 # Clockwise
15+
else:
16+
return 2 # Counterclockwise
17+
18+
19+
def convex_hull(points: list[tuple[i32, i32]]) -> list[tuple[i32, i32]]:
20+
"""Finds the convex hull of a set of points.
21+
22+
Args:
23+
points: A list of points.
24+
25+
Returns:
26+
A list of points that form the convex hull.
27+
"""
28+
29+
n: i32 = len(points)
30+
if n < 3:
31+
return [(-1, -1)] # Convex hull not possible
32+
33+
# Find the leftmost point
34+
leftmost: tuple[i32, i32] = min(points)
35+
hull: list[tuple[i32, i32]] = []
36+
37+
p: tuple[i32, i32] = leftmost
38+
39+
while True:
40+
hull.append(p)
41+
q: tuple[i32, i32] = points[0]
42+
43+
r: tuple[i32, i32]
44+
for r in points:
45+
if r == p or r == q:
46+
continue
47+
direction: i32 = orientation(p, q, r)
48+
if direction == 1 or (direction == 0 and distance(p, r) > distance(p, q)):
49+
q = r
50+
51+
p = q
52+
53+
if p == leftmost:
54+
break
55+
56+
return hull
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from lpython import i32, f64
2+
3+
def min(points: list[tuple[i32, i32]]) -> tuple[i32, i32]:
4+
"""Finds the left-most point in a list of points.
5+
6+
Args:
7+
points: A list of points.
8+
9+
Returns:
10+
The left-most point in the list.
11+
"""
12+
13+
left_most_point: tuple[i32, i32] = points[0]
14+
point: tuple[i32, i32]
15+
for point in points:
16+
if point[0] < left_most_point[0]:
17+
left_most_point = point
18+
19+
return left_most_point
20+
21+
22+
def distance(p: tuple[i32, i32], q: tuple[i32, i32]) -> f64:
23+
# Function to calculate the Euclidean distance between two points
24+
x1: i32; y1: i32
25+
x2: i32; y2: i32
26+
27+
x1, y1 = p
28+
x2, y2 = q
29+
return f64((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5

0 commit comments

Comments
 (0)