Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1452 | chris | 1 | #region --- License --- |
| 2 | /* |
||
| 3 | Copyright (c) 2006 - 2008 The Open Toolkit library. |
||
| 4 | |||
| 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of |
||
| 6 | this software and associated documentation files (the "Software"), to deal in |
||
| 7 | the Software without restriction, including without limitation the rights to |
||
| 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
||
| 9 | of the Software, and to permit persons to whom the Software is furnished to do |
||
| 10 | so, subject to the following conditions: |
||
| 11 | |||
| 12 | The above copyright notice and this permission notice shall be included in all |
||
| 13 | copies or substantial portions of the Software. |
||
| 14 | |||
| 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||
| 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||
| 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||
| 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||
| 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||
| 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||
| 21 | SOFTWARE. |
||
| 22 | */ |
||
| 23 | #endregion |
||
| 24 | |||
| 25 | using System; |
||
| 26 | using System.Runtime.InteropServices; |
||
| 27 | |||
| 28 | namespace OpenTK.Math |
||
| 29 | { |
||
| 30 | /// <summary>Represents a 2D vector using two double-precision floating-point numbers.</summary> |
||
| 31 | [Obsolete("OpenTK.Math functions have been moved to the root OpenTK namespace (reason: XNA compatibility")] |
||
| 32 | [Serializable] |
||
| 33 | [StructLayout(LayoutKind.Sequential)] |
||
| 34 | public struct Vector2d : IEquatable<Vector2d> |
||
| 35 | { |
||
| 36 | #region Fields |
||
| 37 | |||
| 38 | /// <summary>The X coordinate of this instance.</summary> |
||
| 39 | public double X; |
||
| 40 | |||
| 41 | /// <summary>The Y coordinate of this instance.</summary> |
||
| 42 | public double Y; |
||
| 43 | |||
| 44 | /// <summary> |
||
| 45 | /// Defines a unit-length Vector2d that points towards the X-axis. |
||
| 46 | /// </summary> |
||
| 47 | public static Vector2d UnitX = new Vector2d(1, 0); |
||
| 48 | |||
| 49 | /// <summary> |
||
| 50 | /// Defines a unit-length Vector2d that points towards the Y-axis. |
||
| 51 | /// </summary> |
||
| 52 | public static Vector2d UnitY = new Vector2d(0, 1); |
||
| 53 | |||
| 54 | /// <summary> |
||
| 55 | /// Defines a zero-length Vector2d. |
||
| 56 | /// </summary> |
||
| 57 | public static Vector2d Zero = new Vector2d(0, 0); |
||
| 58 | |||
| 59 | /// <summary> |
||
| 60 | /// Defines an instance with all components set to 1. |
||
| 61 | /// </summary> |
||
| 62 | public static readonly Vector2d One = new Vector2d(1, 1); |
||
| 63 | |||
| 64 | /// <summary> |
||
| 65 | /// Defines the size of the Vector2d struct in bytes. |
||
| 66 | /// </summary> |
||
| 67 | public static readonly int SizeInBytes = Marshal.SizeOf(new Vector2d()); |
||
| 68 | |||
| 69 | #endregion |
||
| 70 | |||
| 71 | #region Constructors |
||
| 72 | |||
| 73 | /// <summary>Constructs left vector with the given coordinates.</summary> |
||
| 74 | /// <param name="x">The X coordinate.</param> |
||
| 75 | /// <param name="y">The Y coordinate.</param> |
||
| 76 | public Vector2d(double x, double y) |
||
| 77 | { |
||
| 78 | this.X = x; |
||
| 79 | this.Y = y; |
||
| 80 | } |
||
| 81 | |||
| 82 | #endregion |
||
| 83 | |||
| 84 | #region Public Members |
||
| 85 | |||
| 86 | #region Instance |
||
| 87 | |||
| 88 | #region public void Add() |
||
| 89 | |||
| 90 | /// <summary>Add the Vector passed as parameter to this instance.</summary> |
||
| 91 | /// <param name="right">Right operand. This parameter is only read from.</param> |
||
| 92 | public void Add(Vector2d right) |
||
| 93 | { |
||
| 94 | this.X += right.X; |
||
| 95 | this.Y += right.Y; |
||
| 96 | } |
||
| 97 | |||
| 98 | /// <summary>Add the Vector passed as parameter to this instance.</summary> |
||
| 99 | /// <param name="right">Right operand. This parameter is only read from.</param> |
||
| 100 | [CLSCompliant(false)] |
||
| 101 | public void Add(ref Vector2d right) |
||
| 102 | { |
||
| 103 | this.X += right.X; |
||
| 104 | this.Y += right.Y; |
||
| 105 | } |
||
| 106 | |||
| 107 | #endregion public void Add() |
||
| 108 | |||
| 109 | #region public void Sub() |
||
| 110 | |||
| 111 | /// <summary>Subtract the Vector passed as parameter from this instance.</summary> |
||
| 112 | /// <param name="right">Right operand. This parameter is only read from.</param> |
||
| 113 | public void Sub(Vector2d right) |
||
| 114 | { |
||
| 115 | this.X -= right.X; |
||
| 116 | this.Y -= right.Y; |
||
| 117 | } |
||
| 118 | |||
| 119 | /// <summary>Subtract the Vector passed as parameter from this instance.</summary> |
||
| 120 | /// <param name="right">Right operand. This parameter is only read from.</param> |
||
| 121 | [CLSCompliant(false)] |
||
| 122 | public void Sub(ref Vector2d right) |
||
| 123 | { |
||
| 124 | this.X -= right.X; |
||
| 125 | this.Y -= right.Y; |
||
| 126 | } |
||
| 127 | |||
| 128 | #endregion public void Sub() |
||
| 129 | |||
| 130 | #region public void Mult() |
||
| 131 | |||
| 132 | /// <summary>Multiply this instance by a scalar.</summary> |
||
| 133 | /// <param name="f">Scalar operand.</param> |
||
| 134 | public void Mult(double f) |
||
| 135 | { |
||
| 136 | this.X *= f; |
||
| 137 | this.Y *= f; |
||
| 138 | } |
||
| 139 | |||
| 140 | #endregion public void Mult() |
||
| 141 | |||
| 142 | #region public void Div() |
||
| 143 | |||
| 144 | /// <summary>Divide this instance by a scalar.</summary> |
||
| 145 | /// <param name="f">Scalar operand.</param> |
||
| 146 | public void Div(double f) |
||
| 147 | { |
||
| 148 | double mult = 1.0 / f; |
||
| 149 | this.X *= mult; |
||
| 150 | this.Y *= mult; |
||
| 151 | } |
||
| 152 | |||
| 153 | #endregion public void Div() |
||
| 154 | |||
| 155 | #region public double Length |
||
| 156 | |||
| 157 | /// <summary> |
||
| 158 | /// Gets the length (magnitude) of the vector. |
||
| 159 | /// </summary> |
||
| 160 | /// <seealso cref="LengthSquared"/> |
||
| 161 | public double Length |
||
| 162 | { |
||
| 163 | get |
||
| 164 | { |
||
| 165 | return (float)System.Math.Sqrt(X * X + Y * Y); |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | #endregion |
||
| 170 | |||
| 171 | #region public double LengthSquared |
||
| 172 | |||
| 173 | /// <summary> |
||
| 174 | /// Gets the square of the vector length (magnitude). |
||
| 175 | /// </summary> |
||
| 176 | /// <remarks> |
||
| 177 | /// This property avoids the costly square root operation required by the Length property. This makes it more suitable |
||
| 178 | /// for comparisons. |
||
| 179 | /// </remarks> |
||
| 180 | /// <see cref="Length"/> |
||
| 181 | public double LengthSquared |
||
| 182 | { |
||
| 183 | get |
||
| 184 | { |
||
| 185 | return X * X + Y * Y; |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | #endregion |
||
| 190 | |||
| 191 | #region public Vector2d PerpendicularRight |
||
| 192 | |||
| 193 | /// <summary> |
||
| 194 | /// Gets the perpendicular vector on the right side of this vector. |
||
| 195 | /// </summary> |
||
| 196 | public Vector2d PerpendicularRight |
||
| 197 | { |
||
| 198 | get |
||
| 199 | { |
||
| 200 | return new Vector2d(Y, -X); |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | #endregion |
||
| 205 | |||
| 206 | #region public Vector2d PerpendicularLeft |
||
| 207 | |||
| 208 | /// <summary> |
||
| 209 | /// Gets the perpendicular vector on the left side of this vector. |
||
| 210 | /// </summary> |
||
| 211 | public Vector2d PerpendicularLeft |
||
| 212 | { |
||
| 213 | get |
||
| 214 | { |
||
| 215 | return new Vector2d(-Y, X); |
||
| 216 | } |
||
| 217 | } |
||
| 218 | |||
| 219 | #endregion |
||
| 220 | |||
| 221 | #region public void Normalize() |
||
| 222 | |||
| 223 | /// <summary> |
||
| 224 | /// Scales the Vector2 to unit length. |
||
| 225 | /// </summary> |
||
| 226 | public void Normalize() |
||
| 227 | { |
||
| 228 | double scale = 1.0f / Length; |
||
| 229 | X *= scale; |
||
| 230 | Y *= scale; |
||
| 231 | } |
||
| 232 | |||
| 233 | #endregion |
||
| 234 | |||
| 235 | #region public void Scale() |
||
| 236 | |||
| 237 | /// <summary> |
||
| 238 | /// Scales the current Vector2 by the given amounts. |
||
| 239 | /// </summary> |
||
| 240 | /// <param name="sx">The scale of the X component.</param> |
||
| 241 | /// <param name="sy">The scale of the Y component.</param> |
||
| 242 | public void Scale(double sx, double sy) |
||
| 243 | { |
||
| 244 | X *= sx; |
||
| 245 | Y *= sy; |
||
| 246 | } |
||
| 247 | |||
| 248 | /// <summary>Scales this instance by the given parameter.</summary> |
||
| 249 | /// <param name="scale">The scaling of the individual components.</param> |
||
| 250 | public void Scale(Vector2d scale) |
||
| 251 | { |
||
| 252 | this.X *= scale.X; |
||
| 253 | this.Y *= scale.Y; |
||
| 254 | } |
||
| 255 | |||
| 256 | /// <summary>Scales this instance by the given parameter.</summary> |
||
| 257 | /// <param name="scale">The scaling of the individual components.</param> |
||
| 258 | [CLSCompliant(false)] |
||
| 259 | public void Scale(ref Vector2d scale) |
||
| 260 | { |
||
| 261 | this.X *= scale.X; |
||
| 262 | this.Y *= scale.Y; |
||
| 263 | } |
||
| 264 | |||
| 265 | #endregion public void Scale() |
||
| 266 | |||
| 267 | #endregion |
||
| 268 | |||
| 269 | #region Static |
||
| 270 | |||
| 271 | #region Add |
||
| 272 | |||
| 273 | /// <summary> |
||
| 274 | /// Add two Vectors |
||
| 275 | /// </summary> |
||
| 276 | /// <param name="a">First operand</param> |
||
| 277 | /// <param name="b">Second operand</param> |
||
| 278 | /// <returns>Result of addition</returns> |
||
| 279 | public static Vector2d Add(Vector2d a, Vector2d b) |
||
| 280 | { |
||
| 281 | a.X += b.X; |
||
| 282 | a.Y += b.Y; |
||
| 283 | return a; |
||
| 284 | } |
||
| 285 | |||
| 286 | /// <summary> |
||
| 287 | /// Add two Vectors |
||
| 288 | /// </summary> |
||
| 289 | /// <param name="a">First operand</param> |
||
| 290 | /// <param name="b">Second operand</param> |
||
| 291 | /// <param name="result">Result of addition</param> |
||
| 292 | public static void Add(ref Vector2d a, ref Vector2d b, out Vector2d result) |
||
| 293 | { |
||
| 294 | result.X = a.X + b.X; |
||
| 295 | result.Y = a.Y + b.Y; |
||
| 296 | } |
||
| 297 | |||
| 298 | #endregion |
||
| 299 | |||
| 300 | #region Sub |
||
| 301 | |||
| 302 | /// <summary> |
||
| 303 | /// Subtract one Vector from another |
||
| 304 | /// </summary> |
||
| 305 | /// <param name="a">First operand</param> |
||
| 306 | /// <param name="b">Second operand</param> |
||
| 307 | /// <returns>Result of subtraction</returns> |
||
| 308 | public static Vector2d Sub(Vector2d a, Vector2d b) |
||
| 309 | { |
||
| 310 | a.X -= b.X; |
||
| 311 | a.Y -= b.Y; |
||
| 312 | return a; |
||
| 313 | } |
||
| 314 | |||
| 315 | /// <summary> |
||
| 316 | /// Subtract one Vector from another |
||
| 317 | /// </summary> |
||
| 318 | /// <param name="a">First operand</param> |
||
| 319 | /// <param name="b">Second operand</param> |
||
| 320 | /// <param name="result">Result of subtraction</param> |
||
| 321 | public static void Sub(ref Vector2d a, ref Vector2d b, out Vector2d result) |
||
| 322 | { |
||
| 323 | result.X = a.X - b.X; |
||
| 324 | result.Y = a.Y - b.Y; |
||
| 325 | } |
||
| 326 | |||
| 327 | #endregion |
||
| 328 | |||
| 329 | #region Mult |
||
| 330 | |||
| 331 | /// <summary> |
||
| 332 | /// Multiply a vector and a scalar |
||
| 333 | /// </summary> |
||
| 334 | /// <param name="a">Vector operand</param> |
||
| 335 | /// <param name="d">Scalar operand</param> |
||
| 336 | /// <returns>Result of the multiplication</returns> |
||
| 337 | public static Vector2d Mult(Vector2d a, double d) |
||
| 338 | { |
||
| 339 | a.X *= d; |
||
| 340 | a.Y *= d; |
||
| 341 | return a; |
||
| 342 | } |
||
| 343 | |||
| 344 | /// <summary> |
||
| 345 | /// Multiply a vector and a scalar |
||
| 346 | /// </summary> |
||
| 347 | /// <param name="a">Vector operand</param> |
||
| 348 | /// <param name="d">Scalar operand</param> |
||
| 349 | /// <param name="result">Result of the multiplication</param> |
||
| 350 | public static void Mult(ref Vector2d a, double d, out Vector2d result) |
||
| 351 | { |
||
| 352 | result.X = a.X * d; |
||
| 353 | result.Y = a.Y * d; |
||
| 354 | } |
||
| 355 | |||
| 356 | #endregion |
||
| 357 | |||
| 358 | #region Div |
||
| 359 | |||
| 360 | /// <summary> |
||
| 361 | /// Divide a vector by a scalar |
||
| 362 | /// </summary> |
||
| 363 | /// <param name="a">Vector operand</param> |
||
| 364 | /// <param name="d">Scalar operand</param> |
||
| 365 | /// <returns>Result of the division</returns> |
||
| 366 | public static Vector2d Div(Vector2d a, double d) |
||
| 367 | { |
||
| 368 | double mult = 1.0 / d; |
||
| 369 | a.X *= mult; |
||
| 370 | a.Y *= mult; |
||
| 371 | return a; |
||
| 372 | } |
||
| 373 | |||
| 374 | /// <summary> |
||
| 375 | /// Divide a vector by a scalar |
||
| 376 | /// </summary> |
||
| 377 | /// <param name="a">Vector operand</param> |
||
| 378 | /// <param name="d">Scalar operand</param> |
||
| 379 | /// <param name="result">Result of the division</param> |
||
| 380 | public static void Div(ref Vector2d a, double d, out Vector2d result) |
||
| 381 | { |
||
| 382 | double mult = 1.0 / d; |
||
| 383 | result.X = a.X * mult; |
||
| 384 | result.Y = a.Y * mult; |
||
| 385 | } |
||
| 386 | |||
| 387 | #endregion |
||
| 388 | |||
| 389 | #region Min |
||
| 390 | |||
| 391 | /// <summary> |
||
| 392 | /// Calculate the component-wise minimum of two vectors |
||
| 393 | /// </summary> |
||
| 394 | /// <param name="a">First operand</param> |
||
| 395 | /// <param name="b">Second operand</param> |
||
| 396 | /// <returns>The component-wise minimum</returns> |
||
| 397 | public static Vector2d Min(Vector2d a, Vector2d b) |
||
| 398 | { |
||
| 399 | a.X = a.X < b.X ? a.X : b.X; |
||
| 400 | a.Y = a.Y < b.Y ? a.Y : b.Y; |
||
| 401 | return a; |
||
| 402 | } |
||
| 403 | |||
| 404 | /// <summary> |
||
| 405 | /// Calculate the component-wise minimum of two vectors |
||
| 406 | /// </summary> |
||
| 407 | /// <param name="a">First operand</param> |
||
| 408 | /// <param name="b">Second operand</param> |
||
| 409 | /// <param name="result">The component-wise minimum</param> |
||
| 410 | public static void Min(ref Vector2d a, ref Vector2d b, out Vector2d result) |
||
| 411 | { |
||
| 412 | result.X = a.X < b.X ? a.X : b.X; |
||
| 413 | result.Y = a.Y < b.Y ? a.Y : b.Y; |
||
| 414 | } |
||
| 415 | |||
| 416 | #endregion |
||
| 417 | |||
| 418 | #region Max |
||
| 419 | |||
| 420 | /// <summary> |
||
| 421 | /// Calculate the component-wise maximum of two vectors |
||
| 422 | /// </summary> |
||
| 423 | /// <param name="a">First operand</param> |
||
| 424 | /// <param name="b">Second operand</param> |
||
| 425 | /// <returns>The component-wise maximum</returns> |
||
| 426 | public static Vector2d Max(Vector2d a, Vector2d b) |
||
| 427 | { |
||
| 428 | a.X = a.X > b.X ? a.X : b.X; |
||
| 429 | a.Y = a.Y > b.Y ? a.Y : b.Y; |
||
| 430 | return a; |
||
| 431 | } |
||
| 432 | |||
| 433 | /// <summary> |
||
| 434 | /// Calculate the component-wise maximum of two vectors |
||
| 435 | /// </summary> |
||
| 436 | /// <param name="a">First operand</param> |
||
| 437 | /// <param name="b">Second operand</param> |
||
| 438 | /// <param name="result">The component-wise maximum</param> |
||
| 439 | public static void Max(ref Vector2d a, ref Vector2d b, out Vector2d result) |
||
| 440 | { |
||
| 441 | result.X = a.X > b.X ? a.X : b.X; |
||
| 442 | result.Y = a.Y > b.Y ? a.Y : b.Y; |
||
| 443 | } |
||
| 444 | |||
| 445 | #endregion |
||
| 446 | |||
| 447 | #region Clamp |
||
| 448 | |||
| 449 | /// <summary> |
||
| 450 | /// Clamp a vector to the given minimum and maximum vectors |
||
| 451 | /// </summary> |
||
| 452 | /// <param name="vec">Input vector</param> |
||
| 453 | /// <param name="min">Minimum vector</param> |
||
| 454 | /// <param name="max">Maximum vector</param> |
||
| 455 | /// <returns>The clamped vector</returns> |
||
| 456 | public static Vector2d Clamp(Vector2d vec, Vector2d min, Vector2d max) |
||
| 457 | { |
||
| 458 | vec.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X; |
||
| 459 | vec.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y; |
||
| 460 | return vec; |
||
| 461 | } |
||
| 462 | |||
| 463 | /// <summary> |
||
| 464 | /// Clamp a vector to the given minimum and maximum vectors |
||
| 465 | /// </summary> |
||
| 466 | /// <param name="vec">Input vector</param> |
||
| 467 | /// <param name="min">Minimum vector</param> |
||
| 468 | /// <param name="max">Maximum vector</param> |
||
| 469 | /// <param name="result">The clamped vector</param> |
||
| 470 | public static void Clamp(ref Vector2d vec, ref Vector2d min, ref Vector2d max, out Vector2d result) |
||
| 471 | { |
||
| 472 | result.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X; |
||
| 473 | result.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y; |
||
| 474 | } |
||
| 475 | |||
| 476 | #endregion |
||
| 477 | |||
| 478 | #region Normalize |
||
| 479 | |||
| 480 | /// <summary> |
||
| 481 | /// Scale a vector to unit length |
||
| 482 | /// </summary> |
||
| 483 | /// <param name="vec">The input vector</param> |
||
| 484 | /// <returns>The normalized vector</returns> |
||
| 485 | public static Vector2d Normalize(Vector2d vec) |
||
| 486 | { |
||
| 487 | double scale = 1.0f / vec.Length; |
||
| 488 | vec.X *= scale; |
||
| 489 | vec.Y *= scale; |
||
| 490 | return vec; |
||
| 491 | } |
||
| 492 | |||
| 493 | /// <summary> |
||
| 494 | /// Scale a vector to unit length |
||
| 495 | /// </summary> |
||
| 496 | /// <param name="vec">The input vector</param> |
||
| 497 | /// <param name="result">The normalized vector</param> |
||
| 498 | public static void Normalize(ref Vector2d vec, out Vector2d result) |
||
| 499 | { |
||
| 500 | double scale = 1.0f / vec.Length; |
||
| 501 | result.X = vec.X * scale; |
||
| 502 | result.Y = vec.Y * scale; |
||
| 503 | } |
||
| 504 | |||
| 505 | #endregion |
||
| 506 | |||
| 507 | #region NormalizeFast |
||
| 508 | |||
| 509 | /// <summary> |
||
| 510 | /// Scale a vector to approximately unit length |
||
| 511 | /// </summary> |
||
| 512 | /// <param name="vec">The input vector</param> |
||
| 513 | /// <returns>The normalized vector</returns> |
||
| 514 | public static Vector2d NormalizeFast(Vector2d vec) |
||
| 515 | { |
||
| 516 | double scale = Functions.InverseSqrtFast(vec.X * vec.X + vec.Y * vec.Y); |
||
| 517 | vec.X *= scale; |
||
| 518 | vec.Y *= scale; |
||
| 519 | return vec; |
||
| 520 | } |
||
| 521 | |||
| 522 | /// <summary> |
||
| 523 | /// Scale a vector to approximately unit length |
||
| 524 | /// </summary> |
||
| 525 | /// <param name="vec">The input vector</param> |
||
| 526 | /// <param name="result">The normalized vector</param> |
||
| 527 | public static void NormalizeFast(ref Vector2d vec, out Vector2d result) |
||
| 528 | { |
||
| 529 | double scale = Functions.InverseSqrtFast(vec.X * vec.X + vec.Y * vec.Y); |
||
| 530 | result.X = vec.X * scale; |
||
| 531 | result.Y = vec.Y * scale; |
||
| 532 | } |
||
| 533 | |||
| 534 | #endregion |
||
| 535 | |||
| 536 | #region Dot |
||
| 537 | |||
| 538 | /// <summary> |
||
| 539 | /// Calculate the dot (scalar) product of two vectors |
||
| 540 | /// </summary> |
||
| 541 | /// <param name="left">First operand</param> |
||
| 542 | /// <param name="right">Second operand</param> |
||
| 543 | /// <returns>The dot product of the two inputs</returns> |
||
| 544 | public static double Dot(Vector2d left, Vector2d right) |
||
| 545 | { |
||
| 546 | return left.X * right.X + left.Y * right.Y; |
||
| 547 | } |
||
| 548 | |||
| 549 | /// <summary> |
||
| 550 | /// Calculate the dot (scalar) product of two vectors |
||
| 551 | /// </summary> |
||
| 552 | /// <param name="left">First operand</param> |
||
| 553 | /// <param name="right">Second operand</param> |
||
| 554 | /// <param name="result">The dot product of the two inputs</param> |
||
| 555 | public static void Dot(ref Vector2d left, ref Vector2d right, out double result) |
||
| 556 | { |
||
| 557 | result = left.X * right.X + left.Y * right.Y; |
||
| 558 | } |
||
| 559 | |||
| 560 | #endregion |
||
| 561 | |||
| 562 | #region Lerp |
||
| 563 | |||
| 564 | /// <summary> |
||
| 565 | /// Returns a new Vector that is the linear blend of the 2 given Vectors |
||
| 566 | /// </summary> |
||
| 567 | /// <param name="a">First input vector</param> |
||
| 568 | /// <param name="b">Second input vector</param> |
||
| 569 | /// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param> |
||
| 570 | /// <returns>a when blend=0, b when blend=1, and a linear combination otherwise</returns> |
||
| 571 | public static Vector2d Lerp(Vector2d a, Vector2d b, double blend) |
||
| 572 | { |
||
| 573 | a.X = blend * (b.X - a.X) + a.X; |
||
| 574 | a.Y = blend * (b.Y - a.Y) + a.Y; |
||
| 575 | return a; |
||
| 576 | } |
||
| 577 | |||
| 578 | /// <summary> |
||
| 579 | /// Returns a new Vector that is the linear blend of the 2 given Vectors |
||
| 580 | /// </summary> |
||
| 581 | /// <param name="a">First input vector</param> |
||
| 582 | /// <param name="b">Second input vector</param> |
||
| 583 | /// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param> |
||
| 584 | /// <param name="result">a when blend=0, b when blend=1, and a linear combination otherwise</param> |
||
| 585 | public static void Lerp(ref Vector2d a, ref Vector2d b, double blend, out Vector2d result) |
||
| 586 | { |
||
| 587 | result.X = blend * (b.X - a.X) + a.X; |
||
| 588 | result.Y = blend * (b.Y - a.Y) + a.Y; |
||
| 589 | } |
||
| 590 | |||
| 591 | #endregion |
||
| 592 | |||
| 593 | #region Barycentric |
||
| 594 | |||
| 595 | /// <summary> |
||
| 596 | /// Interpolate 3 Vectors using Barycentric coordinates |
||
| 597 | /// </summary> |
||
| 598 | /// <param name="a">First input Vector</param> |
||
| 599 | /// <param name="b">Second input Vector</param> |
||
| 600 | /// <param name="c">Third input Vector</param> |
||
| 601 | /// <param name="u">First Barycentric Coordinate</param> |
||
| 602 | /// <param name="v">Second Barycentric Coordinate</param> |
||
| 603 | /// <returns>a when u=v=0, b when u=1,v=0, c when u=0,v=1, and a linear combination of a,b,c otherwise</returns> |
||
| 604 | public static Vector2d BaryCentric(Vector2d a, Vector2d b, Vector2d c, double u, double v) |
||
| 605 | { |
||
| 606 | return a + u * (b - a) + v * (c - a); |
||
| 607 | } |
||
| 608 | |||
| 609 | /// <summary>Interpolate 3 Vectors using Barycentric coordinates</summary> |
||
| 610 | /// <param name="a">First input Vector.</param> |
||
| 611 | /// <param name="b">Second input Vector.</param> |
||
| 612 | /// <param name="c">Third input Vector.</param> |
||
| 613 | /// <param name="u">First Barycentric Coordinate.</param> |
||
| 614 | /// <param name="v">Second Barycentric Coordinate.</param> |
||
| 615 | /// <param name="result">Output Vector. a when u=v=0, b when u=1,v=0, c when u=0,v=1, and a linear combination of a,b,c otherwise</param> |
||
| 616 | public static void BaryCentric(ref Vector2d a, ref Vector2d b, ref Vector2d c, float u, float v, out Vector2d result) |
||
| 617 | { |
||
| 618 | result = a; // copy |
||
| 619 | |||
| 620 | Vector2d temp = b; // copy |
||
| 621 | temp.Sub(ref a); |
||
| 622 | temp.Mult(u); |
||
| 623 | result.Add(ref temp); |
||
| 624 | |||
| 625 | temp = c; // copy |
||
| 626 | temp.Sub(ref a); |
||
| 627 | temp.Mult(v); |
||
| 628 | result.Add(ref temp); |
||
| 629 | } |
||
| 630 | |||
| 631 | #endregion |
||
| 632 | |||
| 633 | #endregion |
||
| 634 | |||
| 635 | #region Operators |
||
| 636 | |||
| 637 | /// <summary> |
||
| 638 | /// Adds two instances. |
||
| 639 | /// </summary> |
||
| 640 | /// <param name="left">The left instance.</param> |
||
| 641 | /// <param name="right">The right instance.</param> |
||
| 642 | /// <returns>The result of the operation.</returns> |
||
| 643 | public static Vector2d operator +(Vector2d left, Vector2d right) |
||
| 644 | { |
||
| 645 | left.X += right.X; |
||
| 646 | left.Y += right.Y; |
||
| 647 | return left; |
||
| 648 | } |
||
| 649 | |||
| 650 | /// <summary> |
||
| 651 | /// Subtracts two instances. |
||
| 652 | /// </summary> |
||
| 653 | /// <param name="left">The left instance.</param> |
||
| 654 | /// <param name="right">The right instance.</param> |
||
| 655 | /// <returns>The result of the operation.</returns> |
||
| 656 | public static Vector2d operator -(Vector2d left, Vector2d right) |
||
| 657 | { |
||
| 658 | left.X -= right.X; |
||
| 659 | left.Y -= right.Y; |
||
| 660 | return left; |
||
| 661 | } |
||
| 662 | |||
| 663 | /// <summary> |
||
| 664 | /// Negates an instance. |
||
| 665 | /// </summary> |
||
| 666 | /// <param name="vec">The instance.</param> |
||
| 667 | /// <returns>The result of the operation.</returns> |
||
| 668 | public static Vector2d operator -(Vector2d vec) |
||
| 669 | { |
||
| 670 | vec.X = -vec.X; |
||
| 671 | vec.Y = -vec.Y; |
||
| 672 | return vec; |
||
| 673 | } |
||
| 674 | |||
| 675 | /// <summary> |
||
| 676 | /// Multiplies an instance by a scalar. |
||
| 677 | /// </summary> |
||
| 678 | /// <param name="vec">The instance.</param> |
||
| 679 | /// <param name="f">The scalar.</param> |
||
| 680 | /// <returns>The result of the operation.</returns> |
||
| 681 | public static Vector2d operator *(Vector2d vec, double f) |
||
| 682 | { |
||
| 683 | vec.X *= f; |
||
| 684 | vec.Y *= f; |
||
| 685 | return vec; |
||
| 686 | } |
||
| 687 | |||
| 688 | /// <summary> |
||
| 689 | /// Multiply an instance by a scalar. |
||
| 690 | /// </summary> |
||
| 691 | /// <param name="f">The scalar.</param> |
||
| 692 | /// <param name="vec">The instance.</param> |
||
| 693 | /// <returns>The result of the operation.</returns> |
||
| 694 | public static Vector2d operator *(double f, Vector2d vec) |
||
| 695 | { |
||
| 696 | vec.X *= f; |
||
| 697 | vec.Y *= f; |
||
| 698 | return vec; |
||
| 699 | } |
||
| 700 | |||
| 701 | /// <summary> |
||
| 702 | /// Divides an instance by a scalar. |
||
| 703 | /// </summary> |
||
| 704 | /// <param name="vec">The instance.</param> |
||
| 705 | /// <param name="f">The scalar.</param> |
||
| 706 | /// <returns>The result of the operation.</returns> |
||
| 707 | public static Vector2d operator /(Vector2d vec, double f) |
||
| 708 | { |
||
| 709 | double mult = 1.0f / f; |
||
| 710 | vec.X *= mult; |
||
| 711 | vec.Y *= mult; |
||
| 712 | return vec; |
||
| 713 | } |
||
| 714 | |||
| 715 | /// <summary> |
||
| 716 | /// Compares two instances for equality. |
||
| 717 | /// </summary> |
||
| 718 | /// <param name="left">The left instance.</param> |
||
| 719 | /// <param name="right">The right instance.</param> |
||
| 720 | /// <returns>True, if both instances are equal; false otherwise.</returns> |
||
| 721 | public static bool operator ==(Vector2d left, Vector2d right) |
||
| 722 | { |
||
| 723 | return left.Equals(right); |
||
| 724 | } |
||
| 725 | |||
| 726 | /// <summary> |
||
| 727 | /// Compares two instances for ienquality. |
||
| 728 | /// </summary> |
||
| 729 | /// <param name="left">The left instance.</param> |
||
| 730 | /// <param name="right">The right instance.</param> |
||
| 731 | /// <returns>True, if the instances are not equal; false otherwise.</returns> |
||
| 732 | public static bool operator !=(Vector2d left, Vector2d right) |
||
| 733 | { |
||
| 734 | return !left.Equals(right); |
||
| 735 | } |
||
| 736 | |||
| 737 | /// <summary>Converts OpenTK.Vector2 to OpenTK.Vector2d.</summary> |
||
| 738 | /// <param name="v2">The Vector2 to convert.</param> |
||
| 739 | /// <returns>The resulting Vector2d.</returns> |
||
| 740 | public static explicit operator Vector2d(Vector2 v2) |
||
| 741 | { |
||
| 742 | return new Vector2d(v2.X, v2.Y); |
||
| 743 | } |
||
| 744 | |||
| 745 | /// <summary>Converts OpenTK.Vector2d to OpenTK.Vector2.</summary> |
||
| 746 | /// <param name="v2d">The Vector2d to convert.</param> |
||
| 747 | /// <returns>The resulting Vector2.</returns> |
||
| 748 | public static explicit operator Vector2(Vector2d v2d) |
||
| 749 | { |
||
| 750 | return new Vector2((float)v2d.X, (float)v2d.Y); |
||
| 751 | } |
||
| 752 | |||
| 753 | #endregion |
||
| 754 | |||
| 755 | #region Overrides |
||
| 756 | |||
| 757 | #region public override string ToString() |
||
| 758 | |||
| 759 | /// <summary> |
||
| 760 | /// Returns a System.String that represents the current instance. |
||
| 761 | /// </summary> |
||
| 762 | /// <returns></returns> |
||
| 763 | public override string ToString() |
||
| 764 | { |
||
| 765 | return String.Format("({0}, {1})", X, Y); |
||
| 766 | } |
||
| 767 | |||
| 768 | #endregion |
||
| 769 | |||
| 770 | #region public override int GetHashCode() |
||
| 771 | |||
| 772 | /// <summary> |
||
| 773 | /// Returns the hashcode for this instance. |
||
| 774 | /// </summary> |
||
| 775 | /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns> |
||
| 776 | public override int GetHashCode() |
||
| 777 | { |
||
| 778 | return X.GetHashCode() ^ Y.GetHashCode(); |
||
| 779 | } |
||
| 780 | |||
| 781 | #endregion |
||
| 782 | |||
| 783 | #region public override bool Equals(object obj) |
||
| 784 | |||
| 785 | /// <summary> |
||
| 786 | /// Indicates whether this instance and a specified object are equal. |
||
| 787 | /// </summary> |
||
| 788 | /// <param name="obj">The object to compare to.</param> |
||
| 789 | /// <returns>True if the instances are equal; false otherwise.</returns> |
||
| 790 | public override bool Equals(object obj) |
||
| 791 | { |
||
| 792 | if (!(obj is Vector2d)) |
||
| 793 | return false; |
||
| 794 | |||
| 795 | return this.Equals((Vector2d)obj); |
||
| 796 | } |
||
| 797 | |||
| 798 | #endregion |
||
| 799 | |||
| 800 | #endregion |
||
| 801 | |||
| 802 | #endregion |
||
| 803 | |||
| 804 | #region IEquatable<Vector2d> Members |
||
| 805 | |||
| 806 | /// <summary>Indicates whether the current vector is equal to another vector.</summary> |
||
| 807 | /// <param name="other">A vector to compare with this vector.</param> |
||
| 808 | /// <returns>true if the current vector is equal to the vector parameter; otherwise, false.</returns> |
||
| 809 | public bool Equals(Vector2d other) |
||
| 810 | { |
||
| 811 | return |
||
| 812 | X == other.X && |
||
| 813 | Y == other.Y; |
||
| 814 | } |
||
| 815 | |||
| 816 | #endregion |
||
| 817 | } |
||
| 818 | } |