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 TextControl.PrintfMonad (printfjoin)
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)
 
angleBetweenDegrees :: (Degrees ->, Degrees) -> Degrees
bearingDelta :: Radians -> Radians -> Radians
angleBetweenDegrees a b = degrees $. bearingDelta (radians. a)join bimap (radians b)
bearingDelta a b -- sign * dot-product
 
= sign * acos ((ax * bx) + (ay * by))
bearingDelta :: (Radians ->, Radians) -> Radians
bearingDelta (a, b) -- sign * dot-product
=
= sign * acos ((ax * bx) + (ay * by))
where
(ax, ay) = (sin a, cos a)
Line 1,267 ⟶ 1,272:
| otherwise = -1
 
degrees :: Radians -> Degrees
degrees = (/ pi) . (180 *)
 
--------------------------- TEST ---------------------------
radians :: Degrees -> Radians
radians = (/ 180) . (pi *)
 
--------------------------- TEST ---------------------------
main :: IO ()
main =
putStrLn . unlines $
fmap
( uncurry (printf "%6.2f° + %6.2f° -> %7.2f°")
(((<*>) . printf "%6.2f° + %6.2f° -> %7.2f°") <*> angleBetweenDegrees))
[ (20.0, 45.0)
, [ (-4520.0, 45.0),
, (-8545.0, 9045.0),
, (-9585.0, 90.0),
, (-4595.0, 12590.0),
, (-45.0, 145125.0),
(-45.0, 145.0)
]</lang>
]
 
------------------------- GENERIC ------------------------
 
degrees :: Radians -> Degrees
degrees = (/ pi) . (180 *)
 
radians :: Degrees -> Radians
radians = (/ 180) . (pi *)</lang>
{{Out}}
<pre> 20.00° + 45.00° -> 25.00°
9,655

edits