/[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 196 - (show annotations) (download)
Thu Jul 29 07:47:26 2010 UTC (10 years ago) by william
File size: 9703 byte(s)
bulk commit

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 /// List of Vector2 FactorsOf(int value)
84 /// </summary>
85 /// <param name="value"></param>
86 /// <returns></returns>
87 public static List<Vector2> FactorsOf(int value)
88 {
89 List<Vector2> list = new List<Vector2>();
90 if (value != 0)
91 {
92 long a = value;
93 long num3 = (long)Math.Floor(Math.Sqrt((double)value));
94 for (int i = 1; i <= num3; i++)
95 {
96 long num5;
97 long num6 = Math.DivRem(a, (long)i, out num5);
98 if (num5 == 0L)
99 {
100 list.Add(new Vector2((float)i, (float)num6));
101 }
102 }
103 }
104 return list;
105 }
106 /// <summary>
107 /// bool FactorsOfUnitTest()
108 /// </summary>
109 /// <returns></returns>
110 public static bool FactorsOfUnitTest()
111 {
112 bool flag = true;
113 if (FactorsOf(0).Count != 0)
114 {
115 flag = false;
116 }
117 List<Vector2> list = FactorsOf(1);
118 if (((list.Count != 1) || (list[0].x != 1f)) || (list[0].y != 1f))
119 {
120 flag = false;
121 }
122 list = FactorsOf(2);
123 if (((list.Count != 1) || (list[0].x != 1f)) || (list[0].y != 2f))
124 {
125 flag = false;
126 }
127 list = FactorsOf(6);
128 if (((list.Count != 2) || (list[0].x != 1f)) || (((list[0].y != 6f) || (list[1].x != 2f)) || (list[1].y != 3f)))
129 {
130 flag = false;
131 }
132 list = FactorsOf(0x18);
133 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))))
134 {
135 flag = false;
136 }
137 list = FactorsOf(100);
138 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);
139 }
140 /// <summary>
141 /// float Floor(float value)
142 /// </summary>
143 /// <param name="value"></param>
144 /// <returns></returns>
145 public static float Floor(float value)
146 {
147 return (float)Math.Floor((double)value);
148 }
149 /// <summary>
150 /// bool IsSimilar(float x, float y)
151 /// </summary>
152 /// <param name="x"></param>
153 /// <param name="y"></param>
154 /// <returns></returns>
155 public static bool IsSimilar(float x, float y)
156 {
157 return (Math.Abs((float)(x - y)) < 1E-05f);
158 }
159 /// <summary>
160 /// IsSimilar(float x, float y, float tolerance)
161 /// </summary>
162 /// <param name="x"></param>
163 /// <param name="y"></param>
164 /// <param name="tolerance"></param>
165 /// <returns></returns>
166 public static bool IsSimilar(float x, float y, float tolerance)
167 {
168 return (Math.Abs((float)(x - y)) < tolerance);
169 }
170 /// <summary>
171 /// List of long PrimeFactorsOf(long longValue)
172 /// </summary>
173 /// <param name="longValue"></param>
174 /// <returns></returns>
175 public static List<long> PrimeFactorsOf(long longValue)
176 {
177 List<long> list = new List<long>();
178 while (longValue > 1L)
179 {
180 int index = 0;
181 while (index < kPrimeNumbers.Length)
182 {
183 long num3;
184 long b = kPrimeNumbers[index];
185 long num4 = Math.DivRem(longValue, b, out num3);
186 if (num3 == 0L)
187 {
188 list.Add(b);
189 longValue = num4;
190 break;
191 }
192 index++;
193 }
194 if (index >= kPrimeNumbers.Length)
195 {
196 return list;
197 }
198 }
199 return list;
200 }
201 /// <summary>
202 /// bool PrimeFactorsOfUnitTest()
203 /// </summary>
204 /// <returns></returns>
205 public static bool PrimeFactorsOfUnitTest()
206 {
207 bool flag = true;
208 if (PrimeFactorsOf(0L).Count != 0)
209 {
210 flag = false;
211 }
212 if (PrimeFactorsOf(1L).Count != 0)
213 {
214 flag = false;
215 }
216 List<long> list = PrimeFactorsOf(2L);
217 if ((list.Count != 1) || (list[0] != 2L))
218 {
219 flag = false;
220 }
221 list = PrimeFactorsOf(6L);
222 if (((list.Count != 2) || (list[0] != 2L)) || (list[1] != 3L))
223 {
224 flag = false;
225 }
226 list = PrimeFactorsOf(0x18L);
227 if (((list.Count != 4) || (list[0] != 2L)) || (((list[1] != 2L) || (list[2] != 2L)) || (list[3] != 3L)))
228 {
229 flag = false;
230 }
231 list = PrimeFactorsOf(100L);
232 return ((((list.Count == 4) && (list[0] == 2L)) && (((list[1] == 2L) && (list[2] == 5L)) && (list[3] == 5L))) && flag);
233 }
234 /// <summary>
235 /// float ToDegrees(float radians)
236 /// </summary>
237 /// <param name="radians"></param>
238 /// <returns></returns>
239 public static float ToDegrees(float radians)
240 {
241 return Microsoft.Xna.Framework.MathHelper.ToDegrees(radians);
242 }
243 /// <summary>
244 /// float ToRadians(float degrees)
245 /// </summary>
246 /// <param name="degrees"></param>
247 /// <returns></returns>
248 public static float ToRadians(float degrees)
249 {
250 return Microsoft.Xna.Framework.MathHelper.ToRadians(degrees);
251 }
252 }
253 }

  ViewVC Help
Powered by ViewVC 1.1.22