Results 1 to 10 of 10

Thread: Find the amount a circle covers a rectangle

  1. #1

    Thread Starter
    Fanatic Member THEROB's Avatar
    Join Date
    Oct 2000
    Location
    I'm cold and there are wolves after me
    Posts
    575

    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
    Attached Images Attached Images  
    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]

  2. #2
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    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
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  3. #3

    Thread Starter
    Fanatic Member THEROB's Avatar
    Join Date
    Oct 2000
    Location
    I'm cold and there are wolves after me
    Posts
    575

    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
    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]

  4. #4
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    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...
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  5. #5
    Hyperactive Member metalmidget's Avatar
    Join Date
    Mar 2007
    Location
    Melbourne, Australia
    Posts
    342

    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.

  6. #6

    Thread Starter
    Fanatic Member THEROB's Avatar
    Join Date
    Oct 2000
    Location
    I'm cold and there are wolves after me
    Posts
    575

    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
    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]

  7. #7
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    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.
    .
    Attached Images Attached Images  
    Last edited by VBAhack; Oct 17th, 2007 at 12:22 PM.

  8. #8

    Thread Starter
    Fanatic Member THEROB's Avatar
    Join Date
    Oct 2000
    Location
    I'm cold and there are wolves after me
    Posts
    575

    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
    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]

  9. #9
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    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%.
    Attached Images Attached Images  
    Last edited by VBAhack; Oct 23rd, 2007 at 10:48 PM.

  10. #10

    Thread Starter
    Fanatic Member THEROB's Avatar
    Join Date
    Oct 2000
    Location
    I'm cold and there are wolves after me
    Posts
    575

    Re: Find the amount a circle covers a rectangle

    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]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width