1 /*
2 Copyright (C) 2004 Parallel Realities
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 
13 See the GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 
19 */
20 
21 #include "headers.h"
22 
limitChar(signed char * in,int low,int high)23 void Math::limitChar(signed char *in, int low, int high)
24 {
25 	if (*in < low)
26 		*in = low;
27 	if (*in > high)
28 		*in = high;
29 }
30 
limitChar(unsigned char * in,int low,int high)31 void Math::limitChar(unsigned char *in, int low, int high)
32 {
33 	if (*in < low)
34 		*in = low;
35 	if (*in > high)
36 		*in = high;
37 }
38 
limitInt(int * in,int low,int high)39 void Math::limitInt(int *in, int low, int high)
40 {
41 	if (*in < low)
42 		*in = low;
43 	if (*in > high)
44 		*in = high;
45 }
46 
limitFloat(float * in,float low,float high)47 void Math::limitFloat(float *in, float low, float high)
48 {
49 	if (*in < low)
50 		*in = low;
51 	if (*in > high)
52 		*in = high;
53 }
54 
wrapChar(signed char * in,signed char low,signed char high)55 void Math::wrapChar(signed char *in, signed char low, signed char high)
56 {
57 	if (*in < low)
58 		*in = high;
59 	if (*in > high)
60 		*in = low;
61 }
62 
wrapInt(int * in,int low,int high)63 void Math::wrapInt(int *in, int low, int high)
64 {
65 	if (*in < low)
66 		*in = high;
67 	if (*in > high)
68 		*in = low;
69 }
70 
wrapFloat(float * in,float low,float high)71 void Math::wrapFloat(float *in, float low, float high)
72 {
73 	if (*in < low)
74 		*in = high;
75 	if (*in > high)
76 		*in = low;
77 }
78 
swap(int * i1,int * i2)79 void Math::swap(int *i1, int *i2)
80 {
81 	int temp = *i1;
82 	*i2 = *i1;
83 	*i1 = temp;
84 }
85 
swap(float * f1,float * f2)86 void Math::swap(float *f1, float *f2)
87 {
88 	float temp = *f1;
89 	*f2 = *f1;
90 	*f1 = temp;
91 }
92 
rrand(int min,int max)93 int Math::rrand(int min, int max)
94 {
95 	int r = min;
96 
97 	max++;
98 
99 	if ((max - min) == 0)
100 		return min;
101 
102 	r += rand() % (max - min);
103 
104 	return r;
105 }
106 
boolFromWord(const char * word)107 bool Math::boolFromWord(const char *word)
108 {
109 	if (strcmp(word, "TRUE") == 0)
110 		return true;
111 
112 	return false;
113 }
114 
addBit(unsigned int * currentBits,int newBits)115 void Math::addBit(unsigned int *currentBits, int newBits)
116 {
117 	if (!(*currentBits & newBits))
118 		*currentBits += newBits;
119 }
120 
removeBit(unsigned int * currentBits,int oldBits)121 void Math::removeBit(unsigned int *currentBits, int oldBits)
122 {
123 	if (*currentBits & oldBits)
124 		*currentBits -= oldBits;
125 }
126 
calculateSlope(float x,float y,float x2,float y2,float * dx,float * dy)127 void Math::calculateSlope(float x, float y, float x2, float y2, float *dx, float *dy)
128 {
129 	int steps = (int)max(fabs(x - x2), fabs(y - y2));
130 	if (steps == 0)
131 	{
132 		*dx = 0;
133 		*dy = 0;
134 		return;
135 	}
136 
137 	*dx = (x - x2);
138 	*dx /= steps;
139 	*dy = (y - y2);
140 	*dy /= steps;
141 }
142