ohsp

Prototype for a game with dual thruster controls.
git clone git://git.amin.space/ohsp.git
Log | Files | Refs | LICENSE

vector.c (1071B)


      1 #include "vector.h"
      2 
      3 #include <math.h>
      4 
      5 
      6 struct Vec2d vec2d_add(struct Vec2d v0, struct Vec2d v1)
      7 {
      8     return vec2d_add_c(v0.x, v0.y, v1.x, v1.y);
      9 }
     10 
     11 
     12 struct Vec2d vec2d_add_c(float x0, float y0, float x1, float y1)
     13 {
     14     struct Vec2d new_vec =
     15     {
     16         .x = x0 + x1,
     17         .y = y0 + y1
     18     };
     19     return new_vec;
     20 }
     21 
     22 
     23 bool vec2d_equal(struct Vec2d v0, struct Vec2d v1)
     24 {
     25     return (v0.x == v1.x) && (v0.y == v1.y);
     26 }
     27 
     28 
     29 float vec2d_get_angle(float x, float y)
     30 {
     31     return 0.5f * M_PI - atan2f(y, x);
     32 }
     33 
     34 
     35 float vec2d_get_length(float x, float y)
     36 {
     37     return hypotf(x, y);
     38 }
     39 
     40 
     41 struct Vec2d vec2d_negate(struct Vec2d v)
     42 {
     43     return vec2d_scale_c(v.x, v.y, -1.0f);
     44 }
     45 
     46 
     47 struct Vec2d vec2d_normalize(float x, float y)
     48 {
     49     float length = vec2d_get_length(x, y);
     50     return vec2d_scale_c(x, y, 1.0f / length);
     51 }
     52 
     53 
     54 struct Vec2d vec2d_scale(struct Vec2d v, float s)
     55 {
     56     return vec2d_scale_c(v.x, v.y, s);
     57 }
     58 
     59 
     60 struct Vec2d vec2d_scale_c(float x, float y, float s)
     61 {
     62     struct Vec2d new_vec =
     63     {
     64         .x = x * s,
     65         .y = y * s
     66     };
     67     return new_vec;
     68 }