Created
August 15, 2014 20:57
-
-
Save tom-galvin/8f904e2f0cb7d144c0b7 to your computer and use it in GitHub Desktop.
C vector2 code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Quackmatic 2014 | |
* https://github.com/Quackmatic | |
*/ | |
#include <math.h> | |
#include "vector2.h" | |
vec2 vec_add(vec2 v1, vec2 v2) | |
{ | |
vec2 value = { v1.x + v2.x, v1.y + v2.y }; | |
return value; | |
} | |
vec2 vec_sub(vec2 v1, vec2 v2) | |
{ | |
vec2 value = { v1.x - v2.x, v1.y - v2.y }; | |
return value; | |
} | |
vec2 vec_mul(vec2 v, double d) | |
{ | |
vec2 value = { v.x * d, v.y * d }; | |
return value; | |
} | |
double vec_mag(vec2 v) | |
{ | |
return sqrt(v.x * v.x + v.y * v.y); | |
} | |
vec2 vec_norm(vec2 v) | |
{ | |
return vec_mul(v, 1 / vec_mag(v)); | |
} | |
double vec_dot(vec2 v1, vec2 v2) | |
{ | |
return v1.x * v2.x + v1.y * v2.y; | |
} | |
double vec_dist(vec2 v1, vec2 v2) | |
{ | |
return vec_mag(vec_sub(v1, v2)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Quackmatic 2014 | |
* https://github.com/Quackmatic | |
*/ | |
#ifndef VECTOR2_H | |
#define VECTOR2_H | |
typedef struct { double x; double y; } vec2; | |
vec2 vec_add(vec2 v1, vec2 v2); | |
vec2 vec_sub(vec2 v1, vec2 v2); | |
vec2 vec_mul(vec2 v, double d); | |
double vec_mag(vec2 v); | |
vec2 vec_norm(vec2 v); | |
double vec_dot(vec2 v1, vec2 v2); | |
double vec_dist(vec2 v1, vec2 v2); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment