Files
vectors/lib/global_objects.c

67 lines
1.3 KiB
C

#ifndef global_objects
#define global_objects
#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;
}
#endif