/
main.go
87 lines (68 loc) · 1.91 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"fmt"
"math"
"time"
"github.com/GaryBrownEEngr/turtle"
"github.com/GaryBrownEEngr/turtle/turtlemodel"
)
// translated from the python tutorial found here: https://www.geeksforgeeks.org/y-fractal-tree-in-python-using-turtle/#
func main() {
params := turtle.Params{Width: 600, Height: 400}
turtle.Start(params, drawFunc)
}
const (
LevelCount = 16
StartSize = 80
TrunkWidth = 15
)
func drawFunc(window turtle.Window) {
time.Sleep(time.Second * 1)
fmt.Println("Creating Turtle")
t := window.NewTurtle()
t.ShapeAsArrow()
t.ShapeScale(.25)
t.DegreesMode()
// t.Speed(turtlemodel.MaxSpeed)
t.ShowTurtle()
go RecursiveTree(t, 0, -180, 90, StartSize, LevelCount)
// go turtle.CreateGif(window, time.Millisecond*200, time.Millisecond*200, "./examples/fractaltree/fractaltree.gif", 75)
}
func dualRate(x1, x2, y1, y2, in float64) float64 {
slope := (y2 - y1) / (x2 - x1)
deltaX := in - x1
ret := deltaX*slope + y1
return ret
}
func RecursiveTree(t turtlemodel.Turtle, x, y, angle, length float64, level int) {
if level <= 0 {
return
}
t.PenUp()
t.Angle(angle)
t.GoTo(x, y)
t.PenDown()
width := TrunkWidth * math.Pow(.75, float64(LevelCount-level))
t.Size(width)
// splitting the rgb range for green
// into equal intervals for each level
// setting the color according
// to the current level
ratio := dualRate(LevelCount, 1, 0, 1, float64(level))
c := turtle.LerpColor(turtle.SaddleBrown, turtle.Lime, ratio)
t.Color(c)
t.Forward(length)
curX, curY := t.GetPos()
curAngle := t.GetAngle()
if level > LevelCount-9 {
go RecursiveTree(t.Clone(), curX, curY, curAngle+30, 0.8*length, level-1)
go RecursiveTree(t.Clone(), curX, curY, curAngle-30, 0.8*length, level-1)
t.HideTurtle()
} else {
RecursiveTree(t, curX, curY, curAngle+30, 0.8*length, level-1)
RecursiveTree(t, curX, curY, curAngle-30, 0.8*length, level-1)
}
if level == LevelCount-9 {
t.HideTurtle()
}
}