如何在 ios 中用贝塞尔曲线制作星形?
How to make star shape with bezier path in ios?
我正在使用 Bezier 路径 并用它制作简单的形状。使用贝塞尔路径制作星形的过程是什么?
我想这就是您要找的:
func starPathInRect(rect: CGRect) -> UIBezierPath {
let path = UIBezierPath()
let starExtrusion:CGFloat = 30.0
let center = CGPointMake(rect.width / 2.0, rect.height / 2.0)
let pointsOnStar = 5 + arc4random() % 10
var angle:CGFloat = -CGFloat(M_PI / 2.0)
let angleIncrement = CGFloat(M_PI * 2.0 / Double(pointsOnStar))
let radius = rect.width / 2.0
var firstPoint = true
for i in 1...pointsOnStar {
let point = pointFrom(angle, radius: radius, offset: center)
let nextPoint = pointFrom(angle + angleIncrement, radius: radius, offset: center)
let midPoint = pointFrom(angle + angleIncrement / 2.0, radius: starExtrusion, offset: center)
if firstPoint {
firstPoint = false
path.moveToPoint(point)
}
path.addLineToPoint(midPoint)
path.addLineToPoint(nextPoint)
angle += angleIncrement
}
path.closePath()
return path
}
This Function let you decide How many points you want in your stars..
查看此 link 了解更多 detailed description
。
希望对您有所帮助。 :) 如有任何疑问,请告诉我..!!!
试试这个路径:
//// Star Drawing
UIBezierPath* starPath = [UIBezierPath bezierPath];
[starPath moveToPoint: CGPointMake(45.25, 0)];
[starPath addLineToPoint: CGPointMake(61.13, 23)];
[starPath addLineToPoint: CGPointMake(88.29, 30.75)];
[starPath addLineToPoint: CGPointMake(70.95, 52.71)];
[starPath addLineToPoint: CGPointMake(71.85, 80.5)];
[starPath addLineToPoint: CGPointMake(45.25, 71.07)];
[starPath addLineToPoint: CGPointMake(18.65, 80.5)];
[starPath addLineToPoint: CGPointMake(19.55, 52.71)];
[starPath addLineToPoint: CGPointMake(2.21, 30.75)];
[starPath addLineToPoint: CGPointMake(29.37, 23)];
[starPath closePath];
[UIColor.redColor setStroke];
starPath.lineWidth = 1;
[starPath stroke];
您可以使用 Paint Code 应用程序为 iOS 和 OSX 绘制形状。
这是易于使用且非常有用的应用程序。
编码愉快。
我升级了@Sneha的算法。
您可以这样使用它:
rect = CGRect(x: 0, y: 0, width: 15, height: 15)
path.addStar(rect: rect, extrusion: 20, points: 5)
path.close()
这是算法:
extension CGPoint {
func pointFrom(angle: CGFloat, radius: CGFloat) -> CGPoint {
return CGPoint(x: self.x + radius * cos(CGFloat.pi - angle), y: self.y - radius * sin(CGFloat.pi - angle))
}
}
extension UIBezierPath {
func addStar(rect: CGRect, extrusion: CGFloat, points: Int) {
self.move(to: CGPoint(x: 0, y: 0))
let center = CGPoint(x: rect.width / 2.0, y: rect.height / 2.0)
var angle:CGFloat = -CGFloat.pi / 2.0
let angleIncrement = CGFloat.pi * 2.0 / CGFloat(points)
let radius = rect.width / 2.0
var firstPoint = true
for _ in 1...points {
let point = center.pointFrom(angle: angle, radius: radius)
let nextPoint = center.pointFrom(angle: angle + angleIncrement, radius: radius)
let midPoint = center.pointFrom(angle: angle + angleIncrement / 2.0, radius: extrusion)
if firstPoint {
firstPoint = false
self.move(to: point)
}
self.addLine(to: midPoint)
self.addLine(to: nextPoint)
angle += angleIncrement
}
}
我正在使用 Bezier 路径 并用它制作简单的形状。使用贝塞尔路径制作星形的过程是什么?
我想这就是您要找的:
func starPathInRect(rect: CGRect) -> UIBezierPath {
let path = UIBezierPath()
let starExtrusion:CGFloat = 30.0
let center = CGPointMake(rect.width / 2.0, rect.height / 2.0)
let pointsOnStar = 5 + arc4random() % 10
var angle:CGFloat = -CGFloat(M_PI / 2.0)
let angleIncrement = CGFloat(M_PI * 2.0 / Double(pointsOnStar))
let radius = rect.width / 2.0
var firstPoint = true
for i in 1...pointsOnStar {
let point = pointFrom(angle, radius: radius, offset: center)
let nextPoint = pointFrom(angle + angleIncrement, radius: radius, offset: center)
let midPoint = pointFrom(angle + angleIncrement / 2.0, radius: starExtrusion, offset: center)
if firstPoint {
firstPoint = false
path.moveToPoint(point)
}
path.addLineToPoint(midPoint)
path.addLineToPoint(nextPoint)
angle += angleIncrement
}
path.closePath()
return path
}
This Function let you decide How many points you want in your stars..
查看此 link 了解更多 detailed description
。
希望对您有所帮助。 :) 如有任何疑问,请告诉我..!!!
试试这个路径:
//// Star Drawing
UIBezierPath* starPath = [UIBezierPath bezierPath];
[starPath moveToPoint: CGPointMake(45.25, 0)];
[starPath addLineToPoint: CGPointMake(61.13, 23)];
[starPath addLineToPoint: CGPointMake(88.29, 30.75)];
[starPath addLineToPoint: CGPointMake(70.95, 52.71)];
[starPath addLineToPoint: CGPointMake(71.85, 80.5)];
[starPath addLineToPoint: CGPointMake(45.25, 71.07)];
[starPath addLineToPoint: CGPointMake(18.65, 80.5)];
[starPath addLineToPoint: CGPointMake(19.55, 52.71)];
[starPath addLineToPoint: CGPointMake(2.21, 30.75)];
[starPath addLineToPoint: CGPointMake(29.37, 23)];
[starPath closePath];
[UIColor.redColor setStroke];
starPath.lineWidth = 1;
[starPath stroke];
您可以使用 Paint Code 应用程序为 iOS 和 OSX 绘制形状。 这是易于使用且非常有用的应用程序。
编码愉快。
我升级了@Sneha的算法。 您可以这样使用它:
rect = CGRect(x: 0, y: 0, width: 15, height: 15)
path.addStar(rect: rect, extrusion: 20, points: 5)
path.close()
这是算法:
extension CGPoint {
func pointFrom(angle: CGFloat, radius: CGFloat) -> CGPoint {
return CGPoint(x: self.x + radius * cos(CGFloat.pi - angle), y: self.y - radius * sin(CGFloat.pi - angle))
}
}
extension UIBezierPath {
func addStar(rect: CGRect, extrusion: CGFloat, points: Int) {
self.move(to: CGPoint(x: 0, y: 0))
let center = CGPoint(x: rect.width / 2.0, y: rect.height / 2.0)
var angle:CGFloat = -CGFloat.pi / 2.0
let angleIncrement = CGFloat.pi * 2.0 / CGFloat(points)
let radius = rect.width / 2.0
var firstPoint = true
for _ in 1...points {
let point = center.pointFrom(angle: angle, radius: radius)
let nextPoint = center.pointFrom(angle: angle + angleIncrement, radius: radius)
let midPoint = center.pointFrom(angle: angle + angleIncrement / 2.0, radius: extrusion)
if firstPoint {
firstPoint = false
self.move(to: point)
}
self.addLine(to: midPoint)
self.addLine(to: nextPoint)
angle += angleIncrement
}
}