package main

import (
	rl "github.com/gen2brain/raylib-go/raylib"
)

type SierpinskiArrow struct {
	dirty bool
}

func (s *SierpinskiArrow) Draw(ctx *RenderCtx) {
	rl.Translatef(float32(ctx.SourceWidth)/2.0, float32(ctx.SourceHeight)/2.0, 0)
	rl.ClearBackground(rl.NewColor(0, 0, 0, 0))
	sierpinskiArrow(ctx, int(ctx.Ports["sierpinskiArrowDepth"]), ctx.Ports["sierpinskiArrowLength"])
}

func (s *SierpinskiArrow) Update(ctx *RenderCtx) {
	s.dirty = true
}

func (s *SierpinskiArrow) IsDirty() bool {
	return s.dirty
}

func sierpinskiArrow(ctx *RenderCtx, order int, length float64) {
	if order == 0 {
		curve(ctx, order, length, ctx.Ports["sierpinskiArrowAngle"])
	} else {
		rl.Rotatef(float32(ctx.Ports["sierpinskiArrowAngle"]), 0, 0, 1)
		curve(ctx, order, length, -ctx.Ports["sierpinskiArrowAngle"])
	}
}

func curve(ctx *RenderCtx, order int, length float64, angle float64) {
	if order == 0 {
		len := int32(length)
		rl.SetLineWidth(4)
		rl.DrawLine(0, 0, len, 0, rl.RayWhite)
		rl.Translatef(float32(length), 0, 0)
	} else {
		curve(ctx, order-1, length/2, -angle)
		rl.Rotatef(float32(angle), 0, 0, 1)
		curve(ctx, order-1, length/2, angle)
		rl.Rotatef(float32(angle), 0, 0, 1)
		curve(ctx, order-1, length/2, -angle)
	}
}
