FastPrepRasterize a Circle with Integer Pixels
Problem · Geometry

Rasterize a Circle with Integer Pixels

Learn this problem
MediumPure Storage logoPure StorageFULLTIMEONSITE INTERVIEW

Problem 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:

  1. Add the eight symmetric coordinates (±x, ±y) and (±y, ±x).
  2. Increase x by one.
  3. If the old decision value was negative, set d = d + 2x + 1 using the new x. Otherwise, decrease y by one and set d = d + 2(x - y) + 1 using 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).

More Pure Storage problems

drafts saved locally
public int[][] drawCirclePixels(int radius) {
    // Write your code here.
}
radius0
expected[[0,0]]
checking account