forked from kmilo17pet/qlibs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qfmathex.c
111 lines (103 loc) · 3.46 KB
/
qfmathex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*!
* @file qfmathex.c
* @author J. Camilo Gomez C.
* @note This file is part of the qLibs distribution.
**/
#include "qfmathex.h"
#include "qffmath.h"
#include <float.h>
#include <limits.h>
#include <string.h>
/*============================================================================*/
float qFMathEx_Normalize( const float x,
const float xMin,
const float xMax )
{
return ( x - xMin )/( xMax - xMin );
}
/*============================================================================*/
float qFMathEx_MapMinMax( const float x,
const float xMin,
const float xMax,
const float yMin,
const float yMax )
{
return ( ( yMax - yMin )*qFMathEx_Normalize( x, xMin, xMax ) ) + yMin;
}
/*============================================================================*/
bool qFMathEx_InRangeCoerce( float * const x,
const float lowerL,
const float upperL )
{
bool retVal = false;
if ( 1 == (int)QLIB_ISNAN( x[ 0 ] ) ) {
x[ 0 ] = lowerL;
}
else {
if ( x[ 0 ] < lowerL ) {
x[ 0 ] = lowerL;
}
else if ( x[ 0 ] > upperL ) {
x[ 0 ] = upperL;
}
else {
retVal = true;
}
}
return retVal;
}
/*============================================================================*/
bool qFMathEx_AlmostEqual( const float a,
const float b,
const float tol )
{
return ( QLIB_ABS( a - b ) <= QLIB_ABS( tol ) );
}
/*============================================================================*/
bool qFMathEx_Equal( const float a,
const float b )
{
return qFMathEx_AlmostEqual( a, b, FLT_MIN );
}
/*============================================================================*/
bool qFMathEx_InPolygon( const float x,
const float y,
const float * const px,
const float * const py,
const size_t p )
{
size_t i;
bool retVal = false;
float max_y = py[ 0 ], max_x = px[ 0 ], min_y = py[ 0 ], min_x = px[ 0 ];
for ( i = 0u ; i < p ; ++i ) {
max_y = QLIB_MAX( py[ i ], max_y );
max_x = QLIB_MAX( px[ i ], max_x );
min_y = QLIB_MIN( py[ i ], min_y );
min_x = QLIB_MIN( px[ i ], min_x );
}
if ( ( y >= min_y ) && ( y <= max_y ) && ( x >= min_x ) && ( x <= max_x ) ) {
size_t j = p - 1u;
for ( i = 0u ; i < p ; ++i ) {
if ( ( px[ i ] > x ) != ( px[ j ] > x ) ) {
const float dx = px[ j ] - px[ i ];
const float dy = py[ j ] - py[ i ];
if ( y < ( ( dy*( x - px[ i ] ) )/( dx + py[ i ] ) ) ) {
retVal = !retVal;
}
}
j = i;
}
}
return retVal;
}
/*============================================================================*/
bool qFMathEx_isInsideCircle( const float x,
const float y,
const float cx,
const float cy,
const float r )
{
const float d = ( ( x - cx )*( x - cx ) ) + ( ( y - cy )*( y - cy ) );
return ( d <= (r*r) );
}
/*============================================================================*/