Last active
January 13, 2017 02:59
-
-
Save jmorton/72f04b022335dc04ba02943958db6138 to your computer and use it in GitHub Desktop.
Point snapping function
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 offset | |
"Calculate distance between point and nearest tile pixel" | |
[point pixel-size tile-pixel-count] | |
(mod point (* pixel-size tile-pixel-count))) | |
(defn near | |
"Find nearest point" | |
[point interval shift] | |
(-> point | |
(- shift) | |
(/ interval) | |
(Math/floor) | |
(* interval) | |
(+ shift))) | |
(defn point->tile | |
"Find the nearest tile" | |
[{x :x y :y :as point} | |
{:keys [xi yi xo yo] :as tile}] | |
{:x (near x xi xo) | |
:y (near y yi yo)}) | |
(let [p1 (point->tile {:x -2040584 :y 3164804} | |
{:xi 3000 :xo 2415 :yi -3000 :yo -195}) | |
p2 (point->tile {:x -2040585 :y 3164805} | |
{:xi 3000 :xo 2415 :yi -3000 :yo -195}) | |
p3 (point->tile {:x -2040586 :y 3164806} | |
{:xi 3000 :xo 2415 :yi -3000 :yo -195})] | |
[p1 p2 p3]) | |
;; [{:x -2040585.0, :y 3164805.0} | |
;; {:x -2040585.0, :y 3164805.0} | |
;; {:x -2043585.0, :y 3167805.0}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
xi
andyi
– interval of a tile in projection coordinate units.xo
andyo
– offset of tile grid from origin in projection coordinate units.