naev 0.12.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"
14
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
68void arrayShuffle( void **array )
69{
70 for ( int n = array_size( array ); n > 1; ) {
71 int k = RNG( 0, n );
72 void *tmp = array[--n];
73 array[n] = array[k];
74 array[k] = tmp;
75 }
76}
77
91int rectOverlap( double x, double y, double w, double h, double x2, double y2,
92 double w2, double h2 )
93{
94 /* Too far left, down, right, or up, respectively. */
95 if ( ( x + w < x2 ) || ( y + h < y2 ) || ( x > x2 + w2 ) || ( y > y2 + h2 ) )
96 return 0;
97
98 return 1;
99}
100
104double ease_SineInOut( double x )
105{
106 return 0.5 * ( 1. - cos( x * M_PI ) );
107}
108
112double ease_QuadraticInOut( double x )
113{
114 if ( x < 0.5 )
115 return 2. * x * x;
116 return -2. * x * x + 4 * x - 1.;
117}
118
122double ease_CubicInOut( double x )
123{
124 if ( x < 0.5 )
125 return 4. * x * x * x;
126 double y = 2. * x - 2.;
127 return 0.5 * y * y * y + 1.;
128}
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:179
Header file with generic functions and naev-specifics.
double ease_SineInOut(double x)
Simple symmetric sine-based easing.
Definition nmath.c:104
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:68
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:122
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:91
double ease_QuadraticInOut(double x)
Simple symmetric quadratic easing.
Definition nmath.c:112
static const double c[]
Definition rng.c:256