You are viewing a single comment's thread. Return to all comments →
Clojure solution.
This code computes, for each (x,y) whether that point is in a "black" triangle or not.
(defn ybetween? [from to v] ; end excluded (and (>= v from) (< v to))) (defn xbetween? [from to v] ; end included (and (>= v from) (<= v to))) (defn inside? [[tx ty th dx] x y] ; is (x,y) inside triangle? (and (ybetween? ty (+ ty th) y) (if (pos? dx) (xbetween? (- tx (* dx (- y ty))) ; tri widens downward (+ tx (* dx (- y ty))) x) (xbetween? (+ tx (* dx (- (+ ty th -1) y))) ; upward (- tx (* dx (- (+ ty th -1) y))) x)))) (defn fractals [[tx ty th dx]] ; generate 4 contained triangles [[tx (+ ty (/ th 2)) (/ th 2) (- dx)] ; dx=-1 means upside down [tx ty (/ th 2) dx] ; top triangle [(- tx (/ th 2)) (+ ty (/ th 2)) (/ th 2) dx] ; bot left [(+ tx (/ th 2)) (+ ty (/ th 2)) (/ th 2) dx]]) ; bot right (defn white? [lvl tri x y] (cond (inside? (first (fractals tri)) x y) true (zero? lvl) false (some #(white? (dec lvl) % x y) (rest (fractals tri))) true true false)) (defn black? [lvl tri x y] (cond (not (inside? tri x y)) false (zero? lvl) true (white? (dec lvl) tri x y) false true true)) (defn get-level [] (Integer/parseInt (read-line)) ) (doseq [lvl [(get-level)] y (range 32)] (println (clojure.string/join (for [x (range 63)] (if (black? lvl [31 0 32 1] x y) \1 \_)))))
Seems like cookies are disabled on this browser, please enable them to open this website
Functions and Fractals: Sierpinski triangles
You are viewing a single comment's thread. Return to all comments →
Clojure solution.
This code computes, for each (x,y) whether that point is in a "black" triangle or not.