Skip to content

Instantly share code, notes, and snippets.

@smidm
Last active March 26, 2023 04:27
Show Gist options
  • Save smidm/b398312a13f60c24449a2c7533877dc0 to your computer and use it in GitHub Desktop.
Save smidm/b398312a13f60c24449a2c7533877dc0 to your computer and use it in GitHub Desktop.
Compute bounding box for an ellipse.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@drscotthawley
Copy link

drscotthawley commented Jun 29, 2021

This can return min values that are greater than their corresponding max values, e.g. when a negative angle is supplied. Recommend you wrap these lines in sorted(), e.g.

    [max_x, min_x] = sorted([x + major / 2 * np.cos(t) * np.cos(np.radians(angle_deg)) -
                      minor / 2 * np.sin(t) * np.sin(np.radians(angle_deg)) for t in (t, t + np.pi)],reverse=True)
    t = np.arctan(minor / 2 * 1. / np.tan(np.radians(angle_deg)) / (major / 2))
    [max_y, min_y] = sorted([y + minor / 2 * np.sin(t) * np.cos(np.radians(angle_deg)) +
                      major / 2 * np.cos(t) * np.sin(np.radians(angle_deg)) for t in (t, t + np.pi)],reverse=True)

@smidm
Copy link
Author

smidm commented Jul 12, 2021

Thanks @drscotthawley! I fixed the notebook with slightly modified code.

@drscotthawley
Copy link

@smidm what happens if angle_dim==0? I get NaNs with a warning:

RuntimeWarning: invalid value encountered in double_scalars
  t = np.arctan(-minor / 2 * np.tan(np.radians(angle_deg)) / (major / 2))

To avoid this, I just added a little fudge factor above that line:

    if 0==angle_deg: angle_deg = 1e-8      # slight fudge to avoid division by zero

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment