I am trying to use a simple ForEach loop to draw circles for a collection of points. I am flabbergasted that the compiler borks on something so simple, so I suspect a bug in my code, but I cannot see it.
This is all done within a View in a GeometryReader in which I have several let constants that compute correct positions.
If I do the following, it works, but the circles are all bunched on the left side of the view since the position is not scaled properly:
let points: [CGPoint] = curve.makePoints()
// Text(" \(points[0].x), \(points[0].y) \(points[1].x), \(points[1].y)")
ForEach(points, id: \.self.x) { point in
Circle()
.position(point)
.frame(width: shapeSize, height: shapeSize)
}
The commented out Text did show the correct values of the first two points.
However, if I scale the position to where I want it to be, as below, I get the dreaded error. The only change between code that builds and runs and the code that breaks the compiler are in these two code blocks.
ForEach(points, id: \.self.x) { point in
var nnextX: Double = xOffset + pixWidth * ((point.x - xminRounded) / xtot)
var nnextY: Double = Double(proxy.size.height) - yOffset - pixHeight * ((points.y - yminRounded) / ytot)
var nextPoint:CGPoint = CGPoint(x: nnextX, y: nnextY)
Circle()
.position(nextPoint)
// .position(point)
.frame(width: shapeSize, height: shapeSize)
}
I know the point.x value is unique so I can use it as the id:
I know the logic for computing "nnextX" and "nnextY" are correct since when I used very similar computations in a
Path { path in
...
for i in 1..<curve.xValues.count {
let nextX: Double = xOffset + pixWidth * ((curve.xValues[i] - xminRounded) / xtot)
let nextY: Double = Double(proxy.size.height) - yOffset - pixHeight * ((curve.yValues[i] - yminRounded) / ytot)
let nextPoint:CGPoint = CGPoint(x: nextX, y: nextY)
path.addLine(to: nextPoint)
}
All the lines were drawn in the proper place in my View.
Finally, the developer documentation shows 3 ways to initialize a CGPoint: with a Double, with an Int, or with a CGFloat, so I should be good there.
Any ideas?