mars is a sphere: account for wrapping x values and scaling when y>0

main
Daniel Barlow 2023-01-22 21:37:53 +00:00
parent bcd833e26b
commit 33243ca123
1 changed files with 41 additions and 8 deletions

View File

@ -5,20 +5,29 @@
(macro expect [text actual expected]
`(let [actual# ,actual]
(or (match actual#
,expected true
_# false)
,expected true)
(assert false (view {
:text ,text
:expected ,expected
:expected ,(view expected)
:actual actual#
})))))
(fn wrap-longitude [x]
(if (<= x -180) (wrap-longitude (+ x 360))
(< 180 x) (wrap-longitude (- x 360))
x))
(fn drive [{: x : y : direction} distance]
(match direction
:n {:y (+ y distance)}
:s {:y (- y distance)}
:w {:x (- x distance)}
:e {:x (+ x distance)}))
(let [distance
(if (. {:w true :e true} direction)
(/ distance (math.cos (/ (* math.pi y) 180)))
distance)]
(match direction
:n {:y (+ y distance)}
:s {:y (- y distance)}
:w {:x (wrap-longitude (- x distance))}
:e {:x (wrap-longitude (+ x distance))})))
(fn turn-left [direction]
{:direction
@ -117,6 +126,30 @@
(execute r [:l :l :f :f :f :r :f :b])
{:x 1 :y 4 :direction :e}))
;; Circumference of mars is 3,376.2km (about half that of earth).
;; We choose the unit of distance to be equal to one degree at the equator
;; (about 60km), so if we travel the same linear distance at a higher
;; latitude, we will move be a larger number of degrees
(expect "At 60 degrees latitude, one unit of drive is two degrees"
(execute (rover 0 60 :e) [:f])
(where {:x x :y 60 :direction :e}
(< (math.abs (- x 2)) 0.000001)
))
(expect "At 45 degrees latitude, one unit of drive is (sqrt 2) degrees"
(execute (rover 0 45 :e) [:f])
(where {:x x } (< (math.abs (- x 1.4141)) 0.001)))
;; valid longitudes are -180 .. 180
(expect "Longitude wraps from positive to negative when travelling west"
(execute (rover -179 0 :w) [:f :f :f])
{:x 178 :y 0 :direction :w})
(expect "Longitude wraps from negative to positive when travelling east"
(execute (rover 179 0 :e) [:f :f :f])
{:x -178 :y 0 :direction :e})
"if we travel west past x=-180, x becomes positive 180"
"if we travel east past x=180, x becomes -180"