0

I have a CGRect A and CGRect B where B is centered inside of A (a CGRect contains the x and y origin and height and width size of a rectangle). If I increase the width and height of A by some proportion, and also increase the width and height of B by that same proportion, will multiplying the x origin of B and the y origin of B by this same proportion (for the width and height respectfully), will that keep B in the center of A as both grow by the new proportion? I've tested this out in a few different scenarios and it works, but just wanted to verify it'll work for all situations as I am not that sharp in math.

Also, was wondering if there is a method that will simply allow you to multiply all values of a CGRect by this proportion without having to do it manually (couldn't find one in the docs).

UPDATE: Actually, this will not work...trying to think of a methodology that will allow me to correctly position a view within another view after a proportional increase in size for both.

Ser Pounce
  • 14,196
  • 18
  • 84
  • 169
  • If you don't want your questions answered, please don't put them on stackoverflow. It takes time to reproduce the problem, come up with a solution and post it here. – Andreas Ley Aug 13 '12 at 07:19
  • @AndreasLey - I appreciate you taking the time to write out an answer, but in both the things I was asking about, you answered incorrectly ie 1) The position will not be correct if it's multiplied by the same proportion that the size increased, you said "Yes, What you proposed works". Then I asked if there was a method in the docs for multiplying all values of a rect, and you wrote out some ones manually for me which was cool, but it was simple enough I could've done it myself. Was just curious if Apple had one in the libraries. – Ser Pounce Aug 13 '12 at 17:51
  • 1
    Your question was ambiguous. I provided you with correct solutions for both the original and updated versions of your question. Just because it's not what you expected doesn't mean it's incorrect. Also, please keep in mind that stackoverflow is used by other people, who will find this page via Google. They, too, would like to know if a proposed solution works. – Andreas Ley Aug 14 '12 at 08:14
  • @andreasley - Even in your update you're still not understanding what I'm asking. I'm asking do the origin points of the inset rectangle grow proportionally as both it and the outset rectangle increase proportionally. That was my first question which you answered incorrectly. Now you've provided me with a method that I haven't even asked for, I know how to center a shape within another shape. Yes I do understand that stackoverflow is used by other people via Google, and that's exactly why I marked your answer as not being right. You're not answering either of my questions. Nothing personal. – Ser Pounce Aug 17 '12 at 19:56
  • 1
    Don't you think it's a bit strange that the only person answering to your question obviously didn't understand it? Also, your update stated that you're looking for a way to "correctly position a view within another view after a proportional increase in size for both", for which I clearly provided you with a working solution. – Andreas Ley Aug 20 '12 at 10:06

1 Answers1

2

Yes, what you proposed works, but only if the origin of the outer CGRect is 0,0 or if you multiply its origin by the factor, too. If you don't do that, the inner rect will be shifted to the bottom right.

Here's what happens if you multiple both origin and size:

center CGRect in CGRect

If you don't multiply the outer rect's origin, this happens:

enter image description here

From your question, it isn't entirely clear what you're trying to achieve. If you want to enlarge a CGRect and (re)center it another one, use these functions:

//  center a CGRect in another one
static inline CGRect ALRectCenterInRect(CGRect outerRect, CGRect innerRect)
{
    return CGRectMake(CGRectGetMidX(outerRect)-innerRect.size.width/2, CGRectGetMidY(outerRect)-innerRect.size.height/2, innerRect.size.width, innerRect.size.height);
}

//  multiply each value of a CGRect with factor
//      combine with CGRectIntegral() to prevent fractions (and the resulting aliasing)
static inline CGRect ALRectMultiply(CGRect rect, CGFloat factor)
{
    return CGRectMake(rect.origin.x*factor, rect.origin.y*factor, rect.size.width*factor, rect.size.height*factor);
}

How to use them:

CGRect centeredInnerRect = ALRectCenterInRect(outerRect, innerRect);

CGRect multipliedRect = ALRectMultiply(someRect, 1.5);

However, when dealing with CGRects, it's usually about UIViews. If you want to center a UIView in its superview, do this:

someSubview.center = CGPointMake(CGRectGetMidX(someSuperview.bounds), CGRectGetMidY(someSuperview.bounds));

If the inner view has the same superview as the outer view, you can simply do this to center it in the outer view:

innerView.center = outerView.center;
Andreas Ley
  • 9,109
  • 1
  • 47
  • 57