0

I am drawing reactangles in a loop based on the parameters that I get from the looping variable event as below :

CGRectMake(cellWidth * event.xOffset,(cellHeight / MINUTES_IN_TWO_HOURS * [event minutesSinceEvent]), cellWidth,cellHeight / MINUTES_IN_TWO_HOURS * [event durationInMinutes]);

in every loop the minutesSinceEvent and durationInMinutes changes, so a different reactangle gets drawn every time.

I want to get the lowest y value in the loop and the greatest height in the loop. Simply saying, I want to have the y value of the rectangle which is above all. And the height of the rectangle which extends below all.

Please let me know, if any other information is needed?

rishi
  • 11,779
  • 4
  • 40
  • 59

2 Answers2

1

A very simple way would be to accumulate all rectangles in a union rectangle:

CGRect unionRect = CGRectNull;
for (...) {
    CGRect currentRect = ...;
    unionRect = CGRectUnion(unionRect, currentRect);
}
NSLog(@"min Y : %f", CGRectGetMinY(unionRect));
NSLog(@"height: %f", CGRectGetHeight(unionRect));

What this does is basically to calculate a rectangle that is large enough to contain all rectangles that were created in the loop (but no larger).

omz
  • 53,243
  • 5
  • 129
  • 141
0

What you can do is declare another CGRect variable before the loop and track the value inside:

CGRect maxRect = CGRectZero;
maxRect.origin.y = HUGE_VALF; //this is to set a very big number of y so the first one you compare to will be always lower - you can set a different number of course...
for(......)
{
    CGRect currentRect = CGRectMake(cellWidth * event.xOffset,(cellHeight / MINUTES_IN_TWO_HOURS * [event minutesSinceEvent]), cellWidth,cellHeight / MINUTES_IN_TWO_HOURS * [event durationInMinutes]);

   if(currentRect.origin.y < maxRect.origin.y)
       maxRect.origin.y = currentRect.origin.y;

   if(currentRect.size.height > maxRect.size.height)
       maxRect.size.height = currentRect.size.height;
}

//After the loop your maxRect.origin.y will be the lowest and your maxRect.size.height will be the greatest...
graver
  • 15,183
  • 4
  • 46
  • 62