平面直角坐标系 SwiftUI

第一部分

第二部分
代码1


import SwiftUI
@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            AxisView()
        }
    }
}

代码2


import SwiftUI
struct AxisView: View {
    var body: some View {
        GeometryReader { geometry in
            ZStack {
                // X轴(X-Axis)
                Path { path in
                    path.move(to: CGPoint(x: 0, y: geometry.size.height / 2))
                    path.addLine(to: CGPoint(x: geometry.size.width, y: geometry.size.height / 2))
                }
                .stroke(Color.blue, lineWidth: 2)
                // X轴正方向刻度(X-Axis positive direction scale)
                let numXTickMarksPos = Int(geometry.size.width / 40) / 2
                ForEach(1...numXTickMarksPos, id: \.self) { index in
                    Path { path in
                        let xPos = CGFloat(index) * 40 + geometry.size.width / 2
                        path.move(to: CGPoint(x: xPos, y: geometry.size.height / 2 - 5))
                        path.addLine(to: CGPoint(x: xPos, y: geometry.size.height / 2))
                    }
                    .stroke(Color.red, lineWidth: 1)
                    
                    Text("\(index * 5)")
                        .position(x: CGFloat(index) * 40 + geometry.size.width / 2, y: geometry.size.height / 2 + 15)
                }
                // X轴负方向刻度(X-Axis negative direction scale)
                let numXTickMarksNeg = Int(geometry.size.width / 40) / 2
                ForEach(-numXTickMarksNeg...(-1), id: \.self) { index in
                    Path { path in
                        let xPos = CGFloat(index) * 40 + geometry.size.width / 2
                        path.move(to: CGPoint(x: xPos, y: geometry.size.height / 2 - 5))
                        path.addLine(to: CGPoint(x: xPos, y: geometry.size.height / 2))
                    }
                    .stroke(Color.red, lineWidth: 1)
                    
                    Text("\(index * 5)")
                        .position(x: CGFloat(index) * 40 + geometry.size.width / 2, y: geometry.size.height / 2 + 15)
                }
                
                // Y轴(Y-Axis)
                Path { path in
                    path.move(to: CGPoint(x: geometry.size.width / 2, y: 0))
                    path.addLine(to: CGPoint(x: geometry.size.width / 2, y: geometry.size.height))
                }
                .stroke(Color.blue, lineWidth: 2)
                // Y轴正方向刻度(Y-Axis positive direction scale)
                let numYTickMarksNeg = Int(geometry.size.height / 40) / 2
                ForEach(-numYTickMarksNeg...(-1), id: \.self) { index in
                    Path { path in
                        let yPos = CGFloat(index) * 40 + geometry.size.height / 2
                        path.move(to: CGPoint(x: geometry.size.width / 2 - 5, y: yPos))
                        path.addLine(to: CGPoint(x: geometry.size.width / 2, y: yPos))
                    }
                    .stroke(Color.red, lineWidth: 1)
                    Text("\(abs(index) * 5)")
                        .position(x: geometry.size.width / 2 + 20, y: CGFloat(index) * 40 + geometry.size.height / 2)
                }
                // Y轴负方向刻度(Y-Axis negative direction scale)
                let numYTickMarksPos = Int(geometry.size.height / 40) / 2
                ForEach(1...numYTickMarksPos, id: \.self) { index in
                    Path { path in
                        let yPos = CGFloat(index) * 40 + geometry.size.height / 2
                        path.move(to: CGPoint(x: geometry.size.width / 2 - 5, y: yPos))
                        path.addLine(to: CGPoint(x: geometry.size.width / 2, y: yPos))
                    }
                    .stroke(Color.red, lineWidth: 1)
                    Text("-\(index * 5)")
                        .position(x: geometry.size.width / 2 + 20, y: CGFloat(index) * 40 + geometry.size.height / 2)
                }
            }
        }
    }
}
2 个赞

哇哦 好棒啊! 感觉你可以再出一个App来教高数了 因为图像会比公式更直观地展示信息

1 个赞

这确实是平面数学图像的一部分,但显然,这个app并不稳定,而且市面上也已经有更好的app,所以打算放弃研究。基于目前研究,想完成的是AR的图像分析(但市面上也有此类app,二维三维都有)。花费大量时间制轮子,并非我所愿。或许可以用于小AR游戏,实现如王国之泪的某些效果。

1 个赞