aboutsummaryrefslogtreecommitdiff
path: root/Client/Source/Phy2DLite/Math.h
blob: 987b55a0e000bf9fb499312c573effa3e6632211 (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#pragma once

#include <math.h>
#include <assert.h>
#include <stdlib.h>
#include <string>

#include "Settings.h"
#include "Constants.h"

namespace Phy2D
{

    struct Vec2
    {
        Vec2() {}
        Vec2(fixed x, fixed y) : x(x), y(y) {}

        void Set(fixed x_, fixed y_) { x = x_; y = y_; }

        Vec2 operator -() { return Vec2(-x, -y); }

        void operator += (const Vec2& v)
        {
            x += v.x; y += v.y;
        }

        void operator -= (const Vec2& v)
        {
            x -= v.x; y -= v.y;
        }

        void operator *= (fixed a)
        {
            x *= a; y *= a;
        }

        fixed Length() const
        {
            return SQRT(x * x + y * y);
        }

        std::string ToString()
        {
            return std::to_string((float)x) + "," + std::to_string((float)y);
        }

        fixed x, y;
    };

    struct Mat22
    {
        Mat22() {}
        Mat22(fixed angle)
        {
            fixed c = COS(angle), s = SIN(angle);
            col1.x = c; col2.x = -s;
            col1.y = s; col2.y = c;
        }

        Mat22(const Vec2& col1, const Vec2& col2) : col1(col1), col2(col2) {}

        Mat22 Transpose() const
        {
            return Mat22(Vec2(col1.x, col2.x), Vec2(col1.y, col2.y));
        }

        Mat22 Invert() const
        {
            fixed a = col1.x, b = col2.x, c = col1.y, d = col2.y;
            Mat22 B;
            fixed det = a * d - b * c;
            assert(det != _0);
            det = (fixed)_1 / det;
            B.col1.x = det * d;	B.col2.x = -det * b;
            B.col1.y = -det * c;	B.col2.y = det * a;
            return B;
        }

        Vec2 col1, col2;
    };

    inline fixed Dot(const Vec2& a, const Vec2& b)
    {
        return a.x * b.x + a.y * b.y;
    }

    inline fixed Cross(const Vec2& a, const Vec2& b)
    {
        return a.x * b.y - a.y * b.x;
    }

    inline Vec2 Cross(const Vec2& a, fixed s)
    {
        return Vec2(s * a.y, -s * a.x);
    }

    inline Vec2 Cross(fixed s, const Vec2& a)
    {
        return Vec2(-s * a.y, s * a.x);
    }

    inline Vec2 operator * (const Mat22& A, const Vec2& v)
    {
        return Vec2(A.col1.x * v.x + A.col2.x * v.y, A.col1.y * v.x + A.col2.y * v.y);
    }

    inline Vec2 operator + (const Vec2& a, const Vec2& b)
    {
        return Vec2(a.x + b.x, a.y + b.y);
    }

    inline Vec2 operator - (const Vec2& a, const Vec2& b)
    {
        return Vec2(a.x - b.x, a.y - b.y);
    }

    inline Vec2 operator * (fixed s, const Vec2& v)
    {
        return Vec2(s * v.x, s * v.y);
    }

    inline Mat22 operator + (const Mat22& A, const Mat22& B)
    {
        return Mat22(A.col1 + B.col1, A.col2 + B.col2);
    }

    inline Mat22 operator * (const Mat22& A, const Mat22& B)
    {
        return Mat22(A * B.col1, A * B.col2);
    }

    inline fixed Abs(fixed a)
    {
        return a > _0 ? a : -a;
    }

    inline Vec2 Abs(const Vec2& a)
    {
        return Vec2(fabsf(a.x), fabsf(a.y));
    }

    inline Mat22 Abs(const Mat22& A)
    {
        return Mat22(Abs(A.col1), Abs(A.col2));
    }

    inline fixed Sign(fixed x)
    {
        return  x < _0 ? -_1 : _1;
    }

    inline fixed Min(fixed a, fixed b)
    {
        return a < b ? a : b;
    }

    inline fixed Max(fixed a, fixed b)
    {
        return a > b ? a : b;
    }

    inline fixed Clamp(fixed a, fixed low, fixed high)
    {
        return Max(low, Min(a, high));
    }

    template<typename T> inline void Swap(T& a, T& b)
    {
        T tmp = a;
        a = b;
        b = tmp;
    }

    //// Random fixed in range [-1,1]
    //inline fixed Random()
    //{
    //    fixed r = (fixed)rand();
    //    r /= RAND_MAX;
    //    r = (fixed)2.0f * r - _1;
    //    return r;
    //}

    //inline fixed Random(fixed lo, fixed hi)
    //{
    //    fixed r = (fixed)rand();
    //    r /= RAND_MAX;
    //    r = (hi - lo) * r + lo;
    //    return r;
    //}

}