1 Attachment(s)
Find the amount a circle covers a rectangle
Hi – I’ve got a problem I need to figure out. Given a circle of known centre and Radius. How can you find what percentage of a rectangle is covered by the circle. The rectangle dimensions and location is also known. If it makes it easier the rectangle can be square.
In the picture A is 0% covered, B is 100% and C is about 25%. Is there any equation to work this out.
Thanks
Robert
Re: Find the amount a circle covers a rectangle
Yes, but you'd have to integrate.
Put x and y axes in at the centre of the circle. Integrate y between x1 and x2, where x1 is the corner of the square that is in the circle and x2 is the smaller of either the other corner of the square or the intercept of the square and circle. You know the value of y at each of these points because you know the radius of the circle, so you can express in terms of x.
Then subtract off the rectangle between the x axis and the base of the square.
Does that make sense?
zaza
Re: Find the amount a circle covers a rectangle
Thanks for the reply - do you have any code to do this?
I will try it myself as well - thanks again.
Rob
Re: Find the amount a circle covers a rectangle
Nope. Coding to work out how much of a rectangle overlaps a circle is not something I have massive use for on a daily basis. But it can't be that hard. Post something if you have probs and I'm sure the wealth of talent on VBF can figure something out...
Re: Find the amount a circle covers a rectangle
lol, what zaza said makes perfect sense to me, and having read it I could do it, but that would never have occured to me to do it that way... Not for a while anyway.
I always find in maths that actually doing it is easy, it's knowing what to do that's the hard bit.
Re: Find the amount a circle covers a rectangle
Any code for this at all - I still haven't managed to write it.
Thanks
Robert
1 Attachment(s)
Re: Find the amount a circle covers a rectangle
As zaza said, integrate. The problem boils down to finding the area between two curves, in this case between y2 = r2 - x2 and y = y1 with limits of integration being x1 and x2. Here's a picture to illustrate and a link to describe the methodology. The code would be quite simple once you understand the math. :)
http://www.mathwords.com/a/area_between_curves.htm
Edit: This link will help evaluate the integral. Also, you'll need to keep track what part of the circle you are in, y = sqrt(r2-x2) or y = - sqrt(r2-x2)
http://en.wikipedia.org/wiki/List_of...onal_functions
Actually, for the illustration, it would be less work to find the area between y2 = r2 - x2 and y = y2.
.
Re: Find the amount a circle covers a rectangle
I am still very lost - following that link I couldn't find any conversion for y = sqrt(r2-x2). I have searched through the net trying to find anything to help - nothing. I have even resorted to reading my old school books - still nothing. Can you tell me what y = sqrt(r2-x2) converts to. I am sure I can get it from there.
Thanks for the reply.
Robert
1 Attachment(s)
Re: Find the amount a circle covers a rectangle
Quote:
Originally Posted by THEROB
I am still very lost - following that link I couldn't find any conversion for y = sqrt(r2-x2)
The integral for y = sqrt(r2 - x2) is there, you have to scroll down.
Integral (sqrt(r2 - x2)) = 1/2[ x*sqrt(r2 - x2) + r2*arcsin(x/r) ]
Let me give a simple specific example. Refer to the attached picture. The circle origin is at (0, 0) and r = 5. The yellow square has corner coordinates (2, -4), (2, -5), (3, -4), (3, -5) and has an area of 1.
The easiest way is to calculate the total area between the curve and the x-axis between x = 3 and x = 2 (i.e. the cross hatched section) and then subtract the area from the x-axis to y = -4 (i.e. the purple section)
Step 1: Evaluate integral at x = 3. Result is 14.04376386.
Step 2: Evaluate integral at x = 2. Result is 9.726536271
Step 3: Subtract: 14.04376386 - 9.726536271 = 4.317227589. This is the cross hatched area.
Step 4: Subtract purple section: 4.317227589 - 4*1 = 0.317227589. This is the area of the square that is contained with the circle. Since the square has an area of 1, this also represents the percentage of the yellow square covered by the circle, or 31.7%.
Re: Find the amount a circle covers a rectangle
Brillant - thank you so much for the reply. It works a treat.
Thanks again.
Robert