Beginner recursion question

13 views
Skip to first unread message

Samuel Cho

unread,
Dec 18, 2019, 6:27:14 PM12/18/19
to clj-processing
Hello everyone, 
I am new to Quil, coming from Processing, and am having quite a tough time getting used to functional thinking. I am currently trying out a simple algorithm by Tyler Hobbs. The algorithm is a simple triangle splitting one and goes like this:

1) draw a line between point A B C
2) create a point D between adjacent points
3) draw a line between D and opposite angle. 

Trying this out in Processing was no problem. I tried the same code in Quil and got this result instead...

0001.png

(ns trisplit.core
  (:require [quil.core :as q]
            [quil.middleware :as m]))

(def w 500)
(def h 500)
(def levels 4)
(def point-a [w 0])
(def point-b [0 h])
(def point-c [w h])

(defn triSplit[a b c levels]
  (if (> levels 0) 
  (do
(q/line (first a)(second a)
      (first b)(second b))
(q/line (first b)(second b)
      (first c)(second c))
(q/line (first c)(second c)
      (first a)(second a))

(def d 
 (apply vector ; convert to vector 
   (map (partial * 0.5) ; half the sum ^
     (map + a b)))) ;add a and b ^

(q/line (first c)(second c)
      (first d)(second d))
(triSplit a c d (- levels 1))
(triSplit b c d (- levels 1))
)))


(defn setup []
  (q/no-loop)
  (q/background 255)
  )

(defn update-state [state]
  )

(defn draw-state [state]
(triSplit point-a point-b point-c levels)
(q/save-frame "output/####.png")

  )


(q/defsketch trisplit
  :title ""
  :size [w h]
  :setup setup
  :update update-state
  :draw draw-state
  :features [:keep-on-top]
  :middleware [m/fun-mode])



I know this is a rather banal question, but I cannot seem to figure out why this is giving a strange output. I appreciate your time in reading this and thank you in advance!


Best 

Sam



Mikita Belahlazau

unread,
Dec 20, 2019, 12:14:25 AM12/20/19
to clj-processing
Hi Sam

The issue you are seeing is caused by the `(def d ...)` part in your code. In clojure and clojurescript (def) usually used to define variables global for that file. Instead you should use 

(let [d ...]
  (q/line ...)
  (triSplit a c d)
  (triSplit b c d)))


A couple of more improvements to make the code more idiomatic:

1. Use (when foo ...) instead of (if foo (do ...))
2. (q/line) can take points as it is, no need to pass coordinates individually (first a) (second a).
3. You don't have to convert sequence to vector when calculating d. Given that you I doubt you need fast index access here.
4. Looks like you are not using any state yet, so for now sketch doesn't need to use fun-mode. 


Hope this helps.

Mikita



--
You received this message because you are subscribed to the Google Groups "clj-processing" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clj-processin...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/clj-processing/fab8c657-850a-4078-8f8a-9a1f3b9b2b52%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages