From 322dff19976abd653a38b6c6cdc3f61018b3792c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= Date: Thu, 19 May 2022 20:56:43 +0200 Subject: [PATCH] thurtle: Add fractals --- src/web/thurtle/programs.ts | 70 +++++++++++++++++++++++++++++++++++++ src/web/thurtle/thurtle.tsx | 2 +- 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/src/web/thurtle/programs.ts b/src/web/thurtle/programs.ts index b54b6db..8739e02 100644 --- a/src/web/thurtle/programs.ts +++ b/src/web/thurtle/programs.ts @@ -133,6 +133,76 @@ PENUP -500 -180 SETXY PENDOWN 1 SPIRAL`, }, + { + name: "Snowflake", + isExample: true, + program: ` +850 CONSTANT LENGTH +4 CONSTANT DEPTH + +: SIDE ( length depth -- ) + DUP 0= IF + DROP FORWARD EXIT + THEN + SWAP 3 / SWAP 1- + 2DUP RECURSE + 60 LEFT 2DUP RECURSE + 120 RIGHT 2DUP RECURSE + 60 LEFT RECURSE +; + +: SNOWFLAKE ( -- ) + PENUP + LENGTH 4 / NEGATE + LENGTH 2/ NEGATE + SETXY + PENDOWN + 3 0 DO + LENGTH DEPTH SIDE + 120 RIGHT + LOOP +; + +SNOWFLAKE +`, + }, + { + name: "Plant", + isExample: true, + program: ` +300 CONSTANT SIZE +0 CONSTANT ANGLE +5 CONSTANT BRANCHES + +VARIABLE RND +HERE RND ! + +: RANDOM ( -- n ) + RND @ 31421 * 6927 + 65536 MOD + DUP RND ! +; + +: CHOOSE ( n1 -- n2 ) + RANDOM 65536 */MOD SWAP DROP +; + +: PLANT ( size angle -- ) + OVER 10 < IF 2DROP EXIT THEN + DUP RIGHT + OVER FORWARD + BRANCHES 0 DO + OVER 2/ + 160 CHOOSE 80 - + RECURSE + LOOP + SWAP BACKWARD + LEFT +; + +PENUP 0 SIZE NEGATE SETXY PENDOWN +SIZE ANGLE PLANT +`, + }, ].map((e) => ({ ...e, program: e.program.trimStart() })); // Load programs diff --git a/src/web/thurtle/thurtle.tsx b/src/web/thurtle/thurtle.tsx index 82d701a..79fc60c 100644 --- a/src/web/thurtle/thurtle.tsx +++ b/src/web/thurtle/thurtle.tsx @@ -372,7 +372,7 @@ function setVisible(b: boolean) { // Programs ////////////////////////////////////////////////////////////////////////////////////////// -const DEFAULT_PROGRAM = "Flower"; +const DEFAULT_PROGRAM = "Snowflake"; function loadProgram(name: string) { const program = getProgram(name)!;