Skip to content

Commit

Permalink
fix(polygon): Add extra checks to avoid ZeroDivisionErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswmackey authored and Chris Mackey committed Mar 23, 2023
1 parent 649f787 commit 14f43fa
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
5 changes: 5 additions & 0 deletions ladybug_geometry/geometry2d/polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,17 +606,22 @@ def offset(self, distance, check_intersection=False):
# loop through the vertices and get the new offset vectors
init_verts = self._vertices if not self.is_clockwise \
else list(reversed(self._vertices))
init_verts = [pt for i, pt in enumerate(init_verts) if pt != init_verts[i - 1]]
move_vecs, max_i = [], len(init_verts) - 1
for i, pt in enumerate(init_verts):
v1 = init_verts[i - 1] - pt
end_i = i + 1 if i != max_i else 0
v2 = init_verts[end_i] - pt
if not self.is_clockwise:
ang = v1.angle_clockwise(v2) / 2
if ang == 0:
ang = math.pi / 2
m_vec = v1.rotate(-ang).normalize()
m_dist = distance / math.sin(ang)
else:
ang = v1.angle_counterclockwise(v2) / 2
if ang == 0:
ang = math.pi / 2
m_vec = v1.rotate(ang).normalize()
m_dist = -distance / math.sin(ang)
m_vec = m_vec * m_dist
Expand Down
2 changes: 1 addition & 1 deletion ladybug_geometry/intersection2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def intersect_line2d_infinite(line_ray_a, line_ray_b):
Args:
line_ray_a: A LineSegment2D or Ray2D object.
line_ray_b: ALineSegment2D or Ray2D that will be extended infinitely
line_ray_b: A LineSegment2D or Ray2D that will be extended infinitely
for intersection.
Returns:
Expand Down

0 comments on commit 14f43fa

Please sign in to comment.