/[gr2lib]/trunk/gr2lib/core/coretypes/implementation/MathUtils.cs
ViewVC logotype

Contents of /trunk/gr2lib/core/coretypes/implementation/MathUtils.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 186 - (show annotations) (download)
Sat Jul 24 02:23:18 2010 UTC (9 years, 4 months ago) by william
File size: 9991 byte(s)
continue to add xml documentation

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace gr2lib.core.coretypes.implementation
7 {
8 /// <summary>
9 /// MathUtils class
10 /// </summary>
11 public class MathUtils
12 {
13 // Fields
14 /// <summary>
15 /// kEpsilon
16 /// </summary>
17 public const float kEpsilon = 1E-05f;
18 /// <summary>
19 /// kInvalid
20 /// </summary>
21 public const float kInvalid = float.PositiveInfinity;
22 /// <summary>
23 /// kOutOfWorld
24 /// </summary>
25 public const float kOutOfWorld = -20000f;
26 /// <summary>
27 /// kPrimeNumbers
28 /// </summary>
29 private static readonly long[] kPrimeNumbers = new long[] {
30 2L, 3L, 5L, 7L, 11L, 13L, 0x11L, 0x13L, 0x17L, 0x1dL, 0x1fL, 0x25L, 0x29L, 0x2bL, 0x2fL, 0x35L,
31 0x3bL, 0x3dL, 0x43L, 0x47L, 0x49L, 0x4fL, 0x53L, 0x59L, 0x61L, 0x65L, 0x67L, 0x6bL, 0x6dL, 0x71L, 0x7fL, 0x83L,
32 0x89L, 0x8bL, 0x95L, 0x97L, 0x9dL, 0xa3L, 0xa7L, 0xadL, 0xb3L, 0xb5L, 0xbfL, 0xc1L, 0xc5L, 0xc7L, 0xd3L, 0xdfL,
33 0xe3L, 0xe5L, 0xe9L, 0xefL, 0xf1L, 0xfbL, 0x101L, 0x107L, 0x10dL, 0x10fL, 0x115L, 0x119L, 0x11bL, 0x125L, 0x133L, 0x137L,
34 0x139L, 0x13dL, 0x14bL, 0x151L, 0x15bL, 0x15dL, 0x161L, 0x167L, 0x16fL, 0x175L, 0x17bL, 0x17fL, 0x185L, 0x18dL, 0x191L, 0x199L,
35 0x1a3L, 0x1a5L, 0x1afL, 0x1b1L, 0x1b7L, 0x1bbL, 0x1c1L, 0x1c9L, 0x1cdL, 0x1cfL, 0x1d3L, 0x1dfL, 0x1e7L, 0x1ebL, 0x1f3L, 0x1f7L,
36 0x1fdL, 0x209L, 0x20bL, 0x21dL, 0x223L, 0x22dL, 0x233L, 0x239L, 0x23bL, 0x241L, 0x24bL, 0x251L, 0x257L, 0x259L, 0x25fL, 0x265L,
37 0x269L, 0x26bL, 0x277L, 0x281L, 0x283L, 0x287L, 0x28dL, 0x293L, 0x295L, 0x2a1L, 0x2a5L, 0x2abL, 0x2b3L, 0x2bdL, 0x2c5L, 0x2cfL,
38 0x2d7L, 0x2ddL, 0x2e3L, 0x2e7L, 0x2efL, 0x2f5L, 0x2f9L, 0x301L, 0x305L, 0x313L, 0x31dL, 0x329L, 0x32bL, 0x335L, 0x337L, 0x33bL,
39 0x33dL, 0x347L, 0x355L, 0x359L, 0x35bL, 0x35fL, 0x36dL, 0x371L, 0x373L, 0x377L, 0x38bL, 0x38fL, 0x397L, 0x3a1L, 0x3a9L, 0x3adL,
40 0x3b3L, 0x3b9L, 0x3c7L, 0x3cbL, 0x3d1L, 0x3d7L, 0x3dfL, 0x3e5L
41 };
42
43 // Methods
44 /// <summary>
45 /// int Clamp(int value, int minValue, int maxValue)
46 /// </summary>
47 /// <param name="value"></param>
48 /// <param name="minValue"></param>
49 /// <param name="maxValue"></param>
50 /// <returns></returns>
51 public static int Clamp(int value, int minValue, int maxValue)
52 {
53 if (value < minValue)
54 {
55 return minValue;
56 }
57 if (value > maxValue)
58 {
59 return maxValue;
60 }
61 return value;
62 }
63 /// <summary>
64 /// float Clamp(float value, float minValue, float maxValue)
65 /// </summary>
66 /// <param name="value"></param>
67 /// <param name="minValue"></param>
68 /// <param name="maxValue"></param>
69 /// <returns></returns>
70 public static float Clamp(float value, float minValue, float maxValue)
71 {
72 if (value < minValue)
73 {
74 return minValue;
75 }
76 if (value > maxValue)
77 {
78 return maxValue;
79 }
80 return value;
81 }
82 /// <summary>
83 /// float Degree2Radian(float deg)
84 /// </summary>
85 /// <param name="deg"></param>
86 /// <returns></returns>
87 public static float Degree2Radian(float deg)
88 {
89 return ((3.141593f * deg) / 180f);
90 }
91 /// <summary>
92 /// List of Vector2 FactorsOf(int value)
93 /// </summary>
94 /// <param name="value"></param>
95 /// <returns></returns>
96 public static List<Vector2> FactorsOf(int value)
97 {
98 List<Vector2> list = new List<Vector2>();
99 if (value != 0)
100 {
101 long a = value;
102 long num3 = (long)Math.Floor(Math.Sqrt((double)value));
103 for (int i = 1; i <= num3; i++)
104 {
105 long num5;
106 long num6 = Math.DivRem(a, (long)i, out num5);
107 if (num5 == 0L)
108 {
109 list.Add(new Vector2((float)i, (float)num6));
110 }
111 }
112 }
113 return list;
114 }
115 /// <summary>
116 /// bool FactorsOfUnitTest()
117 /// </summary>
118 /// <returns></returns>
119 public static bool FactorsOfUnitTest()
120 {
121 bool flag = true;
122 if (FactorsOf(0).Count != 0)
123 {
124 flag = false;
125 }
126 List<Vector2> list = FactorsOf(1);
127 if (((list.Count != 1) || (list[0].x != 1f)) || (list[0].y != 1f))
128 {
129 flag = false;
130 }
131 list = FactorsOf(2);
132 if (((list.Count != 1) || (list[0].x != 1f)) || (list[0].y != 2f))
133 {
134 flag = false;
135 }
136 list = FactorsOf(6);
137 if (((list.Count != 2) || (list[0].x != 1f)) || (((list[0].y != 6f) || (list[1].x != 2f)) || (list[1].y != 3f)))
138 {
139 flag = false;
140 }
141 list = FactorsOf(0x18);
142 if ((((list.Count != 4) || (list[0].x != 1f)) || ((list[0].y != 24f) || (list[1].x != 2f))) || (((list[1].y != 12f) || (list[2].x != 3f)) || (((list[2].y != 8f) || (list[3].x != 4f)) || (list[3].y != 6f))))
143 {
144 flag = false;
145 }
146 list = FactorsOf(100);
147 return (((((list.Count == 5) && (list[0].x == 1f)) && ((list[0].y == 100f) && (list[1].x == 2f))) && ((((list[1].y == 50f) && (list[2].x == 4f)) && ((list[2].y == 25f) && (list[3].x == 5f))) && (((list[3].y == 20f) && (list[4].x == 10f)) && (list[4].y == 10f)))) && flag);
148 }
149 /// <summary>
150 /// float Floor(float value)
151 /// </summary>
152 /// <param name="value"></param>
153 /// <returns></returns>
154 public static float Floor(float value)
155 {
156 return (float)Math.Floor((double)value);
157 }
158 /// <summary>
159 /// bool IsSimilar(float x, float y)
160 /// </summary>
161 /// <param name="x"></param>
162 /// <param name="y"></param>
163 /// <returns></returns>
164 public static bool IsSimilar(float x, float y)
165 {
166 return (Math.Abs((float)(x - y)) < 1E-05f);
167 }
168 /// <summary>
169 /// IsSimilar(float x, float y, float tolerance)
170 /// </summary>
171 /// <param name="x"></param>
172 /// <param name="y"></param>
173 /// <param name="tolerance"></param>
174 /// <returns></returns>
175 public static bool IsSimilar(float x, float y, float tolerance)
176 {
177 return (Math.Abs((float)(x - y)) < tolerance);
178 }
179 /// <summary>
180 /// List of long PrimeFactorsOf(long longValue)
181 /// </summary>
182 /// <param name="longValue"></param>
183 /// <returns></returns>
184 public static List<long> PrimeFactorsOf(long longValue)
185 {
186 List<long> list = new List<long>();
187 while (longValue > 1L)
188 {
189 int index = 0;
190 while (index < kPrimeNumbers.Length)
191 {
192 long num3;
193 long b = kPrimeNumbers[index];
194 long num4 = Math.DivRem(longValue, b, out num3);
195 if (num3 == 0L)
196 {
197 list.Add(b);
198 longValue = num4;
199 break;
200 }
201 index++;
202 }
203 if (index >= kPrimeNumbers.Length)
204 {
205 return list;
206 }
207 }
208 return list;
209 }
210 /// <summary>
211 /// bool PrimeFactorsOfUnitTest()
212 /// </summary>
213 /// <returns></returns>
214 public static bool PrimeFactorsOfUnitTest()
215 {
216 bool flag = true;
217 if (PrimeFactorsOf(0L).Count != 0)
218 {
219 flag = false;
220 }
221 if (PrimeFactorsOf(1L).Count != 0)
222 {
223 flag = false;
224 }
225 List<long> list = PrimeFactorsOf(2L);
226 if ((list.Count != 1) || (list[0] != 2L))
227 {
228 flag = false;
229 }
230 list = PrimeFactorsOf(6L);
231 if (((list.Count != 2) || (list[0] != 2L)) || (list[1] != 3L))
232 {
233 flag = false;
234 }
235 list = PrimeFactorsOf(0x18L);
236 if (((list.Count != 4) || (list[0] != 2L)) || (((list[1] != 2L) || (list[2] != 2L)) || (list[3] != 3L)))
237 {
238 flag = false;
239 }
240 list = PrimeFactorsOf(100L);
241 return ((((list.Count == 4) && (list[0] == 2L)) && (((list[1] == 2L) && (list[2] == 5L)) && (list[3] == 5L))) && flag);
242 }
243 /// <summary>
244 /// float ToDegrees(float radians)
245 /// </summary>
246 /// <param name="radians"></param>
247 /// <returns></returns>
248 public static float ToDegrees(float radians)
249 {
250 return Microsoft.Xna.Framework.MathHelper.ToDegrees(radians);
251 }
252 /// <summary>
253 /// float ToRadians(float degrees)
254 /// </summary>
255 /// <param name="degrees"></param>
256 /// <returns></returns>
257 public static float ToRadians(float degrees)
258 {
259 return Microsoft.Xna.Framework.MathHelper.ToRadians(degrees);
260 }
261 }
262 }

  ViewVC Help
Powered by ViewVC 1.1.22