/[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 83 - (show annotations) (download)
Fri Jul 16 21:26:08 2010 UTC (10 years, 1 month ago) by william
File size: 7205 byte(s)
bulk commit --
** Enable Support for Skeletons & Bones

** A bug has found its way into the code
 -get an unhandled EHAccessViolation
 - I think the problem is in the Textures Class (or one of the sub-classes)

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

  ViewVC Help
Powered by ViewVC 1.1.22