Skip to content

Commit

Permalink
Avoid division by 0 in perimeter_of_ellipse calculation
Browse files Browse the repository at this point in the history
While the initial cause of the division by 0 error mentioned in #44
should be fixed, now that this function will never be passed negative
numbers, it's still possible when both rx and ry are 0.

Add a special case to handle either rx or ry being 0, which returns
the "perimeter" of the ellipse as being the twice the length of the
line segment between the extremeties. (If both rx or ry are 0, the
resulting perimeter will be 0.)
  • Loading branch information
kepstin committed Jul 13, 2023
1 parent 89389a3 commit a053049
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions bbb_presentation_video/renderer/tldraw/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ class Decoration(Enum):

def perimeter_of_ellipse(rx: float, ry: float) -> float:
"""Find the approximate perimeter of an ellipse."""

# Handle degenerate case where the "ellipse" is actually a line or a point
if rx == 0:
return 2 * ry
elif ry == 0:
return 2 * rx

h = (rx - ry) ** 2 / (rx + ry) ** 2
return pi * (rx + ry) * (1 + (3 * h) / (10 + sqrt(4 - 3 * h)))

Expand Down

0 comments on commit a053049

Please sign in to comment.