~aleteoryx/muditaos

ref: e9de840a51149e8f352f9141ba1c7995cf8ed943 muditaos/module-utils/math/Math.hpp -rw-r--r-- 1.3 KiB
e9de840a — Marcin Smoczyński [EGD-5290] Fix on audio on voice call 5 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <cstdint>
#include <cmath>

namespace trigonometry
{
    using SideLength = std::int32_t;
    using Degrees    = std::int32_t;
    using Radians    = double;

    constexpr Degrees FullAngle  = 360;
    constexpr Degrees HalfAngle  = 180;
    constexpr Degrees RightAngle = 90;

    constexpr static inline auto toRadians(Degrees degrees) noexcept -> Radians
    {
        return degrees * M_PI / HalfAngle;
    }

    struct AdjacentSide
    {
        static auto fromAngle(Radians angle, SideLength hypotenuse) -> SideLength
        {
            return std::lround(std::cos(angle) * hypotenuse);
        }

        static auto fromCosine(double cosine, SideLength hypotenuse) -> SideLength
        {
            return std::lround(cosine * hypotenuse);
        }
    };

    struct OppositeSide
    {
        static auto fromAngle(Radians angle, SideLength hypotenuse) -> SideLength
        {
            return std::lround(std::sin(angle) * hypotenuse);
        }

        static auto fromSine(double sine, SideLength hypotenuse) -> SideLength
        {
            return std::lround(sine * hypotenuse);
        }
    };
} // namespace trigonometry