我希望在我的游戏中有一些声波绕过屏幕。我能够制作一个直线的正弦但我如何将正弦旋转20度?例如,从左下角到右上角?
"SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into `projects` (`name`, `description`, `updated_at`, `created_at ▶"
答案 0 :(得分:2)
您可以使用2 Bézier curves非常准确地使UIBezierPath
近似正弦波:
/// Make a path that approximates a peak-to-peak sine wave
func sinePath(size: CGSize) -> UIBezierPath {
let path = UIBezierPath()
// path starts at 0,0
path.move(to: CGPoint.zero)
let halfWidth = size.width / 2.0
// base x-values for control points
// baseX approximates the best fit Bézier curve for a sine wave
let baseX: = CGFloat(0.3642124232)
let firstX = halfWidth * baseX
let secondX = halfWidth * (1.0 - baseX)
// curve from start peak to trough
path.addCurve(to: CGPoint(x: halfWidth, y: size.height),
controlPoint1: CGPoint(x: firstX , y: 0),
controlPoint2: CGPoint(x: secondX, y: size.height))
// curve from trough to end peak
path.addCurve(to: CGPoint(x: size.width, y:0),
controlPoint1: CGPoint(x: size.width - secondX, y: size.height),
controlPoint2: CGPoint(x: size.width - firstX , y: 0))
return path
}
然后你可以用这样的CGAffineTransform
旋转它:
/// Convert degrees to radians
func degreeToRadian(_ angle: CGFloat) -> CGFloat {
return angle * CGFloat.pi / 180.0
}
let path = sinePath(size: CGSize(width: 200.0, height: 100.0))
path.apply(CGAffineTransform(rotationAngle: degreeToRadian(20)))
我从这个问题得到baseX
常数:
How to approximate a half-cosine curve with bezier paths in SVG?
答案 1 :(得分:0)
您可以将旋转的仿射变换应用于计算的点位置
xnew = xbase + (x - xbase) * Cos(rot_angle) - (y - ybase) * Sin(rot_angle)
ynew = ybase + (x - xbase) * Sin(rot_angle) + (y - ybase) * Cos(rot_angle)
此处(xbase, ybase)
- 旋转中心的坐标(在您的情况下可能是0,0
),rot_angle - 旋转角度,在您的情况下20 * Pi / 180
可能你的图形库已经包含了仿射变换的工具,在这种情况下只是形成变换矩阵并应用它。