Created
March 19, 2011 19:49
-
-
Save hozumi/877755 to your computer and use it in GitHub Desktop.
rotate 45
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn rotate-90 [arr] | |
(reverse (apply map vector arr))) | |
(defn rotate [arr n] | |
(reduce (fn [x _] (rotate-90 x)) arr (range n))) | |
(defn slide [arr] | |
(let [width (dec (count (first arr)))] | |
(for [[i row] (seq-utils/indexed arr)] | |
(concat (repeat i ::empty) row (repeat (- width i) ::empty))))) | |
(defn rearrange [arr angle] | |
(let [arr (rotate arr 3) ;; initial position | |
r-num (quot angle 2) | |
arr (rotate arr r-num) | |
tilt? (= (rem angle 2) 1) | |
arr (if tilt? (slide arr) arr) | |
height (count arr) | |
width (count (first arr))] | |
(for [x (reverse (range width))] | |
(filter #(not= ::empty %) | |
(map (fn [y] (nth (nth arr y) x)) (range height)))))) | |
(rearrange a 0) | |
=> ((1 2 3 4) (11 12 13 14) (21 22 23 24) (31 32 33 34)) | |
(rearrange a 1) | |
=> ((4) (3 14) (2 13 24) (1 12 23 34) (11 22 33) (21 32) (31)) | |
(rearrange a 2) | |
=> ((4 14 24 34) (3 13 23 33) (2 12 22 32) (1 11 21 31)) | |
(rearrange a 3) | |
=> ((34) (24 33) (14 23 32) (4 13 22 31) (3 12 21) (2 11) (1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment