Subastra
Loading...
Searching...
No Matches
vec2.h
Go to the documentation of this file.
1#ifndef __H__VEC2__
2#define __H__VEC2__
3
4#include "defs.h"
5
6#include <math.h>
7
9#define VEC2_EPSILON 1e-7
10
11typedef struct {
12 float x, y;
13} vec2;
14
18typedef vec2 norm2;
19
21static vec2 vec2_new(float x, float y) {
22 vec2 ret;
23 ret.x = x;
24 ret.y = y;
25 return ret;
26}
27
29static vec2 vec2_zero() { return vec2_new(0., 0.); }
30
33static vec2 vec2_add(vec2 a, vec2 b) {
34 a.x += b.x;
35 a.y += b.y;
36 return a;
37}
38
41static vec2 vec2_sub(vec2 a, vec2 b) {
42 a.x -= b.x;
43 a.y -= b.y;
44 return a;
45}
46
49static vec2 vec2_scale(vec2 a, float c) {
50 a.x *= c;
51 a.y *= c;
52 return a;
53}
54
58 vec2 ret;
59 ret.x = -v.y;
60 ret.y = v.x;
61 return ret;
62}
63
68 norm2 ret;
69 ret.x = 0.;
70 ret.y = 0.;
71
72 float l = sqrtf(v.x * v.x + v.y * v.y);
73 if (l < VEC2_EPSILON)
74 return ret;
75
76 ret.x = v.x / l;
77 ret.y = v.y / l;
78 return ret;
79}
80
83static float vec2_dot(vec2 a, vec2 b) { return a.x * b.x + a.y * b.y; }
84
85#endif
Definition vec2.h:11
static vec2 vec2_add(vec2 a, vec2 b)
{.x = a.x + b.x, .y = a.y + b.y}
Definition vec2.h:33
static norm2 vec2_normalize(vec2 v)
Returns a vector of length 1. Returns the 0 vector for vectors of length VEC2_EPSILON and smaller.
Definition vec2.h:67
static vec2 vec2_sub(vec2 a, vec2 b)
{.x = a.x - b.x, .y = a.y - b.y}
Definition vec2.h:41
static vec2 vec2_scale(vec2 a, float c)
{.x = c * a.x, .y = c * a.y}
Definition vec2.h:49
static float vec2_dot(vec2 a, vec2 b)
Returns a scalar product of the two vectors.
Definition vec2.h:83
float y
Definition vec2.h:12
static vec2 vec2_perpendicular(vec2 v)
{.x = -y, .y = x}
Definition vec2.h:57
float x
Definition vec2.h:12
#define VEC2_EPSILON
The vectors of this length and smaller are considered 0.
Definition vec2.h:9
vec2 norm2
Normalized vector. Does not do any work to uphold a variants, just is more type-safe....
Definition vec2.h:18
static vec2 vec2_zero()
{.x = 0, .y = 0}
Definition vec2.h:29
static vec2 vec2_new(float x, float y)
{.x = x, .y = y}
Definition vec2.h:21