Skip to content

Instantly share code, notes, and snippets.

@noprompt
Last active May 22, 2019 16:33
Show Gist options
  • Select an option

  • Save noprompt/3b17bc7a97e2369f27166e1e5a356e31 to your computer and use it in GitHub Desktop.

Select an option

Save noprompt/3b17bc7a97e2369f27166e1e5a356e31 to your computer and use it in GitHub Desktop.

Revisions

  1. noprompt revised this gist May 22, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion deps.edn
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    org.clojure/clojurescript {:mvn/version "1.10.439"}
    org.clojure/test.check {:mvn/version "0.10.0-alpha3"}
    com.google/clojure-turtle {:mvn/version "0.3.0"}
    meander/delta {:mvn/version "0.0.76"}
    meander/delta {:mvn/version "0.0.85"}
    quil/quil {:mvn/version "3.0.0"}}
    :aliases {:test {:extra-paths ["test"]
    :extra-deps {org.clojure/test.check {:mvn/version "0.10.0-alpha3"}
  2. noprompt revised this gist May 22, 2019. No changes.
  3. noprompt revised this gist May 20, 2019. 1 changed file with 23 additions and 4 deletions.
    27 changes: 23 additions & 4 deletions topiary.clj
    Original file line number Diff line number Diff line change
    @@ -57,37 +57,56 @@

    (def koch-island
    {:axiom '[F - F - F - F]
    :productions
    (r/rewrite
    F [F - F + F + F F - F - F + F]
    ?X ?X)})
    :n 3
    90
    :productions (r/rewrite
    F [F - F + F + F F - F - F + F]
    ?X ?X)})

    (def example-a
    {:axiom '[F - F - F - F]
    :n 4
    90
    :productions (r/rewrite
    F [F F - F - F - F - F - F + F]
    ?X ?X)})

    (def example-b
    {:axiom '[F - F - F - F]
    :n 4
    90
    :productions (r/rewrite
    F [F F - F - F - F - F F]
    ?X ?X)})

    (def example-c
    {:axiom '[F - F - F - F]
    :n 3
    90
    :productions (r/rewrite
    F [F F - F + F - F - F F]
    ?X ?X)})

    (def example-d
    {:axiom '[F - F - F - F]
    :n 4
    90
    :productions (r/rewrite
    F [F F - F - - F - F]
    ?X ?X)})

    (def example-e
    {:axiom '[F - F - F - F]
    :n 5
    90
    :productions (r/rewrite
    F [F - F F - - F - F]
    ?X ?X)})

    (def example-f
    {:axiom '[F - F - F - F]
    :n 4
    90
    :productions (r/rewrite
    F [F - F + F - F - F]
    ?X ?X)})
  4. noprompt revised this gist May 20, 2019. 1 changed file with 12 additions and 6 deletions.
    18 changes: 12 additions & 6 deletions topiary.clj
    Original file line number Diff line number Diff line change
    @@ -155,11 +155,14 @@
    G F
    ?X ?X)})

    (comment
    (let [n 1]
    (turtle/new-window {:size [(* n 1024) (* n 768)]}))

    (let [X plant-d
    (defn make-window
    ([]
    (make-window 1))
    ([scale]
    (turtle/new-window {:size [(* scale 1024) (* scale 768)]})))

    (defn run-specification [specification]
    (let [X specification
    d 3
    n (:n X)
    δ ( X)
    @@ -170,4 +173,7 @@
    (turtle/clean)
    (turtle/home)
    (turtle/setxy 0 -380)
    (interpret d δ (system (:axiom X)))))
    (interpret d δ (system (:axiom X)))))


    (comment (run-specification plant-e))
  5. noprompt revised this gist May 20, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion topiary.clj
    Original file line number Diff line number Diff line change
    @@ -170,4 +170,4 @@
    (turtle/clean)
    (turtle/home)
    (turtle/setxy 0 -380)
    (interpret d δ (plant-d-system (:axiom X)))))
    (interpret d δ (system (:axiom X)))))
  6. noprompt revised this gist May 20, 2019. 2 changed files with 106 additions and 16 deletions.
    2 changes: 1 addition & 1 deletion deps.edn
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    org.clojure/clojurescript {:mvn/version "1.10.439"}
    org.clojure/test.check {:mvn/version "0.10.0-alpha3"}
    com.google/clojure-turtle {:mvn/version "0.3.0"}
    meander/delta {:mvn/version "0.0.65"}
    meander/delta {:mvn/version "0.0.76"}
    quil/quil {:mvn/version "3.0.0"}}
    :aliases {:test {:extra-paths ["test"]
    :extra-deps {org.clojure/test.check {:mvn/version "0.10.0-alpha3"}
    120 changes: 105 additions & 15 deletions topiary.clj
    Original file line number Diff line number Diff line change
    @@ -3,20 +3,21 @@
    [meander.strategy.delta :as r]
    [clojure-turtle.core :as turtle]))

    (turtle/new-window {:size [1024 768]})

    ;; F Move forward drawing a line.
    ;; f Move forward without drawing a line.
    ;; + Turn left by angle δ.
    ;; - Turn right by angle δ.
    ;; ( Push turtle state
    ;; ) Restore turtle state

    (defn interpret [d δ instruction]
    (r.match/find instruction
    F
    (turtle/forward d)

    f
    (do (turtle/penup) (turtle/forward d)
    (do (turtle/penup)
    (turtle/forward d)
    (turtle/pendown))

    +
    @@ -25,17 +26,34 @@
    -
    (turtle/right δ)

    [_ ..1 :as ?instructions]
    [_ ... :as ?instructions]
    (run!
    (fn [?instruction]
    (interpret d δ ?instruction))
    ?instructions)))
    ?instructions)

    (!instructions ... :as ?block)
    (r.match/match (deref turtle/turtle)
    {:angle ?angle
    :color ?color
    :fill ?fill
    :x ?x
    :y ?y}
    (do (interpret d δ !instructions)
    (if ?fill
    (do (turtle/end-fill)
    (turtle/setxy ?x ?y)
    (turtle/setheading ?angle)
    (turtle/color ?color)
    (turtle/start-fill))
    (do (turtle/setxy ?x ?y)
    (turtle/setheading ?angle)
    (turtle/color ?color)))))))

    (defn l-system [s n]
    (if (= n -1)
    (if (= n 0)
    identity
    (fn [t]
    ((r/all (l-system s (dec n))) (s t)))))
    (apply r/pipe (repeat n (r/bottom-up s)))))

    (def koch-island
    {:axiom '[F - F - F - F]
    @@ -74,10 +92,82 @@
    F [F - F + F - F - F]
    ?X ?X)})

    (let [X example-a
    n 3
    δ 90
    system (l-system (:productions X) n)]
    (turtle/clean)
    (turtle/setxy 0 0)
    (interpret 10 δ (system (:axiom X))))
    (def plant-a
    {:axiom 'F
    :n 5
    25.7
    :productions (r/rewrite
    F [F (+ F) F (- F) F]
    ?X ?X)})

    (def plant-b
    {:axiom 'F
    :n 5
    20
    :productions (r/rewrite
    F [F (+ F) F (- F) (F)]
    ?X ?X)})

    (def plant-c
    {:axiom 'F
    :n 4
    22.5
    :productions (r/rewrite
    F [F F - (- F + F + F) + (+ F - F - F)]
    ?X ?X)})

    (def plant-d
    {:axiom 'X
    :n 7
    20
    :productions (r/rewrite
    X [F (+ X) F (- X) + X]
    F [F F]
    ?X ?X)})

    (def plant-e
    {:axiom 'X
    :n 7
    25.7
    :productions (r/rewrite
    X [F (+ X) (- X) F X]
    F [F F]
    ?X ?X)})

    (def plant-f
    {:axiom 'X
    :n 5
    22.5
    :productions (r/rewrite
    X [F - ((X) + X) + F (+ F X) - X]
    F [F F]
    ?X ?X)})

    (def sierpenski-triangle
    {:axiom '[F - G - G]
    :n 6
    120
    :productions (r/rewrite
    F [F - G + F + G - F]
    G [G G]
    ?X ?X)
    :post-productions (r/rewrite
    G F
    ?X ?X)})

    (comment
    (let [n 1]
    (turtle/new-window {:size [(* n 1024) (* n 768)]}))

    (let [X plant-d
    d 3
    n (:n X)
    δ ( X)
    system (l-system (:productions X) n)
    system (if-some [post-production (:post-productions X)]
    (r/pipe system (r/bottom-up post-production))
    system)]
    (turtle/clean)
    (turtle/home)
    (turtle/setxy 0 -380)
    (interpret d δ (plant-d-system (:axiom X)))))
  7. noprompt created this gist May 13, 2019.
    12 changes: 12 additions & 0 deletions deps.edn
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    {:paths ["src"]
    :deps {org.clojure/clojure {:mvn/version "1.10.0"}
    org.clojure/clojurescript {:mvn/version "1.10.439"}
    org.clojure/test.check {:mvn/version "0.10.0-alpha3"}
    com.google/clojure-turtle {:mvn/version "0.3.0"}
    meander/delta {:mvn/version "0.0.65"}
    quil/quil {:mvn/version "3.0.0"}}
    :aliases {:test {:extra-paths ["test"]
    :extra-deps {org.clojure/test.check {:mvn/version "0.10.0-alpha3"}
    com.cognitect/test-runner {:git/url "https://github.com/healthfinch/test-runner"
    :sha "1d0cb97a14152959cdb7c1e8539a1759a1663f5b"}}
    :main-opts ["-m" "cognitect.test-runner"]}}}
    83 changes: 83 additions & 0 deletions topiary.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,83 @@
    (ns topiary.core
    (:require [meander.match.delta :as r.match]
    [meander.strategy.delta :as r]
    [clojure-turtle.core :as turtle]))

    (turtle/new-window {:size [1024 768]})

    ;; F Move forward drawing a line.
    ;; f Move forward without drawing a line.
    ;; + Turn left by angle δ.
    ;; - Turn right by angle δ.

    (defn interpret [d δ instruction]
    (r.match/find instruction
    F
    (turtle/forward d)

    f
    (do (turtle/penup) (turtle/forward d)
    (turtle/pendown))

    +
    (turtle/left δ)

    -
    (turtle/right δ)

    [_ ..1 :as ?instructions]
    (run!
    (fn [?instruction]
    (interpret d δ ?instruction))
    ?instructions)))

    (defn l-system [s n]
    (if (= n -1)
    identity
    (fn [t]
    ((r/all (l-system s (dec n))) (s t)))))

    (def koch-island
    {:axiom '[F - F - F - F]
    :productions
    (r/rewrite
    F [F - F + F + F F - F - F + F]
    ?X ?X)})

    (def example-a
    {:axiom '[F - F - F - F]
    :productions (r/rewrite
    F [F F - F - F - F - F - F + F]
    ?X ?X)})

    (def example-b
    {:axiom '[F - F - F - F]
    :productions (r/rewrite
    F [F F - F - F - F - F F]
    ?X ?X)})

    (def example-c
    {:axiom '[F - F - F - F]
    :productions (r/rewrite
    F [F F - F + F - F - F F]
    ?X ?X)})

    (def example-d
    {:axiom '[F - F - F - F]
    :productions (r/rewrite
    F [F F - F - - F - F]
    ?X ?X)})

    (def example-f
    {:axiom '[F - F - F - F]
    :productions (r/rewrite
    F [F - F + F - F - F]
    ?X ?X)})

    (let [X example-a
    n 3
    δ 90
    system (l-system (:productions X) n)]
    (turtle/clean)
    (turtle/setxy 0 0)
    (interpret 10 δ (system (:axiom X))))