Rebuild autotool system
[construo.git] / math.hxx
blob884335622621c0d0261d62547aca64dfc0bf7afa
1 // $Id: math.hxx,v 1.5 2003/07/24 21:43:35 grumbel Exp $
2 //
3 // Construo - A wire-frame construction gamee
4 // Copyright (C) 2000 Ingo Ruhnke <grumbel@gmx.de>
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #ifndef CONSTRUO_MATH_HXX
21 #define CONSTRUO_MATH_HXX
23 /** A collection of small math helper functions, some of them might be
24 equal in functionality to standard STL functions, but provided
25 here for portability and broken STL implementations
27 @brief A collection of mathematical helper functions */
28 namespace Math {
30 const double pi = 3.14159265358979323846; /* pi */
31 const double pi_2 = 1.57079632679489661923; /* pi/2 */
33 template<class T>
34 T min (const T& a, const T& b)
36 if (a < b)
37 return a;
38 else
39 return b;
42 template<class T>
43 T max (const T& a, const T& b)
45 if (a > b)
46 return a;
47 else
48 return b;
51 template<class T>
52 T mid (const T& a, const T& b, const T& c)
54 return max((a), min((b), (c)));
57 inline int round(float a)
59 return int((a > 0) ? (a + .5f) : (a - .5));
62 /** Round x to a multilple of n */
63 inline int round_to(float x, int n)
65 if (x > 0)
66 return static_cast<int>(x + (n/2)) / n * n;
67 else
68 return static_cast<int>(x - (n/2)) / n * n;
71 } // namespace Math
73 #endif