You can probably shorten the code even more by:
* using implicit result variable
[https://peterme.net/tips-and-tricks-with-implicit-return-in-nim.html](https://peterme.net/tips-and-tricks-with-implicit-return-in-nim.html)
* using the fact that arrays start at []
* doing the 0th case of the for loop
import vmath
type
Seg = array[4, Vec2]
Spline = object
t: seq[float32]
xy: seq[Vec2]
v: seq[Vec2]
a: seq[Vec2]
length: seq[float32]
proc bezier(segment: Seg, res: int): Spline =
let
t1 = 1 / float32(res)
t2 = t1 * t1
t3 = t2 * t1
b0 = segment[0]
b1 = segment[1]
b2 = segment[2]
b3 = segment[3]
a = -b0 + 3 * b1 + -3 * b2 + b3
b = 3 * b0 + -6 * b1 + 3 * b2
c = -3 * b0 + 3 * b1
var
d = b0
fd1 = a * t3 + b * t2 + c * t1 #initial velocity
fd2 = 6 * a * t3 + 2 * b * t2 #initial acceleration
fd3 = 6 * a * t3 #acceleration change
for i in 0 .. res:
result.t.add(float32(i)*t1)
result.xy.add(d)
result.v.add(fd1)
result.a.add(fd2)
result.length.add(length(d))
d += fd1
fd1 += fd2 #speed
fd2 += fd3 #acceleration
Run