Skip to content

Commit

Permalink
refactor is_convex_polygon_2d() function
Browse files Browse the repository at this point in the history
  • Loading branch information
mozman committed Jan 18, 2024
1 parent 18f7ab7 commit 7ab9a5c
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/ezdxf/math/construct2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,8 @@ def is_convex_polygon_2d(polygon: list[Vec2], *, strict=False, epsilon=1e-6) ->
if len(polygon) < 3:
return False

signs: list[int] = []
global_sign: int = 0
current_sign: int = 0
prev = polygon[-1]
prev_prev = polygon[-2]
for vertex in polygon:
Expand All @@ -377,17 +378,18 @@ def is_convex_polygon_2d(polygon: list[Vec2], *, strict=False, epsilon=1e-6) ->

det = (prev - vertex).det(prev_prev - prev)
if abs(det) >= epsilon:
signs.append(-1 if det < 0.0 else +1)
current_sign = -1 if det < 0.0 else +1
if not global_sign:
global_sign = current_sign
# do all determinants have the same sign?
if global_sign != current_sign:
return False
elif strict: # collinear vertices
return False
return False

prev_prev = prev
prev = vertex

if signs:
# Do all determinants have the same sign?
m = signs[0]
return all(m == s for s in signs)
return False
return bool(global_sign)


def is_axes_aligned_rectangle_2d(points: list[Vec2]) -> bool:
Expand Down

0 comments on commit 7ab9a5c

Please sign in to comment.