I'm trying to write some code to draw arithmatical functions, like sine(x), or log(x).
The problem I encounter is that in order to draw it you have to iterate over a range of numbers, to create a Path. In the swiftUI environment ForEach cannot be used, because tou have to return a view after each iteration. A function, returning a Path-object doesn't do me any good, because it can apparently not been drawn, or is not accepted as a Path object.
So I'm stuck. The only solution I can think of right now is to stick with do old way and not use SwiftUi for the UI.
So the question is is there a way to draw something like this using SwiftUI.
My code so far (simplified) is :
A function returning a CGPoint :
func SimpleLine(x: Double) -> CGPoint {
var y: Double = log(x + 1)
return CGPoint(x: x, y: y)
A function creating a Path object :
func fillPath() -> Path {
var path: Path = Path()
path.move(to: CGPoint(x: 20, y: 0))
for x in 0..<100 {
let pnt: CGPoint = SimpleLine(x: Double(x))
print("(pnt)")
path.addLine(to: pnt)
}
path.closeSubpath()
return path
}
And the only way I could imagine to implement it:
var body: some View {
NavigationView ()
{
fillPath()
.stroke(Color.green)
}
}