- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
我有一个任意的 CGPath
,我想找到它的地理中心。我可以使用 CGPathGetPathBoundingBox
获取路径边界框,然后找到该框的中心。但是有没有更好的方法来找到路径的中心?
更新给那些喜欢看代码的人:这里是使用亚当在答案中建议的平均点法的代码(不要错过下面答案中更好的技术)...
BOOL moved = NO; // the first coord should be a move, the rest add lines
CGPoint total = CGPointZero;
for (NSDictionary *coord in [polygon objectForKey:@"coordinates"]) {
CGPoint point = CGPointMake([(NSNumber *)[coord objectForKey:@"x"] floatValue],
[(NSNumber *)[coord objectForKey:@"y"] floatValue]);
if (moved) {
CGContextAddLineToPoint(context, point.x, point.y);
// calculate totals of x and y to help find the center later
// skip the first "move" point since it is repeated at the end in this data
total.x = total.x + point.x;
total.y = total.y + point.y;
} else {
CGContextMoveToPoint(context, point.x, point.y);
moved = YES; // we only move once, then we add lines
}
}
// the center is the average of the total points
CGPoint center = CGPointMake(total.x / ([[polygon objectForKey:@"coordinates"] count]-1), total.y / ([[polygon objectForKey:@"coordinates"] count]-1));
如果你有更好的想法,请分享!
最佳答案
该技术有效,但您在问题中输入的代码无效。 AFAICS,这只适用于你只做直线多边形的少数情况,和你有一个点列表,和你还没有制作 CGPath 对象还没有。
我需要为任意 CGPath 对象执行此操作。使用 Adam(其他 Adam)的建议和 Apple 的 CGPathApply,我想到了这个,效果很好:
{
float dataArray[3] = { 0, 0, 0 };
CGPathApply( (CGPathRef) YOUR_PATH, dataArray, pathApplierSumCoordinatesOfAllPoints);
float averageX = dataArray[0] / dataArray[2];
float averageY = dataArray[1] / dataArray[2];
CGPoint centerOfPath = CGPointMake(averageX, averageY);
}
static void pathApplierSumCoordinatesOfAllPoints(void* info, const CGPathElement* element)
{
float* dataArray = (float*) info;
float xTotal = dataArray[0];
float yTotal = dataArray[1];
float numPoints = dataArray[2];
switch (element->type)
{
case kCGPathElementMoveToPoint:
{
/** for a move to, add the single target point only */
CGPoint p = element->points[0];
xTotal += p.x;
yTotal += p.y;
numPoints += 1.0;
}
break;
case kCGPathElementAddLineToPoint:
{
/** for a line to, add the single target point only */
CGPoint p = element->points[0];
xTotal += p.x;
yTotal += p.y;
numPoints += 1.0;
}
break;
case kCGPathElementAddQuadCurveToPoint:
for( int i=0; i<2; i++ ) // note: quad has TWO not THREE
{
/** for a curve, we add all ppints, including the control poitns */
CGPoint p = element->points[i];
xTotal += p.x;
yTotal += p.y;
numPoints += 1.0;
}
break;
case kCGPathElementAddCurveToPoint:
for( int i=0; i<3; i++ ) // note: cubic has THREE not TWO
{
/** for a curve, we add all ppints, including the control poitns */
CGPoint p = element->points[i];
xTotal += p.x;
yTotal += p.y;
numPoints += 1.0;
}
break;
case kCGPathElementCloseSubpath:
/** for a close path, do nothing */
break;
}
//NSLog(@"new x=%2.2f, new y=%2.2f, new num=%2.2f", xTotal, yTotal, numPoints);
dataArray[0] = xTotal;
dataArray[1] = yTotal;
dataArray[2] = numPoints;
}
关于iphone - 寻找 CGPath 的中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7186054/
我在 CAShapeLayer 上执行了一些 CGPath(一个带有两个圆圈的多边形)。 我可以像这样修剪/减去路径吗? 最佳答案 无需计算,可以在绘制时使用 kCGBlendModeClear 获得
我试图将 UITextField 的左上角和左下角四舍五入。这是我正在使用的代码: override func viewDidLayoutSubviews() { super.viewDidL
我有一个代表椭圆的 SKShape 节点。玩家被放置在基于椭圆 CGPath 的贝塞尔路径的当前点上: 我有两个玩家节点可以执行的操作。玩家可以顺时针或逆时针沿着路径行驶。 rotateCounter
我在 CGPath.h 头文件中找到了它。我很好奇这个 const 的作用是什么? typedef struct CGPath *CGMutablePathRef; typedef const str
我正在使用 CGPath 绘制形状并在绘制后添加到 CAShapeLayer。我正在使用 CGPathGetPathBoundingBox() 获取框架(此框架将设置为 CAShapeLayer)但它
下面的代码有点问题。 points 数组是由寻路算法提供的一组点,它给出了在下面的方法中创建的开始和结束 CGPoints 之间的最短路径。调试此代码后,我知道它有效。 我认为是 CGPath 导致了
我已经研究了一段时间了,作为一个初学者,我似乎无法完成它。我想要做的是采用简单的渐变,并用它来描边 CGPath。我尝试使用 CGContextSetStokePattern 和 PNG 线的宽度和一
我有一个 UIView,其中包含一个带有不规则大小切片的“饼图”,我需要它们是可点击的区域。通过大量的反复试验,我写了一个方法来制作馅饼,但现在我不知道如何将这些切片分成区域。目前这个方法返回 voi
我想创建一个类别来“扩展”CGPath 的“类别”并添加更多功能。因为 CGPath 不是 ObjectiveC 的常规对象,我该怎么做? 请原谅我对c/c++的了解不足 最佳答案 很简单,您只需编写
我正在使用 Swift 制作游戏,并且我有一个任意角度的 CGPath。现在让我们假设它始终是一条直线。 我想创建的是这样的: 我已经尝试了一种非常简单的方法,即以黑色的粗线宽进行描边,复制路径,然后
是否可以在创建时绘制 CGPath(添加每条线时)而不是立即绘制点的整个路径?我希望看到在添加线条时绘制的路径。 此外,是否可以一次绘制一条线作为较大路径的一部分,而不是每次都在自身上绘制一条额外的线
我正在尝试找到一种方法来创建随机闭合平滑路径(CGPath 或 UIBezierPath)。我读过 De Casteljau's algorithm以及其他关于贝塞尔路径的文章,但它似乎不符合我试图实
简单地说,我正在寻找可在 iOS 上使用的与 NSBezierPath 的 -bezierPathByFlatteningPath 等效的方法。这对我来说是直接处理 CGPath 的函数还是 UIBe
更新 我通过使用 OpenGL 绘制所有内容来绕过 CG 的限制。仍然存在一些问题,但到目前为止,它的工作速度要快得多。 一些有趣的点: GLKView :这是一个特定于 iOS 的 View ,它对
我正在尝试绘制一个 CGPath,它的笔划有一个笔划。 基本上我想使用 CGPath 画一条线。然后我想返回并在最后一个 CGPath 的两侧画线,使其具有轮廓效果。 这条线可以以任何方式弯曲和转动,
如何删除路径的某些部分?我在屏幕上绘制了一条路径,现在假设我想删除 1/3: 我试过这样做(路径是我的原始路径,cPath是将被剪切的副本): let sizePath = CGPathGetBoun
我的新游戏 Sprite 有问题... 我的“汽车”在第一圈完美地沿着赛道行驶,但从那时起,它似乎在每圈结束时都会发生偏移。有人能告诉我我做错了什么吗? - (void)createSceneCont
我需要一些示例代码,我可以在其中为曲线/圆弧路径设置动画,该路径应该绘制一个完整的圆作为动画? 我们将不胜感激。 谢谢,周六 最佳答案 更新:意识到这个问题有更好的解决方案。只需创建一个圆形路径并将
我意识到在 iPhone 上让图像遵循预先确定的路径并不是很困难(请参阅 here ),但我想知道图像是否有可能仅遵循部分的路径。例如,如果我有一条从 A 到 B 的路径,如何使图像仅以动画形式呈现朝
假设我有一个 CGPath,它是 CGContextRef 中的一个圆圈。我想做的是用颜色填充路径的反面。下面演示了其中圆圈是当前绘制的路径,我想用颜色填充圆圈的反面,留下一个空洞: 最佳答案 首先将
我是一名优秀的程序员,十分优秀!