Angle difference between two bearings: Difference between revisions

Content added Content deleted
(→‎{{header|Haskell}}: Switched to functions over tuples.)
Line 1,248: Line 1,248:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang Haskell>import Text.Printf (printf)
<lang Haskell>import Control.Monad (join)
import Data.Bifunctor (bimap)
import Text.Printf (printf)


type Radians = Float
type Radians = Float
Line 1,254: Line 1,256:
type Degrees = Float
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 = degrees . bearingDelta . join bimap radians
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
where
(ax, ay) = (sin a, cos a)
(ax, ay) = (sin a, cos a)
Line 1,267: Line 1,272:
| otherwise = -1
| otherwise = -1


degrees :: Radians -> Degrees
degrees = (/ pi) . (180 *)


--------------------------- TEST -------------------------
radians :: Degrees -> Radians
radians = (/ 180) . (pi *)

--------------------------- TEST ---------------------------
main :: IO ()
main :: IO ()
main =
main =
putStrLn . unlines $
putStrLn . unlines $
fmap
fmap
(uncurry
( uncurry (printf "%6.2f° + %6.2f° -> %7.2f°")
(((<*>) . printf "%6.2f° + %6.2f° -> %7.2f°") <*> angleBetweenDegrees))
<*> angleBetweenDegrees
[ (20.0, 45.0)
)
, (-45.0, 45.0)
[ (20.0, 45.0),
, (-85.0, 90.0)
(-45.0, 45.0),
, (-95.0, 90.0)
(-85.0, 90.0),
, (-45.0, 125.0)
(-95.0, 90.0),
, (-45.0, 145.0)
(-45.0, 125.0),
(-45.0, 145.0)
]</lang>
]

------------------------- GENERIC ------------------------

degrees :: Radians -> Degrees
degrees = (/ pi) . (180 *)

radians :: Degrees -> Radians
radians = (/ 180) . (pi *)</lang>
{{Out}}
{{Out}}
<pre> 20.00° + 45.00° -> 25.00°
<pre> 20.00° + 45.00° -> 25.00°