如何用CGPath绘制多边形?

发布于 2024-08-20 19:22:07 字数 285 浏览 6 评论 0原文

我一直在阅读文档,但是我并不清楚如何使用 CGPath 绘制多边形。我需要做的就是围绕这样的内容绘制 CGPath:

__
\  \ 
 \  \
  \__\

任何人都可以提供有关如何执行此操作的片段吗?

另外,我假设 CGPathContainsPoint 将帮助我确定一个点是否在这样的路径内?或者该路径是否必须是实体图

另外我如何移动 cgpath?这就像在 cgrect 中改变原点一样简单吗?

谢谢。

-奥斯卡

I have been reading thru the documentation however it is not immediatly clear to me how to draw a polygon using CGPath. All I need to do is to draw CGPath around something like this:

__
\  \ 
 \  \
  \__\

Could anyone please provide an snippet on how to do this?

Additionally I assume CGPathContainsPoint will help me determine if a point is inside such path?, or does the path have to be a solid drawing

Also how can i move the cgpath around? Is this as easy as changing something like the origin just like in cgrect?

Thank you.

-Oscar

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

兮颜 2024-08-27 19:22:07

这是如何使用 CGPath 创建三角形的示例,您只需放置点即可。

var path = CGPathCreateMutable()
CGPathMoveToPoint(path, nil, 0, 0) //start from here
CGPathAddLineToPoint(path, nil, 20, 44) 
CGPathAddLineToPoint(path, nil, 40, 0) 
CGPathAddLineToPoint(path, nil, 0, 0)

//and to use in SpriteKit, for example

var tri = SKShapeNode(path: path) 
var color = NSColor.blueColor()
tri.strokeColor = color
tri.fillColor = color

这是结果

Triangle with CGPath

This is an example of how to create a triangle using CGPath, you only have to put the points.

var path = CGPathCreateMutable()
CGPathMoveToPoint(path, nil, 0, 0) //start from here
CGPathAddLineToPoint(path, nil, 20, 44) 
CGPathAddLineToPoint(path, nil, 40, 0) 
CGPathAddLineToPoint(path, nil, 0, 0)

//and to use in SpriteKit, for example

var tri = SKShapeNode(path: path) 
var color = NSColor.blueColor()
tri.strokeColor = color
tri.fillColor = color

This is the result

Triangle with CGPath

丶情人眼里出诗心の 2024-08-27 19:22:07

你应该这样做:

- (void)drawRect:(CGRect)rect { 

        CGContextRef context = UIGraphicsGetCurrentContext(); 

        CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
        CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);

        // Draw them with a 2.0 stroke width so they are a bit more visible.
        CGContextSetLineWidth(context, 2.0);

        for(int idx = 0; idx < self.points.count; idx++)
        {

            point = [self.points objectAtIndex:idx];//Edited 
            if(idx == 0)
            {
                // move to the first point
                CGContextMoveToPoint(context, point.x, point.y);
            }
            else
            {
                CGContextAddLineToPoint(context, point.x, point.y);
            }
        }

        CGContextStrokePath(context);
}

注意这里,点是你想要为其绘制多边形的点的数组。所以它应该是圆形路径,如下所示:您正在绘制一个点三角形 (x1, x2, x3) 然后您应该传递到数组 (x1, x2, x3, x1) >。

希望这有帮助。

You should do it like this:

- (void)drawRect:(CGRect)rect { 

        CGContextRef context = UIGraphicsGetCurrentContext(); 

        CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
        CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);

        // Draw them with a 2.0 stroke width so they are a bit more visible.
        CGContextSetLineWidth(context, 2.0);

        for(int idx = 0; idx < self.points.count; idx++)
        {

            point = [self.points objectAtIndex:idx];//Edited 
            if(idx == 0)
            {
                // move to the first point
                CGContextMoveToPoint(context, point.x, point.y);
            }
            else
            {
                CGContextAddLineToPoint(context, point.x, point.y);
            }
        }

        CGContextStrokePath(context);
}

Note here, the points is the array of points you want to draw the polygon for. So it should be circular path like: You are drawing a triangle of points (x1, x2, x3) then you should pass into array (x1, x2, x3, x1).

Hope this helps.

梨涡少年 2024-08-27 19:22:07

Draelach 的答案已更新为 Swift 4:

let path = CGMutablePath()
path.move(to: CGPoint(x: 0, y: 0))
path.addLine(to: CGPoint(x: 20, y: 44))
path.addLine(to: CGPoint(x: 40, y: 0))
path.addLine(to: CGPoint(x: 0, y: 0))

let tri = SKShapeNode(path: path)

Draelach's answer updated to Swift 4:

let path = CGMutablePath()
path.move(to: CGPoint(x: 0, y: 0))
path.addLine(to: CGPoint(x: 20, y: 44))
path.addLine(to: CGPoint(x: 40, y: 0))
path.addLine(to: CGPoint(x: 0, y: 0))

let tri = SKShapeNode(path: path)
咆哮 2024-08-27 19:22:07

请参阅 Apple 的 QuartzDemo 应用程序。它有执行此操作的代码,以及许多其他 Quartz 绘图函数。

See Apple's QuartzDemo application. It has code for doing this, as well as many other Quartz drawing functions.

彼岸花ソ最美的依靠 2024-08-27 19:22:07

斯坦福大学在 iPhone 上的 CS193P 课程有一个名为 HelloPoly 的项目,它可能正是您想要的 - 请参阅

Stanford's CS193P class on iPhone had a project called HelloPoly that might be exactly what you want - see class home page for the spec and then see the video for how it was implemented (and google solutions from people who did the assignment).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文