[英]Multiple CGPoints to CGRect
I have aa set of CGPoint
s which represent a shape which is a bit like an upside down 'T' shape, now I want to convert those points into a CGRect
which fits inside the shape, so to create a CGRect
which encompasses the entire shape I just loop through and work out the lowest x
and y
for the top left and the highest x
and y
for the bottom right which is great but leaves white areas outside of the image, how could I figure out the largest rectangle without white areas so the final shape is more like an '|' 我有AA组
CGPoint
S的代表的形状有点像一个倒“T”形,现在我想这些点转换成CGRect
符合该形状内,所以要创建一个CGRect
其中包括整个形状我只是循环并计算出y
的最低x
和y
以及右下角的最高x
和y
,这很好但是在图像外面留下了白色区域,我怎么能找出没有白色区域的最大矩形所以最终的形状更像是'|' shape? 形状? My code so far:
我的代码到目前为止:
CGPoint topLeft = CGPointZero;
CGPoint bottomRight = CGPointZero;
for( NSValue *value in points ) {
CGPoint point = [value CGPointValue];
if( topLeft.x == 0 || topLeft.x > point.x ) shapeRect.x = point.x;
if( topLeft.y == 0 || topLeft.y > point.y ) shapeRect.y = point.y;
if( bottomRight.x < point.x ) bottomRight.x = point.x;
if( bottomRight.y < point.y ) bottomRight.y = point.y;
}
CGRect shapeRect = CGRectMake(topLeft.x, topLeft.y, bottomRight.x - topLeft.x, bottomRight.y - topLeft.y);
EDIT: I've drawn some pics to show what I'm trying to achieve. 编辑:我画了一些照片来展示我想要实现的目标。 Grey areas show the
CGRect
. 灰色区域显示
CGRect
。
Here's the image shape, I have coordinates for each point in the shape: 这是图像形状,我有形状中每个点的坐标:
Image Hosted by ImageShack.us http://img684.imageshack.us/img684/121/crop1.png 图片由ImageShack.us提供http://img684.imageshack.us/img684/121/crop1.png
Here's what my code above produces: 这是我上面的代码产生的:
Image Hosted by ImageShack.us http://img26.imageshack.us/img26/2521/crop2j.png 图片由ImageShack.us主持http://img26.imageshack.us/img26/2521/crop2j.png
Here's what I'm trying to achieve: 这是我想要实现的目标:
Image Hosted by ImageShack.us http://img689.imageshack.us/img689/5499/crop3.png 图片由ImageShack.us提供http://img689.imageshack.us/img689/5499/crop3.png
Hard to get a grip on what you are actually asking about. 难以掌握你实际要问的内容。 In regards to the title this function will create the smallest rect for any number of CGPoints.
关于标题,此函数将为任意数量的CGPoints创建最小的rect。
CGRect CGRectSmallestWithCGPoints(CGPoint pointsArray[], int numberOfPoints)
{
CGFloat greatestXValue = pointsArray[0].x;
CGFloat greatestYValue = pointsArray[0].y;
CGFloat smallestXValue = pointsArray[0].x;
CGFloat smallestYValue = pointsArray[0].y;
for(int i = 1; i < numberOfPoints; i++)
{
CGPoint point = pointsArray[i];
greatestXValue = MAX(greatestXValue, point.x);
greatestYValue = MAX(greatestYValue, point.y);
smallestXValue = MIN(smallestXValue, point.x);
smallestYValue = MIN(smallestYValue, point.y);
}
CGRect rect;
rect.origin = CGPointMake(smallestXValue, smallestYValue);
rect.size.width = greatestXValue - smallestXValue;
rect.size.height = greatestYValue - smallestYValue;
return rect;
}
Can be used like this 可以像这样使用
CGPoint poinstArray[] = {topLeft, bottomRight};
CGRect smallestRect = CGRectSmallestWithCGPoints(poinstArray, 2);
If you want, you can use Core Graphics: 如果需要,可以使用Core Graphics:
let path = CGMutablePath()
path.addLines(between: [p1, p2, p3, p4])
return path.boundingBoxOfPath
If I did not misunderstand the question, your aim is to find the blue points: 如果我没有误解这个问题,你的目标是找到蓝点:
If I'm correct, then it's enough for you to store two points (say topL
and topR
) and a value (say bottom
). 如果我是正确的,那么你就可以存储两个点(比如
topL
和topR
)和一个值(比如说bottom
)。
Iteratively: 迭代:
y < topL.y
and eventually update topL
and topR
. y < topL.y
并最终更新topL
和topR
。
y == topL.y
check if current x
is less than topL.x
. y == topL.y
检查当前x
是否小于topL.x
If yes update topL
topL
x>topR.x
; x>topR.x
; if yes update topR
. topR
。 y>bottom
. y>bottom
。 If yes update bottom
. bottom
。 Note that when I say "update topL
" I mean both x
and y
. 请注意,当我说“更新
topL
”时,我指的是x
和y
。
At the end you can get your bottom-left and bottom-right points using x
coordinate of topL
and topR
and setting y
coordinate to bottom. 最后,您可以使用
topL
和topR
x
坐标以及将y
坐标设置为bottom来获得左下和右下点。
Swift version of hfossli (it works great !) : Swift版本的hfossli(效果很好!):
func pointToRect(pointsArray: [CGPoint]) -> CGRect {
var greatestXValue = pointsArray[0].x
var greatestYValue = pointsArray[0].y
var smallestXValue = pointsArray[0].x
var smallestYValue = pointsArray[0].y
for point in pointsArray {
greatestXValue = max(greatestXValue, point.x);
greatestYValue = max(greatestYValue, point.y);
smallestXValue = min(smallestXValue, point.x);
smallestYValue = min(smallestYValue, point.y);
}
let origin = CGPoint(x: smallestXValue, y: smallestYValue)
let size = CGSize(width: greatestXValue - smallestXValue, height: greatestYValue - smallestYValue)
return CGRect(origin: origin, size: size)
}
Hopefully you're only talking about this one shape, always oriented this way, otherwise this becomes a tricky computational geometry problem (something like the largest enclosed rectangle in a concave polygon). 希望你只是谈论这一个形状,总是以这种方式定向,否则这将成为一个棘手的计算几何问题(类似于凹多边形中最大的封闭矩形)。
Given your list of points, the following should work: 根据您的积分列表,以下内容应该有效:
a. 一种。 Find the point with the largest y value.
找到y值最大的点。
b. 湾 Find the other point with an equally large y value.
找到具有相同大y值的另一个点。
c. C。 Compare the x values of these two and determine which is leftmost.
比较这两个的x值并确定哪个是最左边的。
Find the minimum y value of all the points. 找到所有点的最小y值。
Create two new points, each with an x value equal to one of the points you found in step 1, and with the y value found in step 2. 创建两个新点,每个点的x值等于在步骤1中找到的点之一,并且在步骤2中找到y值。
The two points from step 1 are your top left and right points, and the two created in step 3 are the bottom left and right. 第1步中的两个点是左上角和右上角,第3步中创建的两个点是左下角和右下角。 You can now construct the final rectangle.
您现在可以构造最终的矩形。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.