有没有一种方法可以获取NSBezierPath对象所有点的x,y坐标? [英] Is there a way to get the x,y co-ordinates of all points of a NSBezierPath object?

查看:125
本文介绍了有没有一种方法可以获取NSBezierPath对象所有点的x,y坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个NSBezierPath对象,是否可以获取所有绘制点的坐标(x,y)?我想沿着路径移动NSRect.

If I have an NSBezierPath object, is there a way to get coordinates(x,y) of all the points drawn? I want to move an NSRect along the path.

推荐答案

NSBezierPath并未确切定义其绘制的点,但确实包含定义其片段所需的点.您可以使用

An NSBezierPath doesn't define exactly which points it draws in, but it does contain the points needed to define its pieces. You can use the elementAtIndex:associatedPoints: method to get the points for each vector element in the path. To get each point in the path you would have to iterate over all of the elements and get the associated points. For straight lines, this method will give you the endpoint, but if you keep track of the previous point you can use as many points as you want between them.

对于曲线,您将需要实现代码来确定曲线的路径以沿曲线查找点.使用bezierPathByFlatteningPath平坦化路径要简单得多,它会返回一条新路径,并将所有曲线都转换为直线.

For curves, you would need to implement the code to determine the curve's path to find points along the curve. It would be much simpler to flatten the path, using bezierPathByFlatteningPath, which returns a new path with all curves converted into straight lines.

这是一个使路径变平并打印结果中所有行的端点的示例.如果路径包含长直线,则需要根据长度在直线上添加点.

Here's an example which flattens a path and prints the endpoints of all lines in the result. If your path contains long straight lines, you will want to add points along lines depending on the length.

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));
    }
}

这篇关于有没有一种方法可以获取NSBezierPath对象所有点的x,y坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆