找到循环中获得的CGFloat中的最小值

时间:2012-06-22 07:43:41

标签: objective-c minimum cgfloat cgrectmake

我根据循环变量事件中的参数在循环中绘制反应,如下所示:

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

minutesSinceEventdurationInMinutes的每个循环中都会发生变化,因此每次都会绘制一个不同的反应角。

我想获得循环中最低的y值和循环中的最大高度。简单地说,我希望得到矩形的y值,这是最重要的。并且矩形的高度在所有下方延伸。

如果需要任何其他信息,请告诉我?

2 个答案:

答案 0 :(得分:1)

一种非常简单的方法是在联合矩形中累积所有矩形:

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

这样做基本上是计算一个足够大的矩形,以包含在循环中创建的所有矩形(但不能更大)。

答案 1 :(得分:0)

您可以做的是在循环之前声明另一个CGRect变量并跟踪其中的值:

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...