Skip to content

Instantly share code, notes, and snippets.

@stepheng
Created August 11, 2023 21:13
Show Gist options
  • Save stepheng/f2d2f6aa9b4ecdfa5cc9b295f0b0026f to your computer and use it in GitHub Desktop.
Save stepheng/f2d2f6aa9b4ecdfa5cc9b295f0b0026f to your computer and use it in GitHub Desktop.
Godot L-System Koch Snowflake
extends Node2D
var axiom = "F++F++F++"
var angle_step = deg_to_rad(60)
var iteration = 1
var step_length: float = 64
var rules = {
"F": "F-F++F-F"
}
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
func _draw():
var start_x = get_window().size.x / 2
var position = Vector2(start_x, get_window().size.y)
var angle = angle_step
var current = LNode.new(angle, position, step_length)
var stack = []
for command in axiom:
match(command):
"F":
var delta = Vector2.UP.rotated(current.angle) * current.step_length
var end_point = current.position + delta
draw_line(current.position, end_point, Color.GREEN, 1.0)
current.position = end_point
"+":
current.angle -= angle_step
"-":
current.angle += angle_step
func _on_button_pressed():
iteration += 1
var new_axiom = ""
for command in axiom:
var rule = rules.get(command, command)
new_axiom += rule
axiom = new_axiom
queue_redraw()
func _on_shrink_pressed():
step_length /= 2
step_length = max(0.1, step_length)
queue_redraw()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment