extend("kanvas")
extend("matrix")
extend("math")

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

res = kanvas:display(win)

// Define the rotation angle (-π/4 radians = -45 degrees)
angle = -3.141592653589793 / 4

// Compute cosine and sine using math plugin
c = math:cos(angle)
s = math:sin(angle)

// Construct the 2x2 rotation matrix
rot = [
  [c,  -s],
  [s,   c]
]


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

/*
triangle = [
  [150.0, 150.0],
  [350.0, 180.0],
  [220.0, 300.0]
]
*/

// Multiply the triangle points by the rotation matrix
// Note: matrix:multiply expects (M×N) * (N×P)
// Here: triangle is 3×2, rot is 2×2, so multiply triangle * rot
rotated = matrix:multiply(rot, triangle)

slog( rotated )

// Draw the rotated triangle
kanvas:triangle(win, rotated, "#FF00FF")

kanvas:refresh(win)

