snippetMinor
How can I efficiently plot the inside of an ellipse?
Viewed 0 times
canefficientlytheellipsehowplotinside
Problem
I am given the the coefficients for an ellipse in the form:
$$Ax^2 + Bxy + Cy^2 + Dx + Ey + F=0.$$
I have a 2 dimensional array of point objects (in Python) called "pixels", and I need to give each point a "count" if they lie inside the ellipse.
I initially tried iterating through every point with a loop like:
But this is taking way too long because I have several thousand ellipses I need to do this for. Is there a way I can efficiently do this?
(If this is not the right place to ask this question, I apologize.)
Edit:
(I don't have enough reputation to comment).
The Python is mostly irrelevant to my question at hand. I just want a better algorithm instead of iterating through every point.
In order to determine whether something is inside an ellipse, I am actually using this test https://math.stackexchange.com/questions/817130/how-to-determine-if-arbitrary-point-lies-inside-or-outside-a-conic.
The user Evil suggested using Axis Aligned Bounding Box. Does this mean finding the box which bounds an ellipse, and then testing only those points inside the box? That would seem a lot more efficient.
Greybeard, I am not sure what the Bresenham algorithm is, but I will take a look.
$$Ax^2 + Bxy + Cy^2 + Dx + Ey + F=0.$$
I have a 2 dimensional array of point objects (in Python) called "pixels", and I need to give each point a "count" if they lie inside the ellipse.
I initially tried iterating through every point with a loop like:
for row in pixels:
for pixel in rows:
x=point.get(pixel)[0]
z=point.get(pixel)[1]
if a*(x)**2 + b*(x*z) + c*(z)**2 + d*(x) + e*(z) + f >0:
pixel.addpcount()But this is taking way too long because I have several thousand ellipses I need to do this for. Is there a way I can efficiently do this?
(If this is not the right place to ask this question, I apologize.)
Edit:
(I don't have enough reputation to comment).
The Python is mostly irrelevant to my question at hand. I just want a better algorithm instead of iterating through every point.
In order to determine whether something is inside an ellipse, I am actually using this test https://math.stackexchange.com/questions/817130/how-to-determine-if-arbitrary-point-lies-inside-or-outside-a-conic.
The user Evil suggested using Axis Aligned Bounding Box. Does this mean finding the box which bounds an ellipse, and then testing only those points inside the box? That would seem a lot more efficient.
Greybeard, I am not sure what the Bresenham algorithm is, but I will take a look.
Solution
Following @Evil's comment, I suggest two steps. For each ellipse $E$:
(1) Computing a bounding box $B$ for $E$.
(2) For each row of pixels in $B$, walk left-to-right until a pixel $p_1$
is discovered to be inside $E$.
Then walk right-to-left until $p_2$ is in $E$.
Then all the pixels from $p_1$ to $p_2$ are in $E$
(because of convexity).
The most difficult step is computing a bounding box for the ellipse.
You likely don't need it exactly.
Here are two sources:
(1) Computing a bounding box $B$ for $E$.
(2) For each row of pixels in $B$, walk left-to-right until a pixel $p_1$
is discovered to be inside $E$.
Then walk right-to-left until $p_2$ is in $E$.
Then all the pixels from $p_1$ to $p_2$ are in $E$
(because of convexity).
The most difficult step is computing a bounding box for the ellipse.
You likely don't need it exactly.
Here are two sources:
- "How do you calculate the axis-aligned bounding box of an ellipse?."
- "How do you find a tight-fitting, axis-aligned, bounding box of a rotated ellipse in AS3?."
Context
StackExchange Computer Science Q#87104, answer score: 2
Revisions (0)
No revisions yet.