Rev 817 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 667 | chris | 1 | using System; |
| 2 | using System.Collections.Generic; |
||
| 3 | using System.Linq; |
||
| 4 | using System.Text; |
||
| 5 | using System.Threading.Tasks; |
||
| 6 | |||
| 7 | namespace BauzoidNET.math |
||
| 8 | { |
||
| 9 | public class MathUtil |
||
| 10 | { |
||
| 11 | public const float EPSILON = 0.0000001f; |
||
| 12 | |||
| 13 | /// <summary> |
||
| 14 | /// Check if value is in range (inclusive). |
||
| 15 | /// </summary> |
||
| 16 | public static bool isInRange(float value, float min, float max) |
||
| 17 | { |
||
| 18 | return ((value >= min) && (value <= max)); |
||
| 19 | } |
||
| 20 | |||
| 21 | /// <summary> |
||
| 22 | /// Check if value is in range (inclusive min, exclusive max). |
||
| 23 | /// </summary> |
||
| 24 | public static bool isInRangeInEx(float value, float min, float max) |
||
| 25 | { |
||
| 26 | return ((value >= min) && (value < max)); |
||
| 27 | } |
||
| 28 | |||
| 29 | /// <summary> |
||
| 30 | /// Check if value is in range (exclusive min, inclusive max). |
||
| 31 | /// </summary> |
||
| 32 | public static bool isInRangeExIn(float value, float min, float max) |
||
| 33 | { |
||
| 34 | return ((value > min) && (value <= max)); |
||
| 35 | } |
||
| 36 | |||
| 37 | /// <summary> |
||
| 38 | /// Check if value is in range (exclusive). |
||
| 39 | /// </summary> |
||
| 40 | public static bool isInRangeEx(float value, float min, float max) |
||
| 41 | { |
||
| 42 | return ((value > min) && (value < max)); |
||
| 43 | } |
||
| 44 | |||
| 45 | /// <summary> |
||
| 46 | /// Check if value is in range (inclusive). |
||
| 47 | /// </summary> |
||
| 48 | public static bool isInRange(int value, int min, int max) |
||
| 49 | { |
||
| 50 | return ((value >= min) && (value <= max)); |
||
| 51 | } |
||
| 52 | |||
| 53 | /// <summary> |
||
| 54 | /// Clamp value to range. |
||
| 55 | /// </summary> |
||
| 56 | public static float clamp(float value, float min, float max) |
||
| 57 | { |
||
| 58 | if (value < min) |
||
| 59 | return min; |
||
| 60 | if (value > max) |
||
| 61 | return max; |
||
| 62 | return value; |
||
| 63 | } |
||
| 64 | |||
| 65 | /// <summary> |
||
| 66 | /// Clamp value to range. |
||
| 67 | /// </summary> |
||
| 68 | public static int clamp(int value, int min, int max) |
||
| 69 | { |
||
| 70 | if (value < min) |
||
| 71 | return min; |
||
| 72 | if (value > max) |
||
| 73 | return max; |
||
| 74 | return value; |
||
| 75 | } |
||
| 76 | |||
| 77 | /// <summary> |
||
| 78 | /// Float-based sine function in degrees. |
||
| 79 | /// </summary> |
||
| 80 | public static float sin(float degrees) |
||
| 81 | { |
||
| 82 | return (float)Math.Sin(degToRad(degrees)); |
||
| 83 | } |
||
| 84 | |||
| 85 | /// <summary> |
||
| 86 | /// Float-based cosine function in degrees. |
||
| 87 | /// </summary> |
||
| 88 | public static float cos(float degrees) |
||
| 89 | { |
||
| 90 | return (float)Math.Cos(degToRad(degrees)); |
||
| 91 | } |
||
| 92 | |||
| 93 | /// <summary> |
||
| 94 | /// Float-based tangent function in degrees. |
||
| 95 | /// </summary> |
||
| 96 | public static float tan(float degrees) |
||
| 97 | { |
||
| 98 | return (float)Math.Tan(degToRad(degrees)); |
||
| 99 | } |
||
| 100 | |||
| 101 | /// <summary> |
||
| 102 | /// Convert from degrees to radians. |
||
| 103 | /// </summary> |
||
| 104 | public static float degToRad(float degrees) |
||
| 105 | { |
||
| 106 | return (float)(degrees * Math.PI / 180.0f); |
||
| 107 | } |
||
| 108 | |||
| 109 | /// <summary> |
||
| 110 | /// Convert from radians to degrees. |
||
| 111 | /// </summary> |
||
| 112 | public static float radToDeg(float rad) |
||
| 113 | { |
||
| 114 | return (float)(rad * 180.0f / Math.PI); |
||
| 115 | } |
||
| 116 | |||
| 117 | /// <summary> |
||
| 118 | /// Return the angle inside a range of 0 to 360 degrees. |
||
| 119 | /// </summary> |
||
| 120 | public static float stayInDegrees0to360(float degrees) |
||
| 121 | { |
||
| 122 | float result = degrees; |
||
| 123 | while (result > 360.0f) |
||
| 124 | result -= 360.0f; |
||
| 125 | while (result < 0.0f) |
||
| 126 | result += 360.0f; |
||
| 127 | return result; |
||
| 128 | } |
||
| 129 | |||
| 130 | /// <summary> |
||
| 131 | /// Return the (smaller) angle in degrees between the two angles. |
||
| 132 | /// </summary> |
||
| 133 | public static float turnDegrees(float rot1, float rot2) |
||
| 134 | { |
||
| 135 | if (Math.Abs(rot1 - rot2) > (180.0f)) |
||
| 136 | { |
||
| 137 | if (rot1 < rot2) |
||
| 138 | { |
||
| 139 | rot1 += 360.0f; |
||
| 140 | } |
||
| 141 | else |
||
| 142 | { |
||
| 143 | rot1 -= 360.0f; |
||
| 144 | } |
||
| 145 | } |
||
| 146 | return Math.Abs(rot2 - rot1); |
||
| 147 | } |
||
| 811 | chris | 148 | |
| 149 | /// <summary> |
||
| 150 | /// Convert RGB values to HSV values |
||
| 151 | /// </summary> |
||
| 152 | public static Vector3 rgbToHsv(Vector3 rgbColor) |
||
| 153 | { |
||
| 154 | float r = rgbColor.x; |
||
| 155 | float g = rgbColor.y; |
||
| 156 | float b = rgbColor.z; |
||
| 157 | |||
| 158 | float h = 0; |
||
| 159 | float s = 0; |
||
| 160 | float v = 0; |
||
| 161 | |||
| 162 | float min = rgbColor.min(); |
||
| 163 | float max = rgbColor.max(); |
||
| 164 | |||
| 165 | v = max; |
||
| 166 | |||
| 167 | float delta = max - min; |
||
| 817 | chris | 168 | if (v != 0) |
| 811 | chris | 169 | { |
| 817 | chris | 170 | r /= v; |
| 171 | g /= v; |
||
| 172 | b /= v; |
||
| 173 | Vector3 rgb = new Vector3(r, g, b); |
||
| 174 | min = rgb.min(); |
||
| 175 | max = rgb.max(); |
||
| 811 | chris | 176 | |
| 817 | chris | 177 | s = max - min; |
| 178 | if (s == 0) |
||
| 179 | { |
||
| 180 | h = 0; |
||
| 181 | return new Vector3(h, s, v); |
||
| 182 | } |
||
| 183 | |||
| 184 | rgb.x = (rgb.x - min) / (max - min); |
||
| 185 | rgb.y = (rgb.y - min) / (max - min); |
||
| 186 | rgb.z = (rgb.z - min) / (max - min); |
||
| 187 | min = rgb.min(); |
||
| 188 | max = rgb.max(); |
||
| 189 | |||
| 190 | if (max == rgb.x) |
||
| 191 | { |
||
| 192 | h = 0.0f + 60.0f * (rgb.y - rgb.z); |
||
| 193 | if (h < 0.0f) |
||
| 194 | h += 360.0f; |
||
| 195 | } |
||
| 196 | else if (max == rgb.y) |
||
| 197 | { |
||
| 825 | chris | 198 | h = 120.0f + 60.0f * (rgb.z - rgb.x); |
| 817 | chris | 199 | } |
| 200 | else |
||
| 201 | { |
||
| 202 | h = 240.0f + 60.0f * (rgb.x - rgb.y); |
||
| 203 | } |
||
| 204 | |||
| 205 | //h = hueDegrees / 360.0f; |
||
| 206 | |||
| 207 | |||
| 208 | |||
| 209 | /*s = delta / max; |
||
| 210 | |||
| 811 | chris | 211 | if (r == max) |
| 212 | h = (g - b) / delta; |
||
| 213 | else if (g == max) |
||
| 214 | h = 2 + (b - r) / delta; |
||
| 215 | else |
||
| 216 | h = 4 + (r - g) / delta; |
||
| 217 | |||
| 218 | h *= 60; |
||
| 219 | |||
| 220 | if (h < 0) |
||
| 817 | chris | 221 | h += 360;*/ |
| 811 | chris | 222 | } |
| 223 | else |
||
| 224 | { |
||
| 225 | s = 0; |
||
| 817 | chris | 226 | h = 0; |
| 811 | chris | 227 | } |
| 228 | |||
| 229 | return new Vector3(h, s, v); |
||
| 230 | } |
||
| 231 | |||
| 232 | /// <summary> |
||
| 233 | /// Convert HSV values to RGBvalues |
||
| 234 | /// </summary> |
||
| 235 | public static Vector3 hsvToRgb(Vector3 hsvColor) |
||
| 236 | { |
||
| 237 | float h = hsvColor.x; |
||
| 238 | float s = hsvColor.y; |
||
| 239 | float v = hsvColor.z; |
||
| 240 | |||
| 817 | chris | 241 | //if (h == 1.0f) |
| 242 | // h = 0.0f; |
||
| 813 | chris | 243 | |
| 811 | chris | 244 | float r = 0; |
| 245 | float g = 0; |
||
| 246 | float b = 0; |
||
| 247 | |||
| 248 | if (s == 0) |
||
| 249 | { |
||
| 250 | // achromatic (grey) |
||
| 251 | r = v; |
||
| 252 | g = v; |
||
| 253 | b = v; |
||
| 254 | } |
||
| 255 | else |
||
| 256 | { |
||
| 813 | chris | 257 | /*h /= 60; |
| 811 | chris | 258 | int i = (int)Math.Floor(h); |
| 259 | float f = h - i; // factorial part of h |
||
| 260 | float p = v * (1 - s); |
||
| 261 | float q = v * (1 - s * f); |
||
| 813 | chris | 262 | float t = v * (1 - s * (1 - f));*/ |
| 263 | |||
| 817 | chris | 264 | double step = 1.0 / 60.0; |
| 265 | double vh = h * step; |
||
| 813 | chris | 266 | |
| 267 | int i = (int)System.Math.Floor(vh); |
||
| 268 | |||
| 269 | float f = (float)(vh - i); |
||
| 270 | float p = (float)(v * (1.0 - s)); |
||
| 271 | float q = (float)(v * (1.0 - (s * f))); |
||
| 272 | float t = (float)(v * (1.0 - (s * (1.0 - f)))); |
||
| 273 | |||
| 811 | chris | 274 | switch(i) |
| 275 | { |
||
| 276 | case 0: |
||
| 277 | r = v; |
||
| 278 | g = t; |
||
| 279 | b = p; |
||
| 280 | break; |
||
| 281 | case 1: |
||
| 282 | r = q; |
||
| 283 | g = v; |
||
| 284 | b = p; |
||
| 285 | break; |
||
| 813 | chris | 286 | case 2: |
| 287 | r = p; |
||
| 288 | g = v; |
||
| 289 | b = t; |
||
| 290 | break; |
||
| 811 | chris | 291 | case 3: |
| 813 | chris | 292 | r = p; |
| 293 | g = q; |
||
| 811 | chris | 294 | b = v; |
| 295 | break; |
||
| 296 | case 4: |
||
| 297 | r = t; |
||
| 298 | g = p; |
||
| 299 | b = v; |
||
| 300 | break; |
||
| 301 | default: |
||
| 302 | r = v; |
||
| 303 | g = p; |
||
| 304 | b = q; |
||
| 305 | break; |
||
| 306 | } |
||
| 307 | } |
||
| 308 | |||
| 309 | return new Vector3(r, g, b); |
||
| 310 | } |
||
| 667 | chris | 311 | } |
| 312 | } |