繁体   English   中英

在循环中找到CGFloat中的最小值

[英]find minimum values in CGFloat obtained in a loop

我正在根据从循环变量事件获得的参数在循环中绘制reactangles,如下所示:

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

在每个循环中, minutesSinceEventdurationInMinutes更改,因此每次都会绘制一个不同的反应角。

我想在循环中获得最低的y值,并在循环中获得最大的高度。 简而言之,我想首先获得矩形的y值。 并且矩形的高度在所有下方延伸。

请让我知道是否需要其他信息?

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

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

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

您可以做的是在循环之前声明另一个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...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM