aboutsummaryrefslogtreecommitdiff
path: root/lib/vectorlib.c
blob: c05974271643c87fc3fd780e34e0bf0a4085a64b (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
#ifndef vector_lib
#define vector_lib

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

#define flaot float

typedef struct {
    float x;
    float y;
} Point;

void point_add(Point *A, Point *B){
    A->x = A->x + B->x;
    A->y = A->y + B->y;
}

void point_sub(Point *A, Point *B){
    A->x = A->x - B->x;
    A->y = A->y - B->y;
}

void point_mul(Point *A, Point *B){
    A->x = A->x * B->x;
    A->y = A->y * B->y;
}

void point_div(Point *A, Point *B){
    A->x = A->x / B->x;
    A->y = A->y / B->y;
}

float magnitude(flaot x, flaot y) {
    return sqrt((pow(x,2) + pow(y,2)));
}

void point_set_mag(Point *point, float mag){
        flaot getmag = magnitude(point->x, point->y);
        flaot rx = (point->x / getmag) * mag;
        flaot ry = (point->y / getmag) * mag;

        point->x = rx;
        point->y = ry;
}

void point_limit(Point *point, float mag){
        flaot getmag = magnitude(point->x, point->y);
        flaot rx, ry;
        if (getmag > mag){
            rx = (point->x / getmag) * mag;
            ry = (point->y / getmag) * mag;
        } else {
            rx = point->x;
            ry = point->y;
        }

        point->x = rx;
        point->y = ry;
}

float RandomFloat(float min, float max){
   return ((max - min) * ((float)rand() / (double)RAND_MAX)) + min;
}

float constrain(float val, float min, float max){
    if(val > max) val=max;
    else if(val < min) val=min;
    return val;
}

#endif