• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4686
  • Last Modified:

Rectangle intersection

hi all,

how to calculate the intersection of 2 rectangle?

a rectangle is the following:

Rectangle makeRectangle (Point lowerLeft, Point upperRight) {
      
      Rectangle r;

      r.pt1 = lowerLeft;
      r.pt2 = upperRight;
      return r;
}

and Point is the following:

Point makePoint(int x, int y) {
    Point p;
    p.x = x;
    p.y = y;
    return p;
}


And i want the know how to make:

Rectangle intersection(Rectangle r1, Rectangle r2)  

this should return the intersection of the 2 rectangles
anyone can help me plz?

i am not good in math
0
kimos123
Asked:
kimos123
  • 2
  • 2
  • 2
  • +2
1 Solution
 
keteracelCommented:
Rectangle intersection (Rectangle r1, Rectangle r2)
   {
    int intLeft = 0, intBottom = 0, intRight = 0, intTop = 0;
    int emptyIntersection = 0;

    // check on horizontal overlap
    if (r1.pt1.x <= r2.pt1.x)
     {
      intLeft = r2.pt1.x;

      if (r1.pt2.x < r2.pt1.x)
        emptyIntersection = 1;
      else
        intRight = (r1.pt2.x < r2.pt2.x)?r1.pt2.x:r2.pt2.x;
     }
    else
     {
      intLeft = r1.pt1.x;
      if (r2.pt2.x< r1.pt1.x)
        emptyIntersection = 1;
      else
        intRight = (r1.pt2.x < r2.pt2.x)?r1.pt2.x:r2.pt2.x;
     }

    // check on vertical overlap
    if (r1.pt1.y<= r2.pt1.y)
     {
      intBottom = r2.pt1.y;
      if (r1Top < r2.left)
        emptyIntersection = 1;
      else
        intTop = (r1.pt2.y < r2.pt2.y)?r1.pt2.y:r2.pt2.y;
     }
    else
     {
      intBottom = r1.pt1.y;
      if (r2.pt2.y< r1.pt1.x)
        emptyIntersection = 1;
      else
        intTop = (r1.pt2.y < r2.pt2.y)?r1.pt2.y:r2.pt2.y;
     }

    if (emptyIntersection)
      intLeft = intBottom = intTop = intRight = 0;

    return makeRectangle(makePoint(intLeft,intBottom),makePoint(intRight, intTop));
   }
 
0
 
keteracelCommented:
oops... couple of wrong things....

 Rectangle intersection (Rectangle r1, Rectangle r2)
   {
    int intLeft = 0, intBottom = 0, intRight = 0, intTop = 0;
    int emptyIntersection = 0;

    // check on horizontal overlap
    if (r1.pt1.x <= r2.pt1.x)
     {
      intLeft = r2.pt1.x;

      if (r1.pt2.x < r2.pt1.x)
        emptyIntersection = 1;
      else
        intRight = (r1.pt2.x < r2.pt2.x)?r1.pt2.x:r2.pt2.x;
     }
    else
     {
      intLeft = r1.pt1.x;
      if (r2.pt2.x< r1.pt1.x)
        emptyIntersection = 1;
      else
        intRight = (r1.pt2.x < r2.pt2.x)?r1.pt2.x:r2.pt2.x;
     }

    // check on vertical overlap
    if (r1.pt1.y<= r2.pt1.y)
     {
      intBottom = r2.pt1.y;
      if (r1.pt2.y < r2.pt1.x)
        emptyIntersection = 1;
      else
        intTop = (r1.pt2.y < r2.pt2.y)?r1.pt2.y:r2.pt2.y;
     }
    else
     {
      intBottom = r1.pt1.y;
      if (r2.pt2.y< r1.pt1.x)
        emptyIntersection = 1;
      else
        intTop = (r1.pt2.y < r2.pt2.y)?r1.pt2.y:r2.pt2.y;
     }

    if (emptyIntersection)
      intLeft = intBottom = intTop = intRight = 0;

    return makeRectangle(makePoint(intLeft,intBottom),makePoint(intRight, intTop));
   }
 
0
 
Jaime OlivaresSoftware ArchitectCommented:
First of all, you must ensure your rentangles are normalized, that is pt1.x < pt2.x and pt1.y < pt2.y

Second, you can make a helper macro

#define BETWEEN(n, nmin, nmax) ((n>nmin)&&(n>nmax))

Here is the function, writting from my memory please check carefully, drawing in a paper all possible cases

Rectangle insersection(Rectangle r1, Rectangle r2)
{
   CRectangle r;   // I expect to be empty

/* Test if rectangle overlaps, if not return empty Rectangle */

if((BETWEEN(r2.pt1.x,r1.pt1.x,r1.pt2.x) && BETWEEN(r2.pt1.y,r1.pt1.y,r1.pt2.y) ||  
   (BETWEEN(r2.pt2.x,r1.pt1.x,r1.pt2.x) && BETWEEN(r2.pt2.y,r1.pt1.y,r1.pt2.y) ||  
   (BETWEEN(r2.pt1.x,r1.pt1.x,r1.pt2.x) && BETWEEN(r2.pt2.y,r1.pt1.y,r1.pt2.y) ||  
   (BETWEEN(r2.pt2.x,r1.pt1.x,r1.pt2.x) && BETWEEN(r2.pt1.y,r1.pt1.y,r1.pt2.y) ) {

   r.MakeRectangle(
      MakePoint(max(r1.pt1.x, r2.pt1.x), max(r1.pt1.y, r2.pt1.y),
      MakePoint(min(r1.pt2.x, r2.pt2.x), max(r1.pt2.y, r2.pt2.y),
   );
}

return r;
}
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Jaime OlivaresSoftware ArchitectCommented:
Oops! Too late. Hope still to be useful.
0
 
tzxie2000Commented:
may be simple

Rectangle intersection (Rectangle r1, Rectangle r2)
{
   int x[4],y[4];
   int i,j,temp;

   x[0]=r1.pt1.x;
   x[1]=r1.pt2.x;
   x[2]=r2.pt1.x;
   x[3]=r2.pt2.x;

   y[0]=r1.pt1.y;
   y[1]=r1.pt2.y;
   y[2]=r2.pt1.y;
   y[3]=r2.pt2.y;

   for(i=0;i<3;i--)
     for(j=0;j<3-i;j++)
     {
        if(x[j]>x[j+1])
        {
           temp=x[j];
           x[j]=x[j+1];
           x[j+1]=temp;
        }
        if(y[j]>y[j+1])
        {
           temp=y[j];
           y[j]=y[j+1];
           y[j+1]=temp;
        }
     }
  return makeRectangle(makePoint(x[1],y[1]),makePoint(x[2],y[2]));
}
0
 
kimos123Author Commented:
ohh sorry guys i didnt refeshed my page and saw only the first answer of keteracel   and i accepted it becouse its good.   i should had spiltted the points...

anyways thanks for the reactions  but one thing i still dont know what is intersection of 2 rectangels  ?   can someone explane it to me  in english not in C plz
the C code is working thanks to u

thanks in advance
0
 
tzxie2000Commented:
you can use two book and place them together but not just one on another. the part with two book is intersection
0
 
kimos123Author Commented:
ohh i got it  thanks alot
0
 
gelgitdevCommented:
return (x1 < x2 + width2) && (y1 < y2 + height2) &&
            (x1 + width1 > x2) && (y1 + height1 > y2);
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now