16

How I can substract one CGRect from another? I want the result R1 - R2 to be the largest subrectangle of R1 that does not intersect R2.

Example 1:

+----------------------------------+
| +--------+                       |
| |   R2   |                       |
| |        |                       |
| +--------+      R1               |
|                                  |
|                                  |
|                                  |
+----------------------------------+

R3 = CGRectSubstract(R2,R1);

            +----------------------+
            |                      |
            |                      |
            |                      |
            |          R3          |
            |                      |
            |                      |
            |                      |
            +----------------------+

Example 2:

+-----------------------+----------+
|                       |          |
|                       |    R2    |
|                       |          |
|                 R1    +----------+
|                                  |
|                                  |
|                                  |
+----------------------------------+

R3 = CGRectSubstract(R2,R1);

+-----------------------+
|                       |
|                       |
|                       |
|          R3           |
|                       |
|                       |
|                       |
+-----------------------+

Example 3:

+----------------------------------+
|                                  |
|                                  |
|                                  |
|                 R1               |
|         +---------+              |
|         |         |              |
|         |   R2    |              |
+---------+---------+--------------+

R3 = CGRectSubstract(R2,R1);

+----------------------------------+
|                                  |
|                                  |
|              R3                  |
|                                  |
+----------------------------------+



jscs
  • 63,694
  • 13
  • 151
  • 195
anonymous
  • 163
  • 1
  • 1
  • 5

3 Answers3

21

Your definition is fairly ambiguous, what says whether the subtraction is horizontal or vertical? I recommend using a combination of CGRectIntersection and CGRectDivide, along with specifying a direction to remove ambiguity.

(not tested, or even compiled)

CGRect rectSubtract(CGRect r1, CGRect r2, CGRectEdge edge) {
    // Find how much r1 overlaps r2
    CGRect intersection = CGRectIntersection(r1, r2);

    // If they don't intersect, just return r1. No subtraction to be done
    if (CGRectIsNull(intersection)) {
        return r1;
    }

    // Figure out how much we chop off r1
    float chopAmount = (edge == CGRectMinXEdge || edge == CGRectMaxXEdge)
                       ? intersection.size.width
                       : intersection.size.height;

    CGRect r3, throwaway;
    // Chop
    CGRectDivide(r1, &throwaway, &r3, chopAmount, edge);
    return r3;
}
omz
  • 53,243
  • 5
  • 129
  • 141
cobbal
  • 69,903
  • 20
  • 143
  • 156
  • @cobbal: you rock..it's should work...i will check that..finally i got and that i want since last five days...:) – Nitin Apr 09 '12 at 06:32
1
CGRect newRect = CGRectMake(0, 0, rect2.size.width - rect1.size.width, rect2.size.height - rect1.size.height);

In response to your illustration, this code I've given you here will do exactly what you want (assuming you don't care about the origin XY coordinates). I've looked through the docs for CGGeometry functions, and there doesn't seem to be a CGRectDifference or other such method defined. There is, however, CGRectUnion, but that does the opposite of what you are looking for.

Leonard Pauli
  • 2,662
  • 1
  • 23
  • 23
Marc W
  • 19,083
  • 4
  • 59
  • 71
  • I already check the CGGeometry functions. We have GRectUnion and CGRectIntersection. But it's not what I need. – anonymous Jan 27 '11 at 00:28
  • Yeah, that was my conclusion as well. Just use the code I gave you to make your new rect and it should do what you need, again assuming you don't care about the origin coordinates. You could even name it CGRectDifference! =) – Marc W Jan 27 '11 at 00:29
  • I need the right coordinates and right size for R3. I guess your example is not correct for what I need. – anonymous Jan 27 '11 at 00:34
-1

Would probably go something like this:

CGRect frame = CGRectMake(0, 0, 320, 480);
float aWidth  = frame.size.width; /* say for instance 320 */
float aHeight = frame.size.height; /* say for instance 480 */

int final = aWidth - aHeight;
NSLog(@"Should be -160, your answer: %i",final);
WrightsCS
  • 50,551
  • 22
  • 134
  • 186
  • `frame.size.height` and `frame.size.width` is a `CGFloat`, not a `CGRect`. Typo, perhaps? Also, why are you subtracting width from height? Not sure what you're doing here... – Marc W Jan 27 '11 at 00:17
  • Yes, sorry, typo, its just an example. – WrightsCS Jan 27 '11 at 00:18