1 // misc.h
2 //
3 // Copyright (C) 2003, 2004 Jason Bevins
4 //
5 // This library is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation; either version 2.1 of the License, or (at
8 // your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
13 // License (COPYING.txt) for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this library; if not, write to the Free Software Foundation,
17 // Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 //
19 // The developer's email is jlbezigvins@gmzigail.com (for great email, take
20 // off every 'zig'.)
21 //
22 module noise.misc;
23 
24 /// Clamps a value onto a clamping range.
25 ///
26 /// @param value The value to clamp.
27 /// @param lowerBound The lower bound of the clamping range.
28 /// @param upperBound The upper bound of the clamping range.
29 ///
30 /// @returns
31 /// - @a value if @a value lies between @a lowerBound and @a upperBound.
32 /// - @a lowerBound if @a value is less than @a lowerBound.
33 /// - @a upperBound if @a value is greater than @a upperBound.
34 ///
35 /// This function does not modify any parameters.
36 int ClampValue (int value, int lowerBound, int upperBound)
37 {
38   if (value < lowerBound) {
39     return lowerBound;
40   } else if (value > upperBound) {
41     return upperBound;
42   } else {
43     return value;
44   }
45 }
46 
47 /// @addtogroup libnoise
48 /// @{
49 
50 /// Returns the maximum of two values.
51 ///
52 /// @param a The first value.
53 /// @param b The second value.
54 ///
55 /// @returns The maximum of the two values.
56 T GetMax(T) (const ref T a, const ref T b)
57 {
58   return (a > b? a: b);
59 }
60 
61 /// Returns the minimum of two values.
62 ///
63 /// @param a The first value.
64 /// @param b The second value.
65 ///
66 /// @returns The minimum of the two values.
67 T GetMin(T) (const ref T a, const ref T b)
68 {
69   return (a < b? a: b);
70 }
71 
72 /// Swaps two values.
73 ///
74 /// @param a A variable containing the first value.
75 /// @param b A variable containing the second value.
76 ///
77 /// @post The values within the the two variables are swapped.
78 void SwapValues(T) (ref T a, ref T b)
79 {
80   T c = a;
81   a = b;
82   b = c;
83 }
84 
85 /// @}
86 
87