英文:
How to arrange Circles to make Trinity in SwiftUI
问题
使用
ZStack {
Circle()
.frame(width: width * 0.73, height: width * 0.2)
Circle()
.frame(width: width * 0.73, height: width * 0.2)
Circle()
.frame(width: width * 0.73, height: width * 0.2)
}
请不要关心细节,只关注圆的位置。因为如果我们将两个圆在y轴上分别偏移-10和10,并将第三个圆在x轴上向上移动10,它将不再完美居中。
请帮忙,如何使用圆形进行完美的三位一体排列的数学计算是什么?
英文:
The desired outcome
Using
ZStack {
Circle()
.frame(width: width * 0.73, height: width * 0.2)
Circle()
.frame(width: width * 0.73, height: width * 0.2)
Circle()
.frame(width: width * 0.73, height: width * 0.2)
}
Don't care about details here, but the position of circles. Because if we offset two circles one by -10 and other by 10 in y axis, and move up the third circle in x axis by 10, it no longer perfectly centers.
Please help, what's the math in forming a perfect trinity using Circles?
答案1
得分: 1
以下是翻译好的部分:
以下的代码在视图中显示了一个 `trinity`。
struct ContentView: View {
let radius = CGFloat(222)
var body: some View {
ZStack {
Circle().stroke(lineWidth: 4)
.offset(CGSize(width: -radius/4, height: radius/2))
Circle().stroke(lineWidth: 4)
Circle().stroke(lineWidth: 4)
.offset(CGSize(width: radius/4, height: radius/2))
}
.frame(width: radius, height: radius)
}
}
EDIT-1:
为了突出重要部分,`ZStack` 的 `.frame(width: radius, height: radius)`
struct ContentView: View {
var body: some View {
VStack {
Text("Trinity")
TrinityView(radius: CGFloat(123))
}
.frame(width: 333, height: 333)
}
}
struct TrinityView: View {
let radius: CGFloat
var body: some View {
ZStack {
Circle().stroke(lineWidth: 4).offset(x: -radius/4, y: radius/2)
.foregroundColor(.red)
Circle().stroke(lineWidth: 4)
.foregroundColor(.blue)
Circle().stroke(lineWidth: 4).offset(x: radius/4, y: radius/2)
.foregroundColor(.green)
}.frame(width: radius, height: radius)
}
}
英文:
The following code displays a trinity
in a view.
struct ContentView: View {
let radius = CGFloat(222)
var body: some View {
ZStack {
Circle().stroke(lineWidth: 4)
.offset(CGSize(width: -radius/4, height: radius/2))
Circle().stroke(lineWidth: 4)
Circle().stroke(lineWidth: 4)
.offset(CGSize(width: radius/4, height: radius/2))
}
.frame(width: radius, height: radius)
}
}
EDIT-1:
to highlight the important part, the .frame(width: radius, height: radius)
of the ZStack
struct ContentView: View {
var body: some View {
VStack {
Text("Trinity")
TrinityView(radius: CGFloat(123))
}
.frame(width: 333, height: 333)
}
}
struct TrinityView: View {
let radius: CGFloat
var body: some View {
ZStack {
Circle().stroke(lineWidth: 4).offset(x: -radius/4, y: radius/2)
.foregroundColor(.red)
Circle().stroke(lineWidth: 4)
.foregroundColor(.blue)
Circle().stroke(lineWidth: 4).offset(x: radius/4, y: radius/2)
.foregroundColor(.green)
}.frame(width: radius, height: radius)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论