Compare commits

...

2 Commits

Author SHA1 Message Date
1608d26260 draw road labels always left->right 2025-07-02 22:23:53 +01:00
ca02f5c4fa clamp angle to -pi..pi 2025-07-01 22:16:07 +01:00
2 changed files with 28 additions and 5 deletions

View File

@ -211,6 +211,24 @@ label.readout {
(when (not (. tbl k))
(tiles.fetch cq x y zoom #(tset tbl k $1)))))))
;; iso 9899:1999 says
;; "The atan2 functions return arctan y/x in the interval [−π , +π ] radians"
(fn clamp-angle [rad]
(let [circle (* 2 math.pi)]
(if
(< rad (- math.pi)) (clamp-angle (+ rad circle))
(> rad math.pi) (clamp-angle (- rad circle))
rad)))
(define-tests :clamp-angle
(expect= (clamp-angle 2) 2)
(expect= (clamp-angle (+ (* 4 math.pi) 2)) 2)
(expect= (clamp-angle (- 2 (* 4 math.pi))) 2))
(fn deg->rad [deg]
(* math.pi (/ deg 180)))
(fn draw-onto-map-surface [surface bounds zoom]
(let [{ : num-tiles-x : num-tiles-y } bounds
road-width 14
@ -228,7 +246,7 @@ label.readout {
(g:fill)
(g:translate (+ (// bounds.pixels.x 2)) (+ (// bounds.pixels.y 2)))
(g:rotate (* (/ (- app-state.orientation-target) 180) math.pi))
(g:rotate (deg->rad (- app-state.orientation-target)))
(g:translate (- (// bounds.pixels.x 2)) (- (// bounds.pixels.y 2)))
(cairo-roads g lines bounds)
@ -248,7 +266,13 @@ label.readout {
(g:save)
(g:move_to x y)
(g:rotate angle)
(let [theta (clamp-angle
(- angle (deg->rad app-state.orientation-target)))]
(if (and (> theta 0) (< theta math.pi))
(g:rotate (- angle (/ math.pi 2)))
(g:rotate (- angle (* math.pi 1.5)))))
(g:rel_move_to (- (// w 2)) 3)
(g:text_path n)
(g:fill)

View File

@ -77,7 +77,7 @@
(set biggest-n (- i 1)))))
(let [[x y] (. points biggest-n)
[nx ny] (. points (+ 1 biggest-n))
angle (math.atan (- ny y) (- nx x))]
angle (math.atan (- nx x) (- y ny))]
[(/ (+ nx x) 2) (/ (+ ny y) 2) angle]))
@ -92,8 +92,7 @@
(icollect [_ nd (ipairs e.nodes)]
(let [node (. nodes nd)
(tx ty) (latlon->tile node.lat node.lon zoom)]
;;(print e.tags.name e.id e.name node.lat node.lon)
[ tx ty ]))]
[ tx ty ]))]
(tset
lines
e.id