Angle difference between two bearings: Difference between revisions
Content added Content deleted
(→JS ES6: Tidied) |
|||
Line 1,640: | Line 1,640: | ||
<lang JavaScript>(() => { |
<lang JavaScript>(() => { |
||
"use strict"; |
|||
// ------ ANGLE DIFFERENCE BETWEEN TWO BEARINGS ------ |
|||
// bearingDelta :: Radians -> Radians -> Radians |
// bearingDelta :: Radians -> Radians -> Radians |
||
const bearingDelta = |
const bearingDelta = a => |
||
// The difference between two bearings: a and b. |
|||
⚫ | |||
⚫ | |||
const [ax, ay] = [sin(a), cos(a)]; |
|||
⚫ | |||
// Cross-product |
// Cross-product above zero ? |
||
sign = ((ay * bx) - (by * ax)) > 0 ? |
const sign = ((ay * bx) - (by * ax)) > 0 ? ( |
||
⚫ | |||
⚫ | |||
// Sign * dot-product |
// Sign * dot-product. |
||
return sign * acos((ax * bx) + (ay * by)); |
return sign * acos((ax * bx) + (ay * by)); |
||
}; |
}; |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
// main :: IO () |
|||
⚫ | |||
⚫ | |||
// justifyRight :: Int -> Char -> Text -> Text |
|||
const justifyRight = (n, cFiller, strText) => |
|||
n > strText.length ? ( |
|||
(cFiller.repeat(n) + strText) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
justifyRight(6, ' ', `${da}° +`) + |
|||
justifyRight(11, ' ', ` ${db}° -> `) + |
|||
justifyRight(7, ' ', `${(radDeg(bearingDelta(degRad(da), degRad(db)))) |
|||
⚫ | |||
⚫ | |||
[20, 45], |
[20, 45], |
||
[-45, 45], |
[-45, 45], |
||
Line 1,683: | Line 1,671: | ||
[-45, 145] |
[-45, 145] |
||
].map(xy => showMap(...xy)) |
].map(xy => showMap(...xy)) |
||
.join( |
.join("\n"); |
||
// ------------------- FORMATTING -------------------- |
|||
⚫ | |||
const showMap = (da, db) => { |
|||
const |
|||
delta = degreesFromRadians( |
|||
bearingDelta( |
|||
radiansFromDegrees(da) |
|||
)( |
|||
radiansFromDegrees(db) |
|||
) |
|||
) |
|||
⚫ | |||
theta = `${da}° +`.padStart(6, " "), |
|||
theta1 = ` ${db}° -> `.padStart(11, " "), |
|||
diff = `${delta}°`.padStart(7, " "); |
|||
return `${theta}${theta1}${diff}`; |
|||
}; |
|||
// --------------------- GENERIC --------------------- |
|||
// radiansFromDegrees :: Float -> Float |
|||
const radiansFromDegrees = n => |
|||
⚫ | |||
// degreesFromRadians :: Float -> Float |
|||
const degreesFromRadians = x => |
|||
180.0 * x / Pi; |
|||
// Abbreviations for trigonometric methods and |
|||
// properties of the standard Math library. |
|||
const [ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
// MAIN --- |
|||
⚫ | |||
})();</lang> |
})();</lang> |
||
{{Out}} |
{{Out}} |