# Partial Integral of an ellipse

this is my first question on stack exchange so please bear with me.

I am trying to generate a synthetic image of an ellipse in Matlab where each pixel is shaded according to how much of that pixel is contained within the ellipse. I had a similar problem for a circle and tackled it by simply integrating the area of the circle contained in the pixel, that is:

$\textbf{I}(y,x) = \int_{x_{left}}^{x_{right}}\left(\pm \sqrt{r^2-(y-y_c)^2}+x_c\right)dx-A_{extra}$

where $[x_c,y_c]$ is the center location of the circle, $r$ is the radius of the circle, $A_{extra}$ is the extra area between the pixel being considered and the line $y=x_c$, and the plus or minus is determined by whether the upper or lower half of the circle was being considered. This worked very well and produced exactly what I wanted.

I am now trying to generalize this to an ellipse. I currently have an ellipse parameterized as

$Ax^2+Bxy+Cy^2+Dx+Fy+G=0$

that I would like to generate this image for but I am running into some issues. The first issue that I have is that $A-G$ here are parameterized for $(x,y)$ in units of distance and also assume that the coordinate system is based in the center of the image. I tried to get around this by determining the following:

$x_c = \frac{2CD-BF}{B^2-4AC}+imcenter_x$, $y_c = \frac{2AF-BD}{B^2-4AC}+imcenter_y$

$a = dist2pix\sqrt{\frac{2\left[AF^2+CD^2-BDF+G\left(B^2-4AC\right)\right]}{\left(B^2-4AC\right)\left[\sqrt{(A-C)^2+B^2}-A-C\right]}}$ , $b = dist2pix\sqrt{\frac{2\left[AF^2+CD^2-BDF+G\left(B^2-4AC\right)\right]}{\left(B^2-4AC\right)\left[-\sqrt{(A-C)^2+B^2}-A-C\right]}}$

$\phi=\left\{\begin{array}{cc}0 &B=0\text{ and }A<C\\\pi/2 &B=0\text{ and }A>C\\\frac{1}{2}\cot^{-1}{\left(\frac{A-C}{B}\right)}&B\neq0\text{ and }A<C \\\frac{\pi}{2}+\frac{1}{2}\cot^{-1}{\left(\frac{A-C}{B}\right)}&B\neq0\text{ and }A>C \end{array}\right.$

where $imcenter_x$ and $imcenter_y$ are the x and y coordinates of the center of the image, $dist2pix$ is a conversion factor to go from distance units to pixels, $a$ and $b$ are the semi-major and semi-minor distances, and $\phi$ is the rotation from the x-axis to the semi-axis along which $a$ lies. I then reparamaterize into $A_2-G_2$ by expanding

$\frac{\left((x-x_c)\cos(\phi)-(y-y_c)\sin(\phi)\right)^2}{a^2}+\frac{\left((x-x_c)\sin(\phi)+(y-y_c)\cos(\phi)\right)^2}{b^2}=1$

and then rearranging into the form from above. My first question is, is this a valid way to adjust the parameters to reflect units of pixels and be centered at the upper left corner of the image as I desire. (note these parameterizations were taken from http://mathworld.wolfram.com/Ellipse.html)

My next question is what is the best way to perform this integration after getting the right parameterization. I have tried using a similar method to what I used for the circle but I do not believe this is valid due to the fact that there may be some integrals where both the positive and negative halves would need to be considered if the ellipse were extra elongated. I feel like a similar problem would occur if I tried to convert into polar coordinates as well because I believe that $r$ would still be of order 2 leading to a square root.

Any suggestions would be greatly appreciated as would any tips on formatting conventions or the like.

Andrew