@@ -18,62 +18,62 @@ struct Star: Shape {
18
18
// 从矩形的中心绘制
19
19
let center = CGPoint (x : rect.width / 2 , y : rect.height / 2 )
20
20
21
- // start from directly upwards (as opposed to down or to the right)
21
+ // 从直接向上开始(与向下或向右相反)
22
22
var currentAngle = - CGFloat.pi / 2
23
23
24
- // calculate how much we need to move with each star corner
24
+ // 计算每个星形角落需要移动多少。
25
25
let angleAdjustment = .pi * 2 / CGFloat (corners * 2 )
26
26
27
- // figure out how much we need to move X/Y for the inner points of the star
27
+ // 计算星形的内部点需要在X和Y方向上移动多少。
28
28
let innerX = center.x * smoothness
29
29
let innerY = center.y * smoothness
30
30
31
- // we're ready to start with our path now
31
+ // 我们现在准备好开始绘制路径了。
32
32
var path = Path ()
33
33
34
- // move to our initial position
34
+ // 移动到我们的初始位置。
35
35
path.move (to : CGPoint (x : center.x * cos (currentAngle), y : center.y * sin (currentAngle)))
36
36
37
- // track the lowest point we draw to, so we can center later
37
+ // 跟踪我们绘制的最低点,以便稍后进行居中处理。
38
38
var bottomEdge: CGFloat = 0
39
39
40
- // loop over all our points/inner points
40
+ // 循环遍历所有的点/内部点。
41
41
for corner in 0 ..< corners * 2 {
42
- // figure out the location of this point
42
+ // 确定该点的位置。
43
43
let sinAngle = sin (currentAngle)
44
44
let cosAngle = cos (currentAngle)
45
45
let bottom: CGFloat
46
46
47
- // if we're a multiple of 2 we are drawing the outer edge of the star
47
+ // 如果我们是2的倍数,那么我们正在绘制星形的外边缘。
48
48
if corner.isMultiple (of : 2 ) {
49
- // store this Y position
49
+ // 保存这个 Y 位置
50
50
bottom = center.y * sinAngle
51
51
52
- // …and add a line to there
52
+ // ...并添加一条线到那里。
53
53
path.addLine (to : CGPoint (x : center.x * cosAngle, y : bottom))
54
54
} else {
55
- // we're not a multiple of 2, which means we're drawing an inner point
55
+ // 我们不是2的倍数,这意味着我们正在绘制一个内部点。
56
56
57
- // store this Y position
57
+ // 保存这个 Y 位置
58
58
bottom = innerY * sinAngle
59
59
60
- // …and add a line to there
60
+ // ...并添加一条线到那里。
61
61
path.addLine (to : CGPoint (x : innerX * cosAngle, y : bottom))
62
62
}
63
63
64
- // if this new bottom point is our lowest, stash it away for later
64
+ // 如果这个新的底部点是我们最低的点,将其保存供以后使用。
65
65
if bottom > bottomEdge {
66
66
bottomEdge = bottom
67
67
}
68
68
69
- // move on to the next corner
69
+ // 继续下一个角落。
70
70
currentAngle += angleAdjustment
71
71
}
72
72
73
- // figure out how much unused space we have at the bottom of our drawing rectangle
73
+ // 计算我们绘图矩形底部剩余的未使用空间。
74
74
let unusedSpace = (rect.height / 2 - bottomEdge) / 2
75
75
76
- // create and apply a transform that moves our path down by that amount, centering the shape vertically
76
+ // 创建并应用一个变换,将我们的路径向下移动相应的距离,垂直居中形状。
77
77
let transform = CGAffineTransform (translationX : center.x , y : center.y + unusedSpace)
78
78
return path.applying (transform)
79
79
}
@@ -88,4 +88,4 @@ struct ContentView: View {
88
88
89
89
}
90
90
}
91
- ```
91
+ ```
0 commit comments