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
My secretary hopes that I will pay her, her landlord hopes that she will produce some rent, the Electricity Board hopes that he will settle their bill, and so on. I find it a wonderfully optimistic way of life. [Dirk Gently]
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.
Thanks for the reply - do you have any code to do this?
I will try it myself as well - thanks again.
Rob
My secretary hopes that I will pay her, her landlord hopes that she will produce some rent, the Electricity Board hopes that he will settle their bill, and so on. I find it a wonderfully optimistic way of life. [Dirk Gently]
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...
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.
Any code for this at all - I still haven't managed to write it.
Thanks
Robert
My secretary hopes that I will pay her, her landlord hopes that she will produce some rent, the Electricity Board hopes that he will settle their bill, and so on. I find it a wonderfully optimistic way of life. [Dirk Gently]
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.
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)
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
My secretary hopes that I will pay her, her landlord hopes that she will produce some rent, the Electricity Board hopes that he will settle their bill, and so on. I find it a wonderfully optimistic way of life. [Dirk Gently]
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%.
Last edited by VBAhack; Oct 23rd, 2007 at 10:48 PM.
Brillant - thank you so much for the reply. It works a treat.
Thanks again.
Robert
My secretary hopes that I will pay her, her landlord hopes that she will produce some rent, the Electricity Board hopes that he will settle their bill, and so on. I find it a wonderfully optimistic way of life. [Dirk Gently]