0

I have a circle and have to check if a rectangle is inside it. I want to find the furthest corner of the rectangle and then check if it's inside the circle. I have a problem with the first part. I saw that people do:

dx = max(centerX - rectLeft, rectRight - centerX);
dy = max(centerY - rectTop, rectBottom - centerY);

But isn,t that wrong? For example i have center (5,6) topLeft (-2,-3) width=9 height=8

dx = max( 5 - -2, -2 + 9 - 5) = max(7, 2) which is 7 = OK 

but

dy = max( 6 - -3, -3 - 8 - 6) = max( 9, -17 ) = 9

which is not OK because 17 stands further than the center. Am I wrong or I should use fabs to make point 17 instead of -17?

2
  • That won't work. You need to find distance of each corner from center. Suppose top left corner is greater Y distance, but bottom right corner is greater X distance. You will take maxX and maxY which is greater than either.
    – stark
    Jun 28, 2016 at 18:43
  • What's the radius of your circle?
    – Bob__
    Jun 28, 2016 at 19:13

3 Answers 3

2

I think that the correct formulas should be:

dx = max(fabs(rectLeft - centerX), fabs(centerX - rectRight));
dy = max(fabs(rectTop - centerY), fabs(centerY - rectBottom));

Then you can check if:

dx*dx + dy*dy < r*r

If the sides of the rectangle are parallel to the axes this is enough to establish if the rectangle is inside, just consider the fact that with the first formulas we have set dx and dy to the values corresponding to the outermost vertex from the center of the circle, whichever it is.

1

The method of dx and dy mentioned above would not necessarily work. dx*dx + dy*dy < r*r is a necessary but not a sufficient condition to conclude that the rectangle is within the circle.

Instead think of the problem this way. The point on the rectangle that will be farthest from the center of the circle will be one of the vertices of the rectangle. For simplicity, imagine a circle (radius = r) at the origin (0, 0). Imagine a rectangle of height h, width w and the top vertex at (x, y). Then the remaining vertices will be (x, y-h), (x+w, y) and (x+w, y-h). Now to ensure that the rectangle is completely inside the circle, it suffices to check that each of the vertices of the rectangle is within the circle. The conditions are as follows:

x*x + y*y < r*r
(x+w)*(x+w) + y*y < r*r
x*x + (y-h)*(y-h) < r*r
(x+w)*(x+w) + (y-h)*(y-h) < r*r

If any one of the above is not true, then the rectangle is not completely inside the circle.

1
  • You don't need to calculate 4. Just the one with max x and y distance Jun 28, 2016 at 20:32
1

Yes; always use fabs() for distance problems because distance should always be positive.

1
  • 1
    I always will need squares actually, so I did this max( (centerX - rectLeft)*(centerX-rectLeft) , (centerX - rectRight)*(centerX-rectRight) )
    – Gigata
    Jun 28, 2016 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.