Area of circle within square

Started by
17 comments, last by cignox1 15 years, 8 months ago
Quote:Original post by AngleWyrm

  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


Wouldnt that fail if the circle intersected parallel edges of the square?
Quote:Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!
Advertisement
I think it would be easier to generalize this idea to triangles and make your rectangle out of two triangles. Then you have not so many cases of how the circle can intersect the rectangle. A sum of areas of triangles and integrals over a circle will give you the final result. Here I have drawn some possible cases one would need to deal with:
some cases
Or you could check for all possible cases of intersection between the square and the circle. In both cases you end up with a sum of triangle areas and circle portions. The area of the triangles is easy to calculate. Portions of a circle you can calculate either with an integral:
some cases
...or by:
some cases
Let me know if you need some help with the math.
That seems like your best solution. The area of a polygon is easy to find, as is the cord(?) areas.

Finding the actual inner polygon isn't that hard if you know how to intersect a segment with a circle.

Everything is better with Metal.

I think some of this stuff is a bit over my head, so for now at least, I've decided to simply supersample the edges, which gives me decent quality and great speed. It might not be as accurate as a strictly mathematical solution, but it's really quite fast, even with 16x16 samples (Though 4x4 usually seems to be good enough). Thanks for all the help! If I decide to go to something more advanced I expect I'll be asking again. ;)
Odd... this is the same sort of thing I've been trying to figure out all day. The question I posted elsewhere was this:

-------

Say you have a circle who's diameter is the same as the width of one quadrant. How would you find the area of this circle that falls into each quadrant if the circle's center point was not at the origin?

-------

Same basic problem. I'm good with the math I know, but my studies ended just before trig, so I'm clueless. Surely there's some formula which could take as input the x and y offset of the circle and assume the circle's diameter and the sides of each of the four boxes (quadrants) all equalled 1.
I just took a math class this past year and we totally did this stuff, I totally forget how to solve this, search on Google, there are steps and formulas to solve this.
-----------------------------------------"With the mind, anything is possible"
Here's what I came up with...

(Look at AngleWyrm's picture to see what I mean...)

Find the area of the triangle that falls within the square.

Find the area of the circle segment, which is the rounded portion outside the triangle.

The formula for finding the circle segment can be found here:

http://www.mathopenref.com/segmentarea.html

I'm going to verify all of this, so this reply might get editted.
Finding the intersection points of a line and a circle (2D) or sphere (3D) is useful in many situations. So I just wanted to post a function that finds those intersection points.

Intersection of a line and a circle
A and B are points of the line, P is the center of the circle.

int IntersectionLineSphere(vec *Q1, vec *Q2, vec A, vec B, vec P, float r){	vec pa = A - P;	vec ab = B - A;	float a = dot(ab,ab);	float b = dot(pa,ab);	float c = dot(pa,pa) - r*r;	float d = b/a;	float s = d*d-c/a;	if (s < 0.0f)		return 0; // sphere and line do not intersect	else		s = sqrtf(s);	float t1 = -d+s;	float t2 = -d-s;	*Q2 = A + ab * t1; // closer to B	*Q1 = A + ab * t2; // closer to A	int intersections = 0;	// intersection points within line segment?	if (t1 > 0.0f && t1 < 1.0f)		intersections++;	if (t2 > 0.0f && t2 < 1.0f)		intersections++;	// will be either 1 or 2	return intersections;}

The function is valid for 2D and 3D, and not optimized (const pointers to A and B). Having found the intersection points Q1 and Q2 one can find the "theta area" of the circle.

float SegmentOfCircle(vec Q1, vec Q2, vec P, float r){	vec s1 = Q1 - P;	vec s2 = Q2 - P;	float costheta = dot(s1,s2)/(length(s1)*length(s2));	return 0.5f*acosf(costheta)*r*r;}


float AreaOfTriangle(vec A, vec B, vec C){	vec s1 = B - A;	vec s2 = C - A;	vec cr = cross(s1,s2);	return 0.5f * length(cr);}

Now DeathRay2K found a good approximative solution, but the functions above would be the tools to find the correct area of the portion of a circle within a triangle (2 triangles = square). Of course it shouln't be done in real time. If someone is interested in finding this area I could post some code (~150 lines).
There is also the sampling option: you generate many random samples in a shape that contains both the square and the circle. Then you count how many samples are inside the square, how many inside the circle and how many inside both. From that you can find the overlapping area...

I don't really think this is what you need, but I though to put it here nonetheless, you never know....

This topic is closed to new replies.

Advertisement