From 8edc8fe180c08cb48ae09a8ff9f450ec79d57e61 Mon Sep 17 00:00:00 2001 From: Simon Brooke Date: Sat, 30 Aug 2025 18:48:42 +0100 Subject: [PATCH] Added `set-nib!` for variable width lines. --- README.md | 2 +- index.html | 28 +++++++++++++++++++--------- resources/cljs/tittle.cljs | 9 ++++++++- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 51076ec..7cb75d1 100644 --- a/README.md +++ b/README.md @@ -107,7 +107,7 @@ Turn the turtle clockwise by this `angle`, expressed in degrees with respect to Turn the turtle to face `angle`, expressed in degrees with respect to the X axis. If `angle` is not a number, throw an exception. -**TODO: Note that 180° is currently straight up; this is not intended, it is intended that 0° should be straight up, and this change will be made +**TODO**: Note that 180° is currently straight up; this is not intended, it is intended that 0° should be straight up, and this change will be made before version 1.0. ## Licence diff --git a/index.html b/index.html index 7797a01..4105eae 100644 --- a/index.html +++ b/index.html @@ -7,10 +7,10 @@

Tittle: Turtles in Scittle

@@ -73,7 +73,7 @@ "Turn the turtle to face `angle`, expressed in degrees with respect to the X axis. If `angle` is not a number, throw an exception. - **TODO: Note that 180° is currently straight up; this is not intended, it + **TODO**: Note that 180° is currently straight up; this is not intended, it is intended that 0° should be straight up, and this change will be made before version 1.0." [angle] @@ -127,6 +127,7 @@ (.setAttribute elt "x2" x) (.setAttribute elt "y2" y) (.setAttribute elt "stroke" (or (:ink @turtle) "blue")) + (.setAttribute elt "stroke-width" (or (:nib @turtle) 3)) (.appendChild playing-field elt))) (swap! turtle assoc :x x :y y)))) @@ -173,6 +174,13 @@ (when (string? colour) (swap! turtle assoc :ink colour))) + + (defn set-nib! + "Set the nib (width) of the pen to this `n`, which should be a number." + [n] + (when (number-or-error! n) + (swap! turtle assoc :nib n))) + (defn draw-tree! "Draw a tree. This is a fairly crude tree-drawing algorithm; there's lots of ways it can be improved, consider it a place @@ -194,6 +202,7 @@ (log-turtle!) (when (> depth 0) (pen-down!) + (set-nib! (if (<= depth 2) 4 (- depth 2))) (set-ink! (if (<= depth 2) "green" "brown")) (move! length) (turn! curvature) @@ -241,16 +250,17 @@ (log-turtle!) (pen-up!) (log-turtle!) - (move-to! 350 700) + (move-to! 350 500) (log-turtle!) - (turn-to! 180) - (draw-tree! 100 20 16 8 0.2 0.8 8) - (set-ink! "blue") (doall (map #(let [v (* 16 (mod % 16)) - c (str "rgb(" v "," v "," (- 256 v)")")] + c (str "rgb(" v "," 128 "," (- 256 v)")")] (set-ink! c) (draw-polygon! % 100)) - (range 3 16))) + (range 3 24))) + (turn-to! 180) + (set-ink! "blue") + (set-nib! 2) + (draw-tree! 100 20 16 8 0.2 0.8 9) \ No newline at end of file diff --git a/resources/cljs/tittle.cljs b/resources/cljs/tittle.cljs index d6caee2..291fe9e 100644 --- a/resources/cljs/tittle.cljs +++ b/resources/cljs/tittle.cljs @@ -1,6 +1,6 @@ (ns tittle) - (def turtle (atom {:theta 0 :x 0 :y 0 :pen :up :ink "blue"})) +(def turtle (atom {:theta 0 :x 0 :y 0 :pen :up :nib 3 :ink "blue"})) (defn log-turtle! "Prints the state of the turtle to the browser console." @@ -105,6 +105,7 @@ (.setAttribute elt "x2" x) (.setAttribute elt "y2" y) (.setAttribute elt "stroke" (or (:ink @turtle) "blue")) + (.setAttribute elt "stroke-width" (or (:nib @turtle) 3)) (.appendChild playing-field elt))) (swap! turtle assoc :x x :y y)))) @@ -151,6 +152,12 @@ (when (string? colour) (swap! turtle assoc :ink colour))) +(defn set-nib! + "Set the nib (width) of the pen to this `n`, which should be a number." + [n] + (when (number-or-error! n) + (swap! turtle assoc :nib n))) + (defn draw-tree! "Draw a tree. This is a fairly crude tree-drawing algorithm; there's lots of ways it can be improved, consider it a place