1 // $Id: math.hxx,v 1.5 2003/07/24 21:43:35 grumbel Exp $
3 // Construo - A wire-frame construction gamee
4 // Copyright (C) 2000 Ingo Ruhnke <grumbel@gmx.de>
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.
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 */
30 const double pi
= 3.14159265358979323846; /* pi */
31 const double pi_2
= 1.57079632679489661923; /* pi/2 */
34 T
min (const T
& a
, const T
& b
)
43 T
max (const T
& a
, const T
& b
)
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
)
66 return static_cast<int>(x
+ (n
/2)) / n
* n
;
68 return static_cast<int>(x
- (n
/2)) / n
* n
;