I got an arbitrary shaped curve, enclosing some area. I would like to approximate the number of pixels that the curve is enclosing on an iPhone/iPad screen. How can I do so?
- A curve is defined as a successive x/y coordinates of points.
- A curve is closed.
- A curve is drawn by a user’s touches (touchesMoved method), and I
have no knowledge of what it looks like

I was thinking of somehow filling the closed curve with color, then calculating the number of pixels of this color in a screenshot of a screen. This means I need to know how to programmatically fill a closed curve with color.
Is there some other way that I’m not thinking of?
Thank you!
Let’s do this by creating a Quartz path enclosing your curve. Then we’ll create a bitmap context and fill the path in that context. Then we can examine the bitmap and count the pixels that were filled. We’ll wrap this all in a convenient function:
First we need to create the path:
Then we need to get the bounding box of the path.
CGPointcoordinates don’t have to be integers, but a bitmap has to have integer dimensions, so we’ll get an integral bounding box at least as big as the path’s bounding box:We also need to decide how wide (in bytes) to make the bitmap:
Now we can create the bitmap:
The bitmap is filled with black when it’s created. We’ll fill the path with white:
Now we’re done with the path so we can release it:
Next we’ll compute the area that was filled:
Now we’re done with the bitmap context, so we can release it:
Finally, we can return the area we computed:
Well, that was easy! But we have to write all those helper functions. Let’s start at the top. Creating the path is trivial:
Getting the integral bounding box of the path is also trivial:
To choose the bytes per row of the bitmap, we could just use width of the path’s bounding box. But I think Quartz likes to have bitmaps that are multiples of a nice power of two. I haven’t done any testing on this, so you might want to experiment. For now, we’ll round up the width to the next smallest multiple of 64:
We create the bitmap context with the computed sizes. We also need to translate the origin of the coordinate system. Why? Because the origin of the path’s bounding box might not be at (0, 0).
Finally, we need to write the helper that actually counts the filled pixels. We have to decide how we want to count pixels. Each pixel is represented by one unsigned 8-bit integer. A black pixel is 0. A white pixel is 255. The numbers in between are shades of gray. Quartz anti-aliases the edge of the path when it fills it using gray pixels. So we have to decide how to count those gray pixels.
One way is to define a threshold, like 128. Any pixel at or above the threshold counts as filled; the rest count as unfilled.
Another way is to count the gray pixels as partially filled, and add up that partial filling. So two exactly half-filled pixels get combined and count as a single, entirely-filled pixel. Let’s do it that way:
You can find all of the code bundled up in this gist.