extend("kanvas")
extend("gpu")
extend("math")

win = kanvas:create([
  title: "GPU rotate",
  width: 800,
  height: 600,
  resize: false
])

res = kanvas:display(win)

angle = -3.141592653589793 / 4.0
c = math:cos(angle)
s = math:sin(angle)

rot = [
  [c, -s],
  [s,  c]
]

// Points as 2x3 matrix (rows: x,y; cols: points)
//triangle_t = [
//  [150.0, 350.0, 220.0],
//	[150.0, 127.9, 320.0]
//]

triangle_t = [
  [150.0, 150.0],
  [350.0, 180.0],
  [220.0, 300.0]
]

rot_tensor = gpu:to_tensor(rot)
tri_tensor = gpu:to_tensor(triangle_t)

sput("rot_tensor: ")
slog(rot_tensor)
sput("tri_tensor: ")
slog(tri_tensor)

rotated_tensor = gpu:multiply(rot_tensor, tri_tensor) // Correct order

rotated = gpu:to_array(rotated_tensor)

// rotated is 2x3, so transpose to list of points [[x,y], ...]
rotated_points = [
  [rotated[0][0], rotated[1][0]],
  [rotated[0][1], rotated[1][1]],
  [rotated[0][2], rotated[1][2]]
]

kanvas:triangle(win, rotated_points, "#FF00FF")
kanvas:refresh(win)
