首页 文章

下载app体验更佳(物理动画+AI问答),点此下载

并排式(平移拼接) 勾股定理

时间: 2025-09-25 08:26:07

你没瞎 😄,我懂你的意思:我刚才写的版本虽然画出了三个正方形,但摆放方式不够“直观”,看不出 两个小面积正好填满大面积

教科书里常见的两种直观证明:


🟦 方案一:并排式(平移拼接)


🟨 方案二:大正方形包围法


🚀 给你一个改进后的 SwiftUI 演示(方案二)

//
//  PythagorasSquareProof.swift
//  jianyi
//
//  Created by f on 2025/9/25.
//
import SwiftUI
struct PythagorasSquareProof: View {
@State private var a: Double = 3
@State private var b: Double = 4
var c: Double {
sqrt(a*a + b*b)
}
var body: some View {
VStack(spacing: 20) {
Text("勾股定理:a² + b² = c²")
.font(.headline)
.padding(.top)
GeometryReader { geo in
let scale = min(geo.size.width, geo.size.height) / (a+b+2)
let side = (a+b) * scale
ZStack {
// 大正方形外框
Rectangle()
.stroke(.black, lineWidth: 2)
.frame(width: side, height: side)
// 四个直角三角形
ForEach(0..<4) { i in
let angle = Double(i) * 90.0
RightTriangleShape(a: a*scale, b: b*scale)
.fill(Color.blue.opacity(0.4))
.rotationEffect(.degrees(angle))
.offset(x: side/2, y: side/2)
.rotationEffect(.degrees(-90), anchor: .center)
}
// 中间的 c² 正方形
Rectangle()
.fill(Color.orange.opacity(0.5))
.frame(width: c*scale, height: c*scale)
}
.frame(width: side, height: side)
.position(x: geo.size.width/2, y: geo.size.height/2)
}
.frame(height: 300)
VStack {
HStack {
Text("a = $Int(a))")
Slider(value: $a, in: 2...6, step: 1)
}
HStack {
Text("b = $Int(b))")
Slider(value: $b, in: 2...6, step: 1)
}
}
.padding()
Text("👉 橙色正方形面积 c² = 蓝色区域剩余面积 = a²+b²")
.font(.footnote)
.foregroundColor(.gray)
}
.navigationTitle("勾股定理演示")
}
}
struct RightTriangleShape: Shape {
var a: Double
var b: Double
func path(in rect: CGRect) -> Path {
var path = Path()
path.move(to: .zero)
path.addLine(to: CGPoint(x: a, y: 0))
path.addLine(to: CGPoint(x: 0, y: b))
path.closeSubpath()
return path
}
}
#Preview {
PythagorasSquareProof()
}

✅ 优化点


要不要我帮你把 动画加上,比如点击按钮时,四个三角形自动移动,显现出“a²+b² → c²”的转化过程?


上一个 平方和、平方差只是代数公式可视化的开端 高中物理知识列表 下一个 月亮的形状和“弦月”状态其实是天文学上的光照方向问题

物理热词


问答

Latest

工具

© 2019-现在 简易物理,让物理教学更简单

沪ICP备17002269号