Skip to content

Commit

Permalink
Alternating tree
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpi committed Dec 26, 2020
1 parent 26b3623 commit cd4d193
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions FractalTrees/Tree.fs
Expand Up @@ -8,9 +8,9 @@ let size = 1000
let drawTree file =
let rnd = new Random()
let img = new Bitmap(size, size)
let pen = new Pen(Color.Black, 1.f)
use g = Graphics.FromImage(img)
g.SmoothingMode <- SmoothingMode.HighQuality;
let pen = new Pen(Color.Black, 1.f)
g.FillRectangle(new SolidBrush(Color.White), 0, 0, size, size)
g.DrawRectangle(new Pen(Color.Black, 1.f), 0, 0, size - 1, size - 1)

Expand All @@ -22,18 +22,20 @@ let drawTree file =
g.DrawLine(pen, int startX, int startY, int nextX, int nextY)
(nextX, nextY)

let randomAngle() = rnd.NextDouble() * 0.6
let largeAngle = Math.PI / 2.4
let smallAngle = 0.2
let factor n = 0.6

let rec innerDrawTree n location direction length =
if n > 0 then
let location = drawTrunk location direction length
innerDrawTree (n - 1) location (direction + randomAngle()) (length * factor n)
innerDrawTree (n - 1) location (direction - randomAngle()) (length * factor n)
let op1, op2 = if n % 2 = 0 then (-), (+) else (+), (-)
innerDrawTree (n - 1) location (op1 direction largeAngle) (length * factor n)
innerDrawTree (n - 1) location (op2 direction smallAngle) (length * factor n)
else
drawLeaf()

innerDrawTree 10 ((float size) / 2., (float size) - 50.) Math.PI 300.
innerDrawTree 10 ((float size) / 2., (float size) - 50.) (Math.PI * 0.98) 300.

img.Save(file)

0 comments on commit cd4d193

Please sign in to comment.