Added set-nib! for variable width lines.

This commit is contained in:
Simon Brooke 2025-08-30 18:48:42 +01:00
parent 1b5aeeb52e
commit 8edc8fe180
3 changed files with 28 additions and 11 deletions

View file

@ -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

View file

@ -7,10 +7,10 @@
<body>
<h1>Tittle: Turtles in <a href="https://github.com/babashka/scittle/tree/main">Scittle</a></h1>
<svg id="playing-field"
stype="width: 700; height: 700"
stype="width: 700; height: 900"
version="1.1"
width="700"
height="700"
height="900"
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg">
@ -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&deg; is currently straight up; this is not intended, it
**TODO**: Note that 180&deg; is currently straight up; this is not intended, it
is intended that 0&deg; 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)
</script>
</body>
</html>

View file

@ -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