commit 4c2bed5ef03fb937bdd4f156c6f54f768f01f700
parent fef52f812a3ba2d845c473e8ab5e617537c89fd3
Author: Daniel Barlow <dan@telent.net>
Date: Fri, 6 Jun 2025 21:14:19 +0100
rewrite map-bounds to fetch fewer off-screen tiles
Diffstat:
1 file changed, 40 insertions(+), 12 deletions(-)
diff --git a/pkgs/maps/main.fnl b/pkgs/maps/main.fnl
@@ -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))
(fn road-width-for [line offset]
@@ -167,8 +195,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)))
seen-road-names {}
g (cairo.Context.create map-surface)]
@@ -214,7 +242,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))]