-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree.script
60 lines (49 loc) · 1.81 KB
/
Tree.script
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
implicit def Float2int(f:Float):Int = f.intValue
implicit def double2Float(d:Double):Float = d.floatValue
def choice[T](a:Array[T]):T =
a(random(0,a.length).floor)
class Turtle (x0:Float,y0:Float){
import java.awt._
import java.awt.geom._
var pos:Point = new Point(x0,y0);
var phi:Float = 0;
var parent:Turtle = null
def this(p:Turtle) = {
this(p.pos.x,p.pos.y)
phi = p.phi
parent = p
}
def advance(len:Float) =
new Point(
pos.x + len * Math.cos(phi)
,pos.y + len * Math.sin(phi))
def line(length:Float) = {
val newPos = advance(length)
g2d.draw(new Line2D.Float(pos,newPos))
pos = newPos
this
}
def rotate(grad:Float) = {
phi = phi + 2*Math.PI*grad/360
this
}
def pop:Turtle = parent
def push:Turtle = new Turtle(this)
}
def baum(depth:Int,length:Float,t:Turtle):Unit =
if (depth > 0 ){
g2d.setStroke(new java.awt.BasicStroke(depth,1,1));
hsb(0.6,0.7,0.4,depth.floatValue/10);
t.line(length)
//t.rotate(-15)
val r = (10-depth).floatValue/10f*arg("winkel",0f,360f,70)
baum(depth-1,length * arg("krumm",0f,1f,0.9f),t.rotate(random(-r,r)).push)
baum(depth-1,length * arg("gerade",0f,1f,0.8f),t.push)
}
g2d.setPaint(new java.awt.RadialGradientPaint(width/2,height/2,width/2
,Array(0f,1f)
,Array(hsb(0.33,0.1,1,0.2),hsb(0.33,0.1,1,0.8))))
rect(0,0,width,height)
val t = new Turtle(width/2,height);
t.rotate(-90)
baum(arg("Stufen",0f,10f,10f),arg("Stammlaenge",0f,200f,100f),t)