Files
sumi/sierpinski.go
2025-12-23 11:08:12 -06:00

52 lines
1.3 KiB
Go

package main
import (
"fmt"
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) {
fmt.Printf("drawing SierpinskiArrow, order = %d, length = %.2f\n", order, length)
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)
}
}