feature/vectors #6
@@ -1,12 +1,23 @@
|
|||||||
#ifndef CURTLE_VECTOR_H
|
#ifndef CURTLE_VECTOR_H
|
||||||
#define CURTLE_VECTOR_H
|
#define CURTLE_VECTOR_H
|
||||||
|
|
||||||
struct vec2
|
struct vec2 {
|
||||||
{
|
float x;
|
||||||
double x;
|
float y;
|
||||||
double y;
|
};
|
||||||
|
|
||||||
|
struct vec3 {
|
||||||
|
float x;
|
||||||
|
float y;
|
||||||
|
float z;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct vec2 vec2_add(struct vec2 a, struct vec2 b);
|
struct vec2 vec2_add(struct vec2 a, struct vec2 b);
|
||||||
|
|
||||||
|
struct vec2 vec2_sub(struct vec2 a, struct vec2 b);
|
||||||
|
|
||||||
|
float vec2_dot(struct vec2 a, struct vec2 b);
|
||||||
|
|
||||||
|
float vec2_det(struct vec2 a, struct vec2 b);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
69
src/vector.c
69
src/vector.c
@@ -1,6 +1,73 @@
|
|||||||
#include "curtle/vector.h"
|
#include "curtle/vector.h"
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
struct vec2 vec2_add(struct vec2 a, struct vec2 b)
|
struct vec2 vec2_add(const struct vec2 a, const struct vec2 b)
|
||||||
{
|
{
|
||||||
return (struct vec2){ a.x + b.x, a.y + b.y };
|
return (struct vec2){ a.x + b.x, a.y + b.y };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct vec2 vec2_sub(const struct vec2 a, const struct vec2 b)
|
||||||
|
{
|
||||||
|
return (struct vec2){ a.x - b.x, a.y - b.y };
|
||||||
|
}
|
||||||
|
|
||||||
|
float vec2_dot(const struct vec2 a, const struct vec2 b)
|
||||||
|
{
|
||||||
|
return a.x * b.x + a.y * b.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
float vec2_det(const struct vec2 a, const struct vec2 b)
|
||||||
|
{
|
||||||
|
return a.x * b.y - a.y * b.x;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct vec2 vec2_scale(const struct vec2 v, const float s)
|
||||||
|
{
|
||||||
|
return (struct vec2){ v.x * s, v.y * s };
|
||||||
|
}
|
||||||
|
|
||||||
|
float vec2_len(const struct vec2 v)
|
||||||
|
{
|
||||||
|
return sqrtf(v.x * v.x + v.y * v.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct vec2 vec2_normalize(const struct vec2 v)
|
||||||
|
{
|
||||||
|
return vec2_scale(v, 1.0f / vec2_len(v));
|
||||||
|
}
|
||||||
|
|
||||||
|
struct vec3 vec3_add(const struct vec3 a, const struct vec3 b)
|
||||||
|
{
|
||||||
|
return (struct vec3){ a.x + b.x, a.y + b.y, a.z + b.z };
|
||||||
|
}
|
||||||
|
|
||||||
|
struct vec3 vec3_sub(const struct vec3 a, const struct vec3 b)
|
||||||
|
{
|
||||||
|
return (struct vec3){ a.x - b.x, a.y - b.y, a.z - b.z };
|
||||||
|
}
|
||||||
|
|
||||||
|
float vec3_dot(const struct vec3 a, const struct vec3 b)
|
||||||
|
{
|
||||||
|
return a.x * b.x + a.y * b.y + a.z * b.z;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct vec3 vec3_cross(const struct vec3 a, const struct vec3 b)
|
||||||
|
{
|
||||||
|
return (struct vec3){ a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z,
|
||||||
|
a.x * b.y - a.y * b.x };
|
||||||
|
}
|
||||||
|
|
||||||
|
struct vec3 vec3_scale(const struct vec3 v, const float s)
|
||||||
|
{
|
||||||
|
return (struct vec3){ v.x * s, v.y * s, v.z * s };
|
||||||
|
}
|
||||||
|
|
||||||
|
float vec3_len(const struct vec3 v)
|
||||||
|
{
|
||||||
|
return sqrtf(v.x * v.x + v.y * v.y + v.z * v.z);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct vec3 vec3_normalize(const struct vec3 v)
|
||||||
|
{
|
||||||
|
return vec3_scale(v, 1.0f / vec3_len(v));
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user