Is rectange fully inside another rectangles

This is a discussion on Is rectange fully inside another rectangles within the Game Programming forums, part of the General Programming Boards category; I have two rectagle. User inputs the co-ordiates of both the rectagle. Let us say, co-ordinates of big rectangle are ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    175

    Is rectange fully inside another rectangles

    I have two rectagle. User inputs the co-ordiates of both the rectagle.

    Let us say, co-ordinates of big rectangle are

    Bx1,By1,Bx2,By2,Bx3,By3,Bx4 and By4

    and co-ordinates of small rectangle are

    Sx1,Sy1,Sx2,Sy2,Sx3,Sy3,Sx4 and Sy4

    Can anybody help me writing "C" program to check, whether small rectagle is fully inside the big rectangle?

    It is simple if you use your own number of "if" and "else". I am looking at program that use minimum number of "if" condition.

    Assume that, x increses from left to right and y increases from botton to top.

    Thanks,
    tiger

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    First, you only need two points to define a rectangle (unless it's angled). Take a look at it and you'll see that some of the points (corners) in the rectangle have the same X or Y coordinates.
    Code:
    (X1, Y1)
       -----------------+
       |                 |
       |                 |
       |                 |
       |                 |
       |                 |
       |                 |
       +-----------------
                      (X2, Y2)
    Assuming Point 1 is always the upper left corner, and Point 2 is always the bottom right corner, this will check if Rectangle 1 is completely inside Rectangle 2:
    Code:
    if((R1.X1 >= R2.X1) && (R1.X2 <= R2.X2))
    {
       if((R1.Y1 >= R2.Y1) && (R1.Y2 <= R2.Y2))
       {
          ...
       }
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. call to realloc() inside a function: memory problem
    By simone.marras in forum C Programming
    Replies: 15
    Last Post: 11-30-2008, 10:01 AM
  2. variables when declared inside or outside a function
    By jas_atwal in forum C Programming
    Replies: 6
    Last Post: 12-14-2007, 02:42 PM
  3. Still battling with Copy Control
    By Mario F. in forum C++ Programming
    Replies: 9
    Last Post: 06-23-2006, 09:04 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Difference Of Rectangles
    By SMurf in forum C Programming
    Replies: 2
    Last Post: 09-14-2005, 02:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21