summaryrefslogtreecommitdiff
path: root/src/vector.c
blob: baef5fc0775721b188045518313d6aed50730023 (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
#include "vector.h"

#include <math.h>


struct Vec2d vec2d_add(struct Vec2d v0, struct Vec2d v1)
{
    return vec2d_add_c(v0.x, v0.y, v1.x, v1.y);
}


struct Vec2d vec2d_add_c(float x0, float y0, float x1, float y1)
{
    struct Vec2d new_vec =
    {
        .x = x0 + x1,
        .y = y0 + y1
    };
    return new_vec;
}


bool vec2d_equal(struct Vec2d v0, struct Vec2d v1)
{
    return (v0.x == v1.x) && (v0.y == v1.y);
}


float vec2d_get_angle(float x, float y)
{
    return 0.5f * M_PI - atan2f(y, x);
}


float vec2d_get_length(float x, float y)
{
    return hypotf(x, y);
}


struct Vec2d vec2d_negate(struct Vec2d v)
{
    return vec2d_scale_c(v.x, v.y, -1.0f);
}


struct Vec2d vec2d_normalize(float x, float y)
{
    float length = vec2d_get_length(x, y);
    return vec2d_scale_c(x, y, 1.0f / length);
}


struct Vec2d vec2d_scale(struct Vec2d v, float s)
{
    return vec2d_scale_c(v.x, v.y, s);
}


struct Vec2d vec2d_scale_c(float x, float y, float s)
{
    struct Vec2d new_vec =
    {
        .x = x * s,
        .y = y * s
    };
    return new_vec;
}