You are viewing a single comment's thread. Return to all comments →
Here's my solution using clojure.
(defn findChilds "take a triangle tip, return 3 Sierpinski triangles" [{:keys [x y h]}] (let [h (/ h 2)] [{:x x :y y :h h} {:x (- x h) :y (+ y h) :h h} {:x (+ x h) :y (+ y h) :h h}])) (defn trianglesTipsPos "return an array of triangles tips position and height" [iteration initialTip] (reduce (fn [acc _] (mapcat findChilds acc)) [initialTip] (range iteration))) (defn tipToTriangle "given a {:x :y :h} return a list of { :x :y } representing a triangle body" [{:keys [x y h]}] (mapcat (fn [h] (map (fn [x] {:x x :y (+ y h)}) (range (- x h) (+ 1 x h)))) (range h))) (defn genRow "generate a row of ASCII characters" [row cols fullTriangles] (map (fn [col] (if (some #(= % {:x col :y row}) fullTriangles) "1" "_")) (range cols))) (defn render "render the final 2D array of ASCII characters" [rows cols triangles] (map (fn [row] (genRow row cols triangles)) (range rows))) (defn flow "Sierpinski triangles generator" [rows cols iter] (let [initialTip {:x (quot cols 2) :y 0 :h rows}] (->> initialTip (trianglesTipsPos iter) (mapcat tipToTriangle) (render rows cols)))) (doseq [row (flow 32 63 (Integer/parseInt (read-line)))] (println (clojure.string/join row)))
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 →
Here's my solution using clojure.