Commit c527f402 authored by nicolas.albanesi's avatar nicolas.albanesi
Browse files

Added the vec2 lib

parent 6a4cb9ea
#ifndef _VEC2_H_
#define _VEC2_H_
#include <stdbool.h>
#include <stdint.h>
typedef struct _vec2
{
double x, y;
} vec2;
typedef struct _coordinates
{
uint32_t row, column;
} coordinates;
vec2 vec2_create(double x_, double y_);
vec2 vec2_create_zero();
vec2 vec2_add(vec2 lhs, vec2 rhs);
vec2 vec2_sub(vec2 lhs, vec2 rhs);
vec2 vec2_mul(double scalar, vec2 rhs);
vec2 vec2_div(double scalar, vec2 rhs);
double vec2_dist(vec2 a, vec2 b);
double vec2_dot(vec2 lhs, vec2 rhs);
double vec2_norm_sqr(vec2 v);
double vec2_norm(vec2 v);
vec2 vec2_normalize(vec2 v);
bool vec2_is_approx_equal(vec2 lhs, vec2 rhs, double eps);
coordinates vec2_to_coordinates(vec2 v, uint32_t width, uint32_t height);
vec2 vec2_map(vec2 v);
void vec2_print(vec2 v, char*);
double map(double x, double in_min, double in_max, double out_min, long out_max);
#endif
#include "../includes/vec2.h"
#include <stdio.h>
#include <math.h>
// ! TEMPORARY FIX, WILL DETELE AT SOME POINT
#define DIST_MAX 1000
/// Create a 2d vector.
/// @param x_ The first component.
/// @param y_ The second component.
/// @return The newly created vector.
vec2 vec2_create(double x_, double y_)
{
vec2 v = {x_, y_};
return v;
}
/// Get the distance between two vectors.
/// @param x_ The first Vector.
/// @param y_ The second Vector.
/// @return The distance between the two vectors.
double vec2_dist(vec2 a, vec2 b)
{
return vec2_norm(vec2_sub(a, b));
}
/// Create a zero 2d vector.
/// @return The newly created zero vector.
vec2 vec2_create_zero() { return vec2_create(0.0, 0.0); }
/// Add two vectors.
/// @param lhs The left operand.
/// @param rhs The right operand.
/// @return The sum in a new vector.
vec2 vec2_add(vec2 lhs, vec2 rhs)
{
return vec2_create(lhs.x + rhs.x, lhs.y + rhs.y);
}
/// Substract two vectors.
/// @param lhs The left operand.
/// @param rhs The right operand.
/// @return The difference in a new vector.
vec2 vec2_sub(vec2 lhs, vec2 rhs)
{
return vec2_create(lhs.x - rhs.x, lhs.y - rhs.y);
}
/// Multiply a vector by a scalar.
/// @param scalar The left operand, a scalar.
/// @param rhs The right operand, a vector.
/// @return The product in a new vector.
vec2 vec2_mul(double scalar, vec2 rhs)
{
return vec2_create(rhs.x * scalar, rhs.y * scalar);
}
/// Divide a vector by a scalar.
/// @param scalar The left operand, a scalar.
/// @param rhs The right operand, a vector.
/// @return The division in a new vector.
vec2 vec2_div(double scalar, vec2 rhs)
{
return vec2_create(rhs.x / scalar, rhs.y / scalar);
}
/// Compute the dot product (scalar product) between two vectors.
/// @param lhs The left operand.
/// @param rhs The right operand.
/// @return The dot product.
double vec2_dot(vec2 lhs, vec2 rhs)
{
return lhs.x * rhs.x + lhs.y * rhs.y;
}
/// Compute the square of the euclidean norm of a given vector.
/// @param v The vector.
/// @return The square of the norm.
double vec2_norm_sqr(vec2 v)
{
return pow(v.x, 2) + pow(v.y, 2);
}
/// Compute the euclidean norm of a given vector.
/// @param v The vector.
/// @return The norm.
double vec2_norm(vec2 v)
{
return sqrt(pow(v.x, 2) + pow(v.y, 2));
}
/// Compute the normalization of a given vector.
/// @param v The vector.
/// @return The new normalized vector.
vec2 vec2_normalize(vec2 v)
{
return vec2_create(v.x / vec2_norm(v), v.y / vec2_norm(v));
}
/// Check whether two vectors are approximately equals within a given tolerance.
/// @param lhs The left operand.
/// @param rhs The right operand.
/// @param eps The tolerance.
/// @return The dot product.
bool vec2_is_approx_equal(vec2 lhs, vec2 rhs, double eps)
{
if (vec2_norm(vec2_sub(rhs, lhs)) < eps)
{
return true;
}
return false;
}
/// Compute the coordinates of a 2d vector (with components between 0 and 1)
/// in a given screen matrix.
/// @param v The 2d vector.
/// @param width The screen width.
/// @param height The screen height.
/// @return The coordinates (row, column).
coordinates vec2_to_coordinates(vec2 v, uint32_t width, uint32_t height)
{
coordinates c;
c.column = v.x * (width-1)/2 + (width-1)/2 + 1;
c.row = v.y * (height-1)/2 + (height-1)/2 + 1;
return c;
}
/// CMap the coordinate of a vector
/// @param v The vector to be mapped.
/// @return The mapped vector.
vec2 vec2_map(vec2 v)
{
return vec2_create(map(v.x, -DIST_MAX, DIST_MAX, -1, 1), map(v.y, -DIST_MAX, DIST_MAX, -1, 1));
}
// P.x = v.x * (nx-1) / 2 + nx-1/2
/// Print a vector in the standard output.
/// @param v The vector.
void vec2_print(vec2 v, char* name)
{
printf("%s : x = %g, y = %g\n", name, v.x, v.y);
}
/**
* @brief Map a value from one range to another
*
* @param x
* @param in_min
* @param in_max
* @param out_min
* @param out_max
* @return double
*/
double map(double x, double in_min, double in_max, double out_min, long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment