L-system: Difference between revisions
Content added Content deleted
(Realize in F#) |
(Added Wren) |
||
Line 127: | Line 127: | ||
[[File:Fōrmulæ - L-system - Koch's snowflake 02.png]] |
[[File:Fōrmulæ - L-system - Koch's snowflake 02.png]] |
||
=={{header|Wren}}== |
|||
{{libheader|DOME}} |
|||
{{libheader|Wren-lsystem}} |
|||
The source code for the Wren-lsystem module is available on this site by clicking the above link and then navigating to the Talk page. |
|||
<syntaxhighlight lang="wren">import "graphics" for Canvas, Color |
|||
import "dome" for Window |
|||
import "math" for Math |
|||
import "./lsystem" for LSystem, Rule |
|||
var TwoPi = Num.pi * 2 |
|||
class SierpinskiSquareCurve { |
|||
construct new(width, height, back, fore) { |
|||
Window.title = "Koch Snowflake" |
|||
Window.resize(width, height) |
|||
Canvas.resize(width, height) |
|||
_w = width |
|||
_h = height |
|||
_bc = back |
|||
_fc = fore |
|||
} |
|||
init() { |
|||
Canvas.cls(_bc) |
|||
var cx = 80 |
|||
var cy = 270 |
|||
var theta = Num.pi/2 |
|||
var h = 9 |
|||
var lsys = LSystem.new( |
|||
["F"], // variables |
|||
["+", "-"], // constants |
|||
"F--F--F", // axiom |
|||
[Rule.new("F", "F+F--F+F")], // rules |
|||
Num.pi / 3 // angle (60 degrees in radians) |
|||
) |
|||
var result = lsys.iterate(3) |
|||
var operations = { |
|||
"F": Fn.new { |
|||
var newX = cx + h*Math.sin(theta) |
|||
var newY = cy - h*Math.cos(theta) |
|||
Canvas.line(cx, cy, newX, newY, _fc, 2) |
|||
cx = newX |
|||
cy = newY |
|||
}, |
|||
"+": Fn.new { |
|||
theta = (theta + lsys.angle) % TwoPi |
|||
}, |
|||
"-": Fn.new { |
|||
theta = (theta - lsys.angle) % TwoPi |
|||
} |
|||
} |
|||
LSystem.execute(result, operations) |
|||
} |
|||
update() {} |
|||
draw(alpha) {} |
|||
} |
|||
var Game = SierpinskiSquareCurve.new(400, 400, Color.blue, Color.yellow)</syntaxhighlight> |
|||
[[File:Wren-Koch_Snowflake.png]] |