naev 0.11.5
nmath.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
10#include <math.h>
11
12#include "naev.h"
15#include "nmath.h"
16
17#include "array.h"
18#include "log.h"
19#include "rng.h"
20
30int nmath_solve2Eq( double results[2], double a, double b, double c )
31{
32 /* Calculate the root. */
33 double root = b * b - 4 * a * c;
34 if (root < 0.)
35 return -1;
36 root = sqrt(root);
37 /* Set the results. */
38 results[0] = (-b + root) / (2 * a);
39 results[1] = (-b - root) / (2 * a);
40 return 0;
41}
42
46double max3( double v1, double v2, double v3 )
47{
48 double max = (v1 > v2) ? v1 : v2;
49 max = (max > v3) ? max : v3;
50 return(max);
51}
52
56double min3( double v1, double v2, double v3 )
57{
58 double min;
59 min = (v1 < v2) ? v1 : v2;
60 min = (min < v3) ? min : v3;
61 return(min);
62}
63
67void arrayShuffle( void** array )
68{
69 for (int n=array_size( array ); n > 1; ) {
70 int k = RNG(0, n);
71 void *tmp = array[--n];
72 array[n] = array[k];
73 array[k] = tmp;
74 }
75}
76
90int rectOverlap( double x, double y, double w, double h,
91 double x2, double y2, double w2, double h2 )
92{
93 /* Too far left, down, right, or up, respectively. */
94 if ((x+w < x2) || (y+h < y2) || (x > x2+w2) || (y > y2+h2))
95 return 0;
96
97 return 1;
98}
99
103double ease_SineInOut( double x )
104{
105 return 0.5 * (1. - cos(x * M_PI));
106}
107
111double ease_QuadraticInOut( double x )
112{
113 if (x < 0.5)
114 return 2.*x*x;
115 return -2.*x*x + 4*x - 1.;
116}
117
121double ease_CubicInOut( double x )
122{
123 if (x < 0.5)
124 return 4.*x*x*x;
125 double y = 2.*x - 2.;
126 return 0.5*y*y*y+1.;
127}
Provides macros to work with dynamic arrays.
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
Definition array.h:168
Header file with generic functions and naev-specifics.
double ease_SineInOut(double x)
Simple symmetric sine-based easing.
Definition nmath.c:103
double min3(double v1, double v2, double v3)
Returns the minimum of 3 values.
Definition nmath.c:56
void arrayShuffle(void **array)
Randomly sorts an array (array.h) of pointers in place with the Fisher-Yates shuffle.
Definition nmath.c:67
double max3(double v1, double v2, double v3)
Returns the maximum of 3 values.
Definition nmath.c:46
int nmath_solve2Eq(double results[2], double a, double b, double c)
Solves the equation: a * x^2 + b * x + c = 0.
Definition nmath.c:30
double ease_CubicInOut(double x)
Simple symmetric cubic easing.
Definition nmath.c:121
int rectOverlap(double x, double y, double w, double h, double x2, double y2, double w2, double h2)
Checks whether two rectangles overlap at any point.
Definition nmath.c:90
double ease_QuadraticInOut(double x)
Simple symmetric quadratic easing.
Definition nmath.c:111
static const double c[]
Definition rng.c:264