Find the Points Where a Line Segment Intercepts an Angled Ellipse (in JavaScript / TypeScript)
I ran into this question while working on perfect-arrows, a TypeScript library for drawing arrows between points, rectangles, and other shapes. While the question didn’t seem to obscure, I found it very hard to find an answer. The math itself is beyond me, but I was able to adapt some earlier work from another language.
For the sake of paying it forward (and saving the next sucker—maybe you!—some headaches), here’s the code I came up with:
The important function there is getEllipseSegmentIntersections
. It’s written in TypeScript, but feel free to pull those types out if you’d like to use it in a JavaScript project.
Depending on your project, you may need to sort the returned points in clockwise or counterclockwise order, relative to a center. You can use the points’ cross product for this, and I’ve included a test (pointsAreClockwise
) as somewhere to start with that.
Good luck!