-->

有没有办法让X,A NSBezierPath对象的所有点的Y坐标?(Is there a way t

2019-07-30 04:49发布

如果我有一个NSBezierPath对象,有没有办法让画的所有点的坐标(X,Y)。 我想沿着路径移动的NSRect。

Answer 1:

一个NSBezierPath不完全定义指向它吸引的,但它确实包含定义其作品所需要的点。 您可以使用elementAtIndex:associatedPoints:方法来获取点路径中的每个向量元素。 要获得路径中的每个点,你将不得不遍历所有元素,并得到相关的点。 对于直线,这种方法会给你的终点,但如果你把前面的点的轨迹,你希望他们之间可以使用尽可能多的积分。

对于曲线,你需要实现的代码,以确定曲线的路径来寻找沿曲线点。 这将是更简单拼合路径,使用bezierPathByFlatteningPath ,它会返回转换成直线所有曲线的新路径。

下面是其变平的路径,并打印在结果的所有行的端点的例子。 如果您的路径包含长直线,你将要沿着这取决于长度线加分。

NSBezierPath *originalPath;
NSBezierPath *flatPath = [originalPath bezierPathByFlatteningPath];
NSInteger count = [flatPath elementCount];
NSPoint prev, curr;
NSInteger i;
for(i = 0; i < count; ++i) {
    // Since we are using a flattened path, no element will contain more than one point
    NSBezierPathElement type = [flatPath elementAtIndex:i associatedPoints:&curr];
    if(type == NSLineToBezierPathElement) {
        NSLog(@"Line from %@ to %@",NSStringFromPoint(prev),NSStringFromPoint(curr));
    } else if(type == NSClosePathBezierPathElement) {
        // Get the first point in the path as the line's end. The first element in a path is a move to operation
        [flatPath elementAtIndex:0 associatedPoints:&curr];
        NSLog(@"Close line from %@ to %@",NSStringFromPoint(prev),NSStringFromPoint(curr));
    }
}


Answer 2:

没有,因为一个路径基于矢量,而不是基于像素的。 你将不得不呈现在路径CGContextRef ,然后检查其像素接到该设置。 但没有内置该方法。

如果你需要沿着路径移动矩形的,但是,你很可能使用CALayer要做到这一点,虽然我并不完全知道如何。



文章来源: Is there a way to get the x,y co-ordinates of all points of a NSBezierPath object?