rewrite map-bouds to get less off-screen tiles

This commit is contained in:
Daniel Barlow 2025-06-02 13:35:10 +01:00
parent 39a2e49422
commit 380c5e7410

View File

@ -75,20 +75,48 @@ label.readout {
(fn map-bounds [lat lon zoom]
(let [num-tiles-x (+ 1 (math.ceil (/ map-width tile-size)))
num-tiles-y (+ 1 (math.ceil (/ map-height tile-size)))
(tile-x tile-y) (tiles.latlon->tile app-state.lat app-state.lon app-state.zoom)
min-tile-x (math.floor (- tile-x (/ num-tiles-x 2)))
max-tile-x (+ min-tile-x num-tiles-x 4)
min-tile-y (math.floor (- tile-y (/ num-tiles-y 2)))
max-tile-y (+ min-tile-y num-tiles-y 4)]
(fn map-bounds-tile [tile-x tile-y]
(let [min-tile-x (math.floor (- tile-x (/ map-width tile-size 2)))
max-tile-x (math.floor (+ tile-x (/ map-width tile-size 2)))
min-tile-y (math.floor (- tile-y (/ map-height tile-size 2)))
max-tile-y (math.floor (+ tile-y (/ map-height tile-size 2)))]
{
:min { :x min-tile-x :y min-tile-y }
:max { :x max-tile-x :y max-tile-y }
: num-tiles-x : num-tiles-y
:num-tiles-x (+ 1 (- max-tile-x min-tile-x))
:num-tiles-y (+ 1 (- max-tile-y min-tile-y))
}))
;; 720 width is 2.8 * 256 pixel tiles
;; 800 height is 3.125 tiles
(let [bounds (map-bounds-tile 65539.5 45014.5)]
;; tile 65539, 45014 is centred on screen. left of it there is space
;; for one tile and right of it likewise.
;; vertical space for other tiles is (/ (- map-height tile-size) 256)
;; => 2.125 tiles, shared equally to top and bottom therefore
;; 1.0625 tiles above and 1.0625 tiles below
(expect= bounds.min {:x 65538 :y 45012})
(expect= bounds.max {:x 65540 :y 45016}))
(let [bounds (map-bounds-tile 65539.0 45014.0)]
;; top left corner of tile 65539, 45014 is centred on screen.
;; to its left there are 360 pixels, so we need two tiles
;; to its right there are 104 pixels, so one tile
;; above there are 400 pixels: two tiles
;; below are 144 pixels: one tile
(expect= bounds.min {:x 65537 :y 45012})
(expect= bounds.max {:x 65540 :y 45015})
)
(fn map-bounds [lat lon zoom]
(let [(tile-x tile-y) (tiles.latlon->tile app-state.lat app-state.lon app-state.zoom)]
(map-bounds-tile tile-x tile-y)))
(local cq (cqueues.new))
(var map-surface nil)
@ -107,8 +135,8 @@ label.readout {
(let [map-surface
(window:create_similar_surface
cairo.Content.COLOR
(* tile-size (+ 4 num-tiles-x))
(* tile-size (+ 4 num-tiles-y)))
(* tile-size (+ 1 num-tiles-x))
(* tile-size (+ 1 num-tiles-y)))
g (cairo.Context.create map-surface)]
(g:set_source_rgb 1 1 1)
@ -137,7 +165,7 @@ label.readout {
(set map-surface (cairo-the-map window))))
(let [(tile-x tile-y) (tiles.latlon->tile app-state.lat app-state.lon app-state.zoom)
bounds (map-bounds tile-x tile-y)
bounds (map-bounds-tile tile-x tile-y)
offset-x (- (* tile-size (- tile-x bounds.min.x)) (/ map-width 2))
offset-y (- (* tile-size (- tile-y bounds.min.y)) (/ map-height 2))]