# How can I efficiently plot the inside of an ellipse?

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:

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:


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.

• Have you considered reversing the problem? For each ellipse calculate Axis Aligned Bounding Box and check only for these points. Alternatively you can transform ellipse to circle to speed up tests (but the transform is rather costly, so it depends on orientation of ellipses and the size), could you give more info? For the tests: Point inside ellipse. In the current form your question seems to be Python dependent, which renders it off-topic here. But I assume that you are looking for better algorithm, right? – Evil Jan 22 '18 at 5:11
• Note that Python is offtopic here. – Raphael Jan 22 '18 at 6:03
• Can you adapt the Bresenham algorithm to draw one quarter of an ellipse? One eighth? – greybeard Jan 22 '18 at 7:32
• This seems to be a completely reasonable computational geometry question. The Python is irrelevant and the code fragment can be read perfectly well as pseudocode. – David Richerby Jan 22 '18 at 10:43
• I missed - twice - that you have to "do something" for every pixel inside: if/as the complexity of checking isInside is constant, operating on every pixel inside a (tight) bounding box is asymptotically optimal, the ellipse covering more than three quarters of it. Are you interested in minimising/replacing isInside checks? – greybeard Jan 23 '18 at 14:51

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:

• This is a very nice approach, especially the walking left then walking right. – Jbag1212 Jan 23 '18 at 17:56