如何在 iOS 中使用贝塞尔曲线创建新月? [英] How to create a crescent moon using bezier paths in iOS?

查看:112
本文介绍了如何在 iOS 中使用贝塞尔曲线创建新月?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当笔触和填充可配置时,如何使用贝塞尔曲线绘制闭合的新月?所以汽车我可以得到一次曲线,但还没有找到连接和绘制另一条曲线的策略.

How to draw a closed crescent moon using bezier paths, when the stroke and fill are configurable? So car I can get once curve but haven't found a strategy to connect and draw the other curve.

推荐答案

如果您想要更好地近似天文上正确的新月形(外弧为 180 度),我可能会建议如下:

If you want something better approximating an astronomically correct crescent (where the outer arc is 180 degrees), I might suggest something like:

CGFloat angle = M_PI_2 * 0.60;      // how much of a crescent do you want (must be less than M_PI_2 and greater than zero)

UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:-M_PI_2 endAngle:M_PI_2 clockwise:TRUE];
[path addArcWithCenter:CGPointMake(center.x - radius * tan(angle), center.y) radius:radius / cosf(angle) startAngle:M_PI_2 - angle endAngle:angle - M_PI_2 clockwise:FALSE];
[path closePath];

结果:

如您所见,路径基本上由两个圆弧定义,一个顺时针,一个逆时针.内圆弧的中心、半径和起始角和终止角的细节是基本三角学的问题(但实际公式将根据所需的精确外观和感觉而有所不同).例如,在我的代码片段中,上面代码中的 angle 是下图中的蓝色角度,然后我计算了红色三角形,用于确定正确的起始角度和结束角度以及中心为内弧.

As you can see, the path is basically defined by two circular arcs, one clockwise, one counter clockwise. The specifics of the center, radius, and starting and ending angles of the inner arc is a matter of basic trigonometry (but the actual formulae will vary depending upon the precise desired look and feel). For example, in my code snippet, the angle in my code above is the blue angle in the following diagram, and I then calculate red triangle that will be used to determine the correct starting and ending angles and center for the inner arc.

下面,有人问这个逆时针旋转 90 度像:

Below, someone asked about rotating this counter clockwise 90 degrees like:

在 Swift 中,这将是:

In Swift, that would be:

let path = UIBezierPath(arcCenter: center, radius: radius, startAngle: -.pi, endAngle: 0, clockwise: true)
path.addArc(withCenter: CGPoint(x: center.x, y: center.y + radius * tan(angle)), radius: radius / cos(angle), startAngle: -angle, endAngle: -.pi + angle, clockwise: false)
path.close()

这篇关于如何在 iOS 中使用贝塞尔曲线创建新月?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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