Angle difference between two bearings: Difference between revisions
→{{header|Haskell}}: Switched to functions over tuples.
(→{{header|Haskell}}: Switched to functions over tuples.) |
|||
Line 1,248:
=={{header|Haskell}}==
<lang Haskell>import
import Data.Bifunctor (bimap)
import Text.Printf (printf)
type Radians = Float
Line 1,254 ⟶ 1,256:
type Degrees = Float
---------- ANGLE DIFFERENCE BETWEEN TWO BEARINGS ---------
angleBetweenDegrees :: Degrees -> Degrees -> Degrees▼
angleBetweenDegrees a b = degrees $ bearingDelta (radians a) (radians b)▼
bearingDelta :: Radians -> Radians -> Radians▼
bearingDelta a b -- sign * dot-product▼
= sign * acos ((ax * bx) + (ay * by))▼
▲bearingDelta (a, b) -- sign * dot-product
=
where
(ax, ay) = (sin a, cos a)
Line 1,267 ⟶ 1,272:
| otherwise = -1
degrees :: Radians -> Degrees▼
degrees = (/ pi) . (180 *)▼
radians :: Degrees -> Radians▼
radians = (/ 180) . (pi *)▼
▲--------------------------- TEST ---------------------------
main :: IO ()
main =
putStrLn . unlines $
fmap
( uncurry (printf "%6.2f° + %6.2f° -> %7.2f°")
(-45.0, 145.0)
]
------------------------- GENERIC ------------------------
▲degrees :: Radians -> Degrees
▲degrees = (/ pi) . (180 *)
▲radians :: Degrees -> Radians
▲radians = (/ 180) . (pi *)</lang>
{{Out}}
<pre> 20.00° + 45.00° -> 25.00°
|