Problem · Geometry
Rasterize a Circle with Integer Pixels
Learn this problemProblem statement
Rasterize a circle of integer radius centered at (0, 0) using the midpoint circle algorithm.
Start with x = 0, y = radius, and decision value d = 1 - radius. While x <= y:
- Add the eight symmetric coordinates
(±x, ±y)and(±y, ±x). - Increase
xby one. - If the old decision value was negative, set
d = d + 2x + 1using the newx. Otherwise, decreaseyby one and setd = d + 2(x - y) + 1using the new values.
A coordinate may be generated more than once when x = 0, x = y, or radius = 0. Return every unique generated coordinate exactly once, sorted first by ascending x and then by ascending y.
Function
drawCirclePixels(radius: int) → int[][]Examples
Example 1
radius = 0return = [[0,0]]The eight symmetric forms all name the origin, so deduplication leaves one pixel.
Example 2
radius = 2return = [[-2,-1],[-2,0],[-2,1],[-1,-2],[-1,2],[0,-2],[0,2],[1,-2],[1,2],[2,-1],[2,0],[2,1]]The midpoint states (0,2) and (1,2) generate the twelve unique symmetric pixels shown in sorted order.
Constraints
0 <= radius <= 10000.- The circle is centered at the origin.
- Use the exact midpoint recurrence and update order stated above.
- The returned coordinates must be unique and lexicographically sorted by
(x, y).