Created
December 9, 2019 21:56
-
-
Save allison-casey/b48727dd51a16e0cb0154a066372668d to your computer and use it in GitHub Desktop.
bitmasking
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
(ns playground.bitmasking.blob | |
(:require [playground.bitmasking.utils | |
:refer [index->coord coord->index reshape2d->1d]])) | |
(defn ^:private calc-offsets | |
[x y] | |
(let [north [x (dec y)] | |
north-east [(inc x) (dec y)] | |
east [(inc x) y] | |
south-east [(inc x) (inc y)] | |
south [x (inc y)] | |
south-west [(dec x) (inc y)] | |
west [(dec x) y] | |
north-west [(dec x) (dec y)]] | |
[north north-east east south-east south south-west west north-west])) | |
(defn ^:private neighbors | |
[v width x y] | |
(let [offsets (calc-offsets x y) | |
directions | |
[:north :north-east :east :south-east :south :south-west :west :north-west]] | |
(println offsets) | |
(zipmap directions | |
(map (fn [[x y]] (get v (coord->index x y width))) offsets)))) | |
(defn bitmask | |
([{:keys [north north-east east south-east south south-west west north-west]}] | |
(bitmask north north-east east south-east south south-west west north-west)) | |
([north north-east east south-east south south-west west north-west] | |
(letfn [(with-default [val] (if val 1 0))] | |
(+ (* 1 (with-default north)) | |
(* 2 (with-default north-east)) | |
(* 4 (with-default east)) | |
(* 8 (with-default south-east)) | |
(* 16 (with-default south)) | |
(* 32 (with-default south-west)) | |
(* 64 (with-default west)) | |
(* 128 (with-default north-west)))))) | |
(defn bitmask-tile | |
[v width x y blocking-ids] | |
(as-> v $ | |
(neighbors $ x y width) | |
(map (juxt key (comp boolean #(some #{%} blocking-ids) val)) $) | |
(into {} $) | |
)) | |
(bitmask-tile map1d 4 0 0 [1]) | |
;; (defn bitmask-tile | |
;; [v width x y] | |
;; (bitmask (neighbors v width x y))) | |
(defn bitmask-map | |
[v width id] | |
(into [] (for [[i cell] (map-indexed list v) | |
:let [[x y] (index->coord i width)]] | |
(if (= cell id) (bitmask-tile v x y width) cell)))) | |
(bitmask-map map1d 4 0) | |
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
(ns playground.bitmasking.two-edge | |
(:require [playground.bitmasking.utils :refer [index->coord coord->index reshape2d->1d]])) | |
(defn ^:private calc-offsets | |
[x y] | |
(let [north [x (dec y)] | |
south [x (inc y)] | |
east [(inc x) y] | |
west [(dec x) y]] | |
[north east south west])) | |
(defn ^:private neighbors | |
[v x y width] | |
(zipmap [:north :east :south :west] | |
(map (fn [[x y]] (get v (coord->index x y width))) | |
(calc-offsets x y)))) | |
(defn bitmask | |
([{:keys [north east south west]}] | |
(bitmask north east south west)) | |
([north east south west] | |
(letfn [(binary [x] (if x 1 0))] | |
(+ (* 1 (binary north)) | |
(* 2 (binary east)) | |
(* 4 (binary south)) | |
(* 8 (binary west)))))) | |
(defn bitmask-tile | |
[v width x y ids] | |
(as-> v $ | |
(neighbors $ x y width) | |
(map (juxt key (comp boolean #(some #{%} ids) val)) $) | |
(into {} $) | |
(bitmask $))) | |
(defn bitmask-map | |
[v width id wall-ids] | |
(into [] (for [[i cell] (map-indexed list v) | |
:let [[x y] (index->coord i width)]] | |
(if (= cell id) (bitmask-tile v width x y wall-ids) 0)))) | |
(for [id [2 1]] | |
(bitmask-map map1d 4 id [1])) | |
(def map2d [[2 1 1 1] | |
[1 1 2 1] | |
[2 2 2 1] | |
[1 0 1 1]]) | |
(def map1d (reshape2d->1d map2d)) | |
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
(ns playground.bitmasking.utils) | |
(defn coord->index | |
[x y width] | |
(when (<= 0 x (dec width)) | |
(+ x (* width y)))) | |
(defn index->coord | |
[i width] | |
[(rem i width) (quot i width)]) | |
(defn reshape1d->2d | |
[v width height] | |
(into [] (for [y (range height)] | |
(into [] (for [x (range width)] | |
(get v (coord->index x y width))))))) | |
(defn reshape2d->1d [v] (into [] (for [row v, cell row] cell))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment