Skip to content

Instantly share code, notes, and snippets.

@jmorton
Last active January 13, 2017 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmorton/72f04b022335dc04ba02943958db6138 to your computer and use it in GitHub Desktop.
Save jmorton/72f04b022335dc04ba02943958db6138 to your computer and use it in GitHub Desktop.
Point snapping function
(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}]
@jmorton
Copy link
Author

jmorton commented Jan 13, 2017

xi and yi – interval of a tile in projection coordinate units.
xo and yo – offset of tile grid from origin in projection coordinate units.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment