MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / polar_angle_key

Function polar_angle_key

geometry/graham_scan.py:158–173  ·  view source on GitHub ↗

Key function for sorting points by polar angle relative to min_point. Points are sorted counter-clockwise. When two points have the same angle, the farther point comes first (we'll remove duplicates later).

(point: Point)

Source from the content-addressed store, hash-verified

156 return []
157
158 def polar_angle_key(point: Point) -> tuple[float, float, float]:
159 """
160 Key function for sorting points by polar angle relative to min_point.
161
162 Points are sorted counter-clockwise. When two points have the same angle,
163 the farther point comes first (we'll remove duplicates later).
164 """
165 # We use a dummy third point (min_point itself) to calculate relative angles
166 # Instead, we'll compute the angle between points
167 dx = point.x - min_point.x
168 dy = point.y - min_point.y
169
170 # Use atan2 for angle, but we can also use cross product for comparison
171 # For sorting, we compare orientations between consecutive points
172 distance = min_point.euclidean_distance(point)
173 return (dx, dy, -distance) # Negative distance to sort farther points first
174
175 # Sort by polar angle using a comparison based on cross product
176 def compare_points(point_a: Point, point_b: Point) -> int:

Callers

nothing calls this directly

Calls 1

euclidean_distanceMethod · 0.80

Tested by

no test coverage detected