Jump to content
  • Advertisement
Sign in to follow this  
DeathRay2K

Area of circle within square

This topic is 3791 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to find the area of the portion of a circle within a square. I'm doing this for a non-realtime rendering of 2D circles; I plan to use the area to calculate the antialiasing. Unfortunately I really have no idea how to go about this, and the closest thing I've been able to find with Google is squares fully with circles, which is of course no help at all. Any help would be much appreciated!

Share this post


Link to post
Share on other sites
Advertisement
assuming im correctly interpreting your question..

area of a circle is pi*r^2
area of a square is w^2 where w = width and w = 2*r

so square = 4r^2
and circle is pi*r^2 so the ratio of a squares area to the area of the circle inside of it is (4r^2)/(pi*r^2) or 4/pi

Share this post


Link to post
Share on other sites
I don't want the ratio of areas, I want to figure out how much of the circle is within the square.
This image shows what I mean; I would want to figure out the area of the darker region in this example:

You see, I don't want to figure out how much area a circle takes up in the square formed by its extremities, I want to know how much some circle overlaps some square, where the two have no relation at all.

Share this post


Link to post
Share on other sites
Guest Lexdysic
How comfortable are you with integrals?

I would create a function to represent the circle and clamp it to the range [0,1] then integrate that function over the domain from [0,1] and the result would be exactly the area of the circle in that range. Let me work something out to start you off.

Share this post


Link to post
Share on other sites
From your picture you can calculate the area of the overlapping square and circle like this:

* You know that every angle inside of a square is 90 degrees.
* You need to find the distance from the corner of the square that is inside the circle to the edge of the circle -- this will be your radius

So:

angle = 90 deg (t)
radius = point_2 - point_1 (r)
overlapping area = 0.5t*r^2

NOTE: This is in radians.

radians = pi / 180 degrees

Share this post


Link to post
Share on other sites
The circle isn't necessarily (And certainly will very rarely be) aligned in any fashion with the square, so I can't simply figure out the radius around a corner, because the circle is unlikely to be perfectly aligned with a corner, or simply overlapping one corner.

I have little experience with integrals, but I am certainly happy to learn and attempt them if necessary.

Share this post


Link to post
Share on other sites
Guest Lexdysic
So here is what I think may work:

function for a circle centered at (x0, y0) with radius r


f(x) = +-sqrt( r^2 - (x-x0)^2 ) - y0 and 0 where invalid


You can then integrate this function over the range [0,1]. But since you only want the parts in the square you would clamp f(x) to the range [0,1].

something like this:


area = integral( 0, 1, clamp( f(x), 0, 1 ) )


Though I think clamp needs a little work. it may be a bit different than that. Its pretty close I imagine.

Additions:
Basically it assumes that the lower left corner of the square is at the origin and the sides of length one. You can integrate over the x in the range zero to one.

The tricky part will to make sure the integral does not count any part of the circle that is completely above or below the pixel. This is what I was trying to accomplish with the clamp. Basically if the bottom edge of the circle is greater than one or the top edge of the circle is lower than zero for any given value of x, you will need to set the function to zero.

[Edited by - Lexdysic on July 28, 2008 12:12:51 AM]

Share this post


Link to post
Share on other sites
How accurate and how fast does it have to be? You want a numerical method?

Else, you can use some rendering callbacks by rendering both, and give you the number of pixels overlapping.

Otherwise it will be quite complicated. My intuition says you'll have to clip the circle using the rectangle's plane, but I wouldn't know exactly how to derive the area for the clipped region, which would rely on integral calculus.

Share this post


Link to post
Share on other sites
He mentions anti-aliasing, so I think he's looking for sub-pixel precision here.

The easiest solution may simply be to super-sample pixels on the edge of the mathematical circle, choosing enough samples to yield acceptable visual quality. Four or nine sample grids, 2x2 and 3x3 respectively, per pixel might be accurate enough for your needs. If not, you can subdivide greater powers of two, and trivially accept/reject entire blocks of sub-samples... Not sure how this compares speed-wise to a pure numerical function, but its worth investigating. I would think that an 16 sample, optimized test would be sufficiently fast and accurate for most needs, and it would probably lend itself well to parallelism and running on SSE or similar instruction sets.

Share this post


Link to post
Share on other sites

  1. Get area of circle
  2. Multiply by fraction of circle covered by chord ( = area of pie slice)
  3. Subtract area of isosceles triangle ( = area of circular segment)
  4. Add area of right triangle


[Edited by - AngleWyrm on July 28, 2008 5:30:50 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!