feature/vectors #6
@@ -1,6 +1,6 @@
|
||||
name: CI
|
||||
|
||||
on: [push, pull_request]
|
||||
on: [ push, pull_request ]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
@@ -1,12 +1,23 @@
|
||||
#ifndef CURTLE_VECTOR_H
|
||||
#define CURTLE_VECTOR_H
|
||||
|
||||
struct vec2
|
||||
{
|
||||
double x;
|
||||
double y;
|
||||
struct vec2 {
|
||||
float x;
|
||||
float y;
|
||||
};
|
||||
|
||||
struct vec3 {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
};
|
||||
|
||||
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
|
||||
71
src/vector.c
71
src/vector.c
@@ -1,6 +1,73 @@
|
||||
#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));
|
||||
}
|
||||
@@ -3,11 +3,11 @@
|
||||
|
||||
int main()
|
||||
{
|
||||
struct vec2 a = {1, 2};
|
||||
struct vec2 b = {3, 4};
|
||||
struct vec2 a = { 1, 2 };
|
||||
struct vec2 b = { 3, 4 };
|
||||
|
||||
struct vec2 r = vec2_add(a, b);
|
||||
struct vec2 r = vec2_add(a, b);
|
||||
|
||||
assert(r.x == 4);
|
||||
assert(r.y == 6);
|
||||
assert(r.x == 4);
|
||||
assert(r.y == 6);
|
||||
}
|
||||
Reference in New Issue
Block a user