-4

As seen in the image, I would like to find the mid-point between two rectangles. If the rectangles are intersecting then the mid-point would simply be between the centers of the rectangles. But if the rectangles are not intersecting, then the mid-point would be from/between the edges of the rectangles.

I would like to write this in Obj-C or Swift.

thx

enter image description here

StackUnderflow
  • 2,403
  • 2
  • 21
  • 28
  • 1
    Writing the ObjC/Swift code is probably the least problem, once you have figure out the *maths* :) – Martin R Aug 19 '15 at 14:24
  • 1
    You should ask this question in http://math.stackexchange.com. *ALSO*, you should at least do an attempt at solving it before posting a question, it's rude to just ask for the answer without any effort on your part. – EmilioPelaez Aug 19 '15 at 16:52
  • You are also missing another case: what if the rects overlap but the center is outside both rects? – EmilioPelaez Aug 19 '15 at 16:53

1 Answers1

0

In this Situation you should check first check wether there Rectangles are overlapped or not.

To Check Wether Overlapped or not

CGRect RectA = CGRectMake(50, 50, 50, 50);

CGRect RectB = CGRectMake(100, 100, 50, 50);

if (CGRectGetMinX(RectA) < CGRectGetMaxX(RectB)  &&  CGRectGetMaxX(RectA) > CGRectGetMinX(RectB)  &&
    CGRectGetMinY(RectA) < CGRectGetMaxY(RectB)  &&  CGRectGetMaxY(RectA) > CGRectGetMinY(RectB) )
{
    NSLog(@"overlapped");

}
else
{
    NSLog(@"Not overlapped");
}

after finding OverLapped or not Find Centres of Both the Rectangles and Do whatever You Want.

To Find Center.

CGPoint  centerB = CGPointMake((CGRectGetMinX(RectB) + CGRectGetMaxX(RectB))/2, (CGRectGetMinY(RectB) + CGRectGetMaxY(RectB))/2);

CGPoint centerA = CGPointMake((CGRectGetMinX(RectA) + CGRectGetMaxX(RectA))/2, (CGRectGetMinY(RectA) + CGRectGetMaxY(RectA))/2);

Kavita
  • 176
  • 8
  • I can use CGRectIntersectsRect instead of the long if-statement, but the code does not account for edges of the rectangles. The mid-point should be between the edges not between the centres. – StackUnderflow Aug 19 '15 at 20:44