'Drawing 2 dots and a line between them in SwiftUI

Swift 5.5 iOS 15

Drawing 2 dots at random places, noting their origins and then trying to use the information to draw a line between them.

Almost there, and yet the result is wrong? What am I doing wrong?

struct ContentView: View {
@State var cords:[CGPoint] = []
@State var showMe = false
var body: some View {
    ZStack {
        ForEach((0..<2), id:\.self) {foo in
            Circle()
                .fill(Color.blue)
                .frame(width: 64, height: 64)
            
                .background(GeometryReader { geo in
                    Circle()
                        .stroke(Color.red, lineWidth: 2)
                        .frame(width: 64, height: 64)
                        .onAppear {
                            cords.append(CGPoint(x: geo.frame(in: .global).origin.x, y: geo.frame(in: .global).origin.y))
                        }
                })
                .position(returnRandom())
                .task {
                    if cords.count > 1 {
                        print("cords \(cords)")
                        showMe = true
                    }
                }
        }
    }
    ZStack {
        if showMe {
            Connected(cords: $cords)
                .stroke(Color.black, lineWidth: 1)
        }
    }
}
func returnRandom() -> CGPoint {
    let x = Double.random(in: 0..<width)
    let y = Double.random(in: 0..<height)
    return CGPoint(x: x, y: y)
}
}

struct Connected: Shape {
@Binding var cords:[CGPoint]
func path(in rect: CGRect) -> Path {
    var path = Path()
    path.move(to: cords[0])
    path.addLine(to: cords[1])
    return path
}
}

Also tried ...

cords.append(CGPoint(x: geo.frame(in: .global).midX, y: geo.frame(in: .global).midY))


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source