I have a raster image with an oval inscribed in a rectangle with a given width and height (in pixels). I need an efficient (ultra-high resolution picture) algorithm to compute the exact number of pixels in the image. Currently I am dividing the rectangle into four parts, iterating over pixels in one of them and multiplying the result by 4, roughly speaking:
pixelCount = 0
frontier = width
newFrontier = frontier
For y in [0 .. height]
For x in [0 .. frontier]
If InsideOval(x, y)
newFrontier = MIN(x, newFrontier)
pixelCount += height – y
If x == frontier-1
frontier = newFrontier
pixelCount *= 4
Is there a more efficient way of doing it? There must be geometric formula... right?
I doubt it exists a geometric formula for that. The number of pixels depends on screen resolution and on the algorithm used for rasterization. As it happen with line rasterization in which Bresenham algorithm gives a different rasterization than DDA algorithm, that would happen with ellipses.
See for instance the different algorithms for rasterizing antialiased ellipses:
http://create.stephan-brumme.com/antialiased-circle/
Also a geometric formula should be rotation invariant while rasterization varies with rotation. Discrete is harder than continuous.