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

// Create a 600x600 window with title "Rotating Triangle"
win = kanvas:create([width: 400, height: 400, title: "Rotating Triangle"])


// Define original triangle vertices
x1 = 100
y1 = 100
x2 = 300
y2 = 100
x3 = 200
y3 = 300

first = [[x1, y1], [x2, y2], [x3, y3]]

// Color as a hex string
color = "#FF00FF00"  // opaque green

// Draw the triangle and store its item handle
triangle_handle = kanvas:triangle(win, first, color)


res = kanvas:display(win)

kanvas:refresh(win)

// Function to rotate a point (x,y) by angle degrees about origin (0,0)
rotate_point = (x, y, angle) => {
    rad = angle * 3.141592653589793 / 180.0
    cos_a = math:cos(rad)
    sin_a = math:sin(rad)
    nx = x * cos_a - y * sin_a
    ny = x * sin_a + y * cos_a
    [nx, ny]
}

p1 = rotate_point(x1, y1, 15)
p2 = rotate_point(x2, y2, 15)
p3 = rotate_point(x3, y3, 15)

slog( p1[0] )


new = [p1,p2,p3]

slog( new )

// Remove old triangle
//kanvas:remove(win, triangle_handle)
// Draw rotated triiangle
new_triangle_handle = kanvas:triangle(win, [[p1[0], p1[1]], [x2, y2], [x3, y3]], "#FF0000")

// Refresh the window to update display
kanvas:refresh(win)

