Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Int2.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under MIT License. See LICENSE.md for details.
3 //
4 // -----------------------------------------------------------------------------
5 // Original code from SlimMath project. http://code.google.com/p/slimmath/
6 // Greetings to SlimDX Group. Original code published with the following license:
7 // -----------------------------------------------------------------------------
8 /*
9 * Copyright (c) 2007-2011 SlimDX Group
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 */
29 using System;
30 using System.Globalization;
31 using System.Runtime.InteropServices;
32 
33 namespace SiliconStudio.Core.Mathematics
34 {
35  /// <summary>
36  /// Represents a three dimensional mathematical vector.
37  /// </summary>
38  [DataContract("Int2")]
39  [DataStyle(DataStyle.Compact)]
40  [StructLayout(LayoutKind.Sequential, Pack = 4)]
41  public struct Int2 : IEquatable<Int2>, IFormattable
42  {
43  /// <summary>
44  /// The size of the <see cref="SiliconStudio.Core.Mathematics.Int2"/> type, in bytes.
45  /// </summary>
46  public static readonly int SizeInBytes = Marshal.SizeOf(typeof(Int2));
47 
48  /// <summary>
49  /// A <see cref="SiliconStudio.Core.Mathematics.Int2"/> with all of its components set to zero.
50  /// </summary>
51  public static readonly Int2 Zero = new Int2();
52 
53  /// <summary>
54  /// The X unit <see cref="SiliconStudio.Core.Mathematics.Int2"/> (1, 0, 0).
55  /// </summary>
56  public static readonly Int2 UnitX = new Int2(1, 0);
57 
58  /// <summary>
59  /// The Y unit <see cref="SiliconStudio.Core.Mathematics.Int2"/> (0, 1, 0).
60  /// </summary>
61  public static readonly Int2 UnitY = new Int2(0, 1);
62 
63  /// <summary>
64  /// A <see cref="SiliconStudio.Core.Mathematics.Int2"/> with all of its components set to one.
65  /// </summary>
66  public static readonly Int2 One = new Int2(1, 1);
67 
68  /// <summary>
69  /// The X component of the vector.
70  /// </summary>
71  [DataMember(0)]
72  public int X;
73 
74  /// <summary>
75  /// The Y component of the vector.
76  /// </summary>
77  [DataMember(1)]
78  public int Y;
79 
80  /// <summary>
81  /// Initializes a new instance of the <see cref="SiliconStudio.Core.Mathematics.Int2"/> struct.
82  /// </summary>
83  /// <param name="value">The value that will be assigned to all components.</param>
84  public Int2(int value)
85  {
86  X = value;
87  Y = value;
88  }
89 
90  /// <summary>
91  /// Initializes a new instance of the <see cref="SiliconStudio.Core.Mathematics.Int2"/> struct.
92  /// </summary>
93  /// <param name="x">Initial value for the X component of the vector.</param>
94  /// <param name="y">Initial value for the Y component of the vector.</param>
95  public Int2(int x, int y)
96  {
97  X = x;
98  Y = y;
99  }
100 
101  /// <summary>
102  /// Initializes a new instance of the <see cref="SiliconStudio.Core.Mathematics.Int2"/> struct.
103  /// </summary>
104  /// <param name="value">A vector containing the values with which to initialize the X and Y components.</param>
105  public Int2(Vector2 value)
106  {
107  X = (int)value.X;
108  Y = (int)value.Y;
109  }
110 
111  /// <summary>
112  /// Initializes a new instance of the <see cref="SiliconStudio.Core.Mathematics.Int2"/> struct.
113  /// </summary>
114  /// <param name="values">The values to assign to the X, Y, and Z components of the vector. This must be an array with three elements.</param>
115  /// <exception cref="ArgumentNullException">Thrown when <paramref name="values"/> is <c>null</c>.</exception>
116  /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="values"/> contains more or less than three elements.</exception>
117  public Int2(int[] values)
118  {
119  if (values == null)
120  throw new ArgumentNullException("values");
121  if (values.Length != 2)
122  throw new ArgumentOutOfRangeException("values", "There must be two and only two input values for Int2.");
123 
124  X = values[0];
125  Y = values[1];
126  }
127 
128  /// <summary>
129  /// Gets a value indicting whether this instance is normalized.
130  /// </summary>
131  public bool IsNormalized
132  {
133  get { return Math.Abs((X * X) + (Y * Y) - 1f) < MathUtil.ZeroTolerance; }
134  }
135 
136  /// <summary>
137  /// Gets or sets the component at the specified index.
138  /// </summary>
139  /// <value>The value of the X or Y component, depending on the index.</value>
140  /// <param name="index">The index of the component to access. Use 0 for the X component and 1 for the Y component.</param>
141  /// <returns>The value of the component at the specified index.</returns>
142  /// <exception cref="System.ArgumentOutOfRangeException">Thrown when the <paramref name="index"/> is out of the range [0, 1].</exception>
143  public int this[int index]
144  {
145  get
146  {
147  switch (index)
148  {
149  case 0: return X;
150  case 1: return Y;
151  }
152 
153  throw new ArgumentOutOfRangeException("index", "Indices for Int2 run from 0 to 1, inclusive.");
154  }
155 
156  set
157  {
158  switch (index)
159  {
160  case 0: X = value; break;
161  case 1: Y = value; break;
162  default: throw new ArgumentOutOfRangeException("index", "Indices for Int2 run from 0 to 1, inclusive.");
163  }
164  }
165  }
166 
167  /// <summary>
168  /// Calculates the length of the vector.
169  /// </summary>
170  /// <returns>The length of the vector.</returns>
171  /// <remarks>
172  /// <see cref="SiliconStudio.Core.Mathematics.Int2.LengthSquared"/> may be preferred when only the relative length is needed
173  /// and speed is of the essence.
174  /// </remarks>
175  public int Length()
176  {
177  return (int)Math.Sqrt((X * X) + (Y * Y));
178  }
179 
180  /// <summary>
181  /// Calculates the squared length of the vector.
182  /// </summary>
183  /// <returns>The squared length of the vector.</returns>
184  /// <remarks>
185  /// This method may be preferred to <see cref="SiliconStudio.Core.Mathematics.Int2.Length"/> when only a relative length is needed
186  /// and speed is of the essence.
187  /// </remarks>
188  public int LengthSquared()
189  {
190  return (X * X) + (Y * Y);
191  }
192 
193  /// <summary>
194  /// Converts the vector into a unit vector.
195  /// </summary>
196  public void Normalize()
197  {
198  int length = Length();
199  if (length > MathUtil.ZeroTolerance)
200  {
201  int inv = 1 / length;
202  X *= inv;
203  Y *= inv;
204  }
205  }
206 
207  /// <summary>
208  /// Raises the exponent for each components.
209  /// </summary>
210  /// <param name="exponent">The exponent.</param>
211  public void Pow(int exponent)
212  {
213  X = (int)Math.Pow(X, exponent);
214  Y = (int)Math.Pow(Y, exponent);
215  }
216 
217  /// <summary>
218  /// Creates an array containing the elements of the vector.
219  /// </summary>
220  /// <returns>A two-element array containing the components of the vector.</returns>
221  public int[] ToArray()
222  {
223  return new int[] { X, Y };
224  }
225 
226  /// <summary>
227  /// Adds two vectors.
228  /// </summary>
229  /// <param name="left">The first vector to add.</param>
230  /// <param name="right">The second vector to add.</param>
231  /// <param name="result">When the method completes, contains the sum of the two vectors.</param>
232  public static void Add(ref Int2 left, ref Int2 right, out Int2 result)
233  {
234  result = new Int2(left.X + right.X, left.Y + right.Y);
235  }
236 
237  /// <summary>
238  /// Adds two vectors.
239  /// </summary>
240  /// <param name="left">The first vector to add.</param>
241  /// <param name="right">The second vector to add.</param>
242  /// <returns>The sum of the two vectors.</returns>
243  public static Int2 Add(Int2 left, Int2 right)
244  {
245  return new Int2(left.X + right.X, left.Y + right.Y);
246  }
247 
248  /// <summary>
249  /// Subtracts two vectors.
250  /// </summary>
251  /// <param name="left">The first vector to subtract.</param>
252  /// <param name="right">The second vector to subtract.</param>
253  /// <param name="result">When the method completes, contains the difference of the two vectors.</param>
254  public static void Subtract(ref Int2 left, ref Int2 right, out Int2 result)
255  {
256  result = new Int2(left.X - right.X, left.Y - right.Y);
257  }
258 
259  /// <summary>
260  /// Subtracts two vectors.
261  /// </summary>
262  /// <param name="left">The first vector to subtract.</param>
263  /// <param name="right">The second vector to subtract.</param>
264  /// <returns>The difference of the two vectors.</returns>
265  public static Int2 Subtract(Int2 left, Int2 right)
266  {
267  return new Int2(left.X - right.X, left.Y - right.Y);
268  }
269 
270  /// <summary>
271  /// Scales a vector by the given value.
272  /// </summary>
273  /// <param name="value">The vector to scale.</param>
274  /// <param name="scale">The amount by which to scale the vector.</param>
275  /// <param name="result">When the method completes, contains the scaled vector.</param>
276  public static void Multiply(ref Int2 value, int scale, out Int2 result)
277  {
278  result = new Int2(value.X * scale, value.Y * scale);
279  }
280 
281  /// <summary>
282  /// Scales a vector by the given value.
283  /// </summary>
284  /// <param name="value">The vector to scale.</param>
285  /// <param name="scale">The amount by which to scale the vector.</param>
286  /// <returns>The scaled vector.</returns>
287  public static Int2 Multiply(Int2 value, int scale)
288  {
289  return new Int2(value.X * scale, value.Y * scale);
290  }
291 
292  /// <summary>
293  /// Modulates a vector with another by performing component-wise multiplication.
294  /// </summary>
295  /// <param name="left">The first vector to modulate.</param>
296  /// <param name="right">The second vector to modulate.</param>
297  /// <param name="result">When the method completes, contains the modulated vector.</param>
298  public static void Modulate(ref Int2 left, ref Int2 right, out Int2 result)
299  {
300  result = new Int2(left.X * right.X, left.Y * right.Y);
301  }
302 
303  /// <summary>
304  /// Modulates a vector with another by performing component-wise multiplication.
305  /// </summary>
306  /// <param name="left">The first vector to modulate.</param>
307  /// <param name="right">The second vector to modulate.</param>
308  /// <returns>The modulated vector.</returns>
309  public static Int2 Modulate(Int2 left, Int2 right)
310  {
311  return new Int2(left.X * right.X, left.Y * right.Y);
312  }
313 
314  /// <summary>
315  /// Scales a vector by the given value.
316  /// </summary>
317  /// <param name="value">The vector to scale.</param>
318  /// <param name="scale">The amount by which to scale the vector.</param>
319  /// <param name="result">When the method completes, contains the scaled vector.</param>
320  public static void Divide(ref Int2 value, int scale, out Int2 result)
321  {
322  result = new Int2(value.X / scale, value.Y / scale);
323  }
324 
325  /// <summary>
326  /// Scales a vector by the given value.
327  /// </summary>
328  /// <param name="value">The vector to scale.</param>
329  /// <param name="scale">The amount by which to scale the vector.</param>
330  /// <returns>The scaled vector.</returns>
331  public static Int2 Divide(Int2 value, int scale)
332  {
333  return new Int2(value.X / scale, value.Y / scale);
334  }
335 
336  /// <summary>
337  /// Reverses the direction of a given vector.
338  /// </summary>
339  /// <param name="value">The vector to negate.</param>
340  /// <param name="result">When the method completes, contains a vector facing in the opposite direction.</param>
341  public static void Negate(ref Int2 value, out Int2 result)
342  {
343  result = new Int2(-value.X, -value.Y);
344  }
345 
346  /// <summary>
347  /// Reverses the direction of a given vector.
348  /// </summary>
349  /// <param name="value">The vector to negate.</param>
350  /// <returns>A vector facing in the opposite direction.</returns>
351  public static Int2 Negate(Int2 value)
352  {
353  return new Int2(-value.X, -value.Y);
354  }
355 
356  /// <summary>
357  /// Restricts a value to be within a specified range.
358  /// </summary>
359  /// <param name="value">The value to clamp.</param>
360  /// <param name="min">The minimum value.</param>
361  /// <param name="max">The maximum value.</param>
362  /// <param name="result">When the method completes, contains the clamped value.</param>
363  public static void Clamp(ref Int2 value, ref Int2 min, ref Int2 max, out Int2 result)
364  {
365  int x = value.X;
366  x = (x > max.X) ? max.X : x;
367  x = (x < min.X) ? min.X : x;
368 
369  int y = value.Y;
370  y = (y > max.Y) ? max.Y : y;
371  y = (y < min.Y) ? min.Y : y;
372 
373  result = new Int2(x, y);
374  }
375 
376  /// <summary>
377  /// Restricts a value to be within a specified range.
378  /// </summary>
379  /// <param name="value">The value to clamp.</param>
380  /// <param name="min">The minimum value.</param>
381  /// <param name="max">The maximum value.</param>
382  /// <returns>The clamped value.</returns>
383  public static Int2 Clamp(Int2 value, Int2 min, Int2 max)
384  {
385  Int2 result;
386  Clamp(ref value, ref min, ref max, out result);
387  return result;
388  }
389 
390  /// <summary>
391  /// Calculates the dot product of two vectors.
392  /// </summary>
393  /// <param name="left">First source vector.</param>
394  /// <param name="right">Second source vector.</param>
395  /// <param name="result">When the method completes, contains the dot product of the two vectors.</param>
396  public static void Dot(ref Int2 left, ref Int2 right, out int result)
397  {
398  result = (left.X * right.X) + (left.Y * right.Y);
399  }
400 
401  /// <summary>
402  /// Calculates the dot product of two vectors.
403  /// </summary>
404  /// <param name="left">First source vector.</param>
405  /// <param name="right">Second source vector.</param>
406  /// <returns>The dot product of the two vectors.</returns>
407  public static int Dot(Int2 left, Int2 right)
408  {
409  return (left.X * right.X) + (left.Y * right.Y);
410  }
411 
412  /// <summary>
413  /// Converts the vector into a unit vector.
414  /// </summary>
415  /// <param name="value">The vector to normalize.</param>
416  /// <param name="result">When the method completes, contains the normalized vector.</param>
417  public static void Normalize(ref Int2 value, out Int2 result)
418  {
419  result = value;
420  result.Normalize();
421  }
422 
423  /// <summary>
424  /// Converts the vector into a unit vector.
425  /// </summary>
426  /// <param name="value">The vector to normalize.</param>
427  /// <returns>The normalized vector.</returns>
428  public static Int2 Normalize(Int2 value)
429  {
430  value.Normalize();
431  return value;
432  }
433 
434  /// <summary>
435  /// Performs a linear interpolation between two vectors.
436  /// </summary>
437  /// <param name="start">Start vector.</param>
438  /// <param name="end">End vector.</param>
439  /// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end"/>.</param>
440  /// <param name="result">When the method completes, contains the linear interpolation of the two vectors.</param>
441  /// <remarks>
442  /// This method performs the linear interpolation based on the following formula.
443  /// <code>start + (end - start) * amount</code>
444  /// Passing <paramref name="amount"/> a value of 0 will cause <paramref name="start"/> to be returned; a value of 1 will cause <paramref name="end"/> to be returned.
445  /// </remarks>
446  public static void Lerp(ref Int2 start, ref Int2 end, float amount, out Int2 result)
447  {
448  result.X = (int)(start.X + ((end.X - start.X) * amount));
449  result.Y = (int)(start.Y + ((end.Y - start.Y) * amount));
450  }
451 
452  /// <summary>
453  /// Performs a linear interpolation between two vectors.
454  /// </summary>
455  /// <param name="start">Start vector.</param>
456  /// <param name="end">End vector.</param>
457  /// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end"/>.</param>
458  /// <returns>The linear interpolation of the two vectors.</returns>
459  /// <remarks>
460  /// This method performs the linear interpolation based on the following formula.
461  /// <code>start + (end - start) * amount</code>
462  /// Passing <paramref name="amount"/> a value of 0 will cause <paramref name="start"/> to be returned; a value of 1 will cause <paramref name="end"/> to be returned.
463  /// </remarks>
464  public static Int2 Lerp(Int2 start, Int2 end, float amount)
465  {
466  Int2 result;
467  Lerp(ref start, ref end, amount, out result);
468  return result;
469  }
470 
471  /// <summary>
472  /// Performs a cubic interpolation between two vectors.
473  /// </summary>
474  /// <param name="start">Start vector.</param>
475  /// <param name="end">End vector.</param>
476  /// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end"/>.</param>
477  /// <param name="result">When the method completes, contains the cubic interpolation of the two vectors.</param>
478  public static void SmoothStep(ref Int2 start, ref Int2 end, float amount, out Int2 result)
479  {
480  amount = (amount > 1) ? 1 : ((amount < 0) ? 0 : amount);
481  amount = (amount * amount) * (3 - (2 * amount));
482 
483  result.X = (int)(start.X + ((end.X - start.X) * amount));
484  result.Y = (int)(start.Y + ((end.Y - start.Y) * amount));
485  }
486 
487  /// <summary>
488  /// Performs a cubic interpolation between two vectors.
489  /// </summary>
490  /// <param name="start">Start vector.</param>
491  /// <param name="end">End vector.</param>
492  /// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end"/>.</param>
493  /// <returns>The cubic interpolation of the two vectors.</returns>
494  public static Int2 SmoothStep(Int2 start, Int2 end, float amount)
495  {
496  Int2 result;
497  SmoothStep(ref start, ref end, amount, out result);
498  return result;
499  }
500 
501  /// <summary>
502  /// Returns a vector containing the smallest components of the specified vectors.
503  /// </summary>
504  /// <param name="left">The first source vector.</param>
505  /// <param name="right">The second source vector.</param>
506  /// <param name="result">When the method completes, contains an new vector composed of the largest components of the source vectors.</param>
507  public static void Max(ref Int2 left, ref Int2 right, out Int2 result)
508  {
509  result.X = (left.X > right.X) ? left.X : right.X;
510  result.Y = (left.Y > right.Y) ? left.Y : right.Y;
511  }
512 
513  /// <summary>
514  /// Returns a vector containing the largest components of the specified vectors.
515  /// </summary>
516  /// <param name="left">The first source vector.</param>
517  /// <param name="right">The second source vector.</param>
518  /// <returns>A vector containing the largest components of the source vectors.</returns>
519  public static Int2 Max(Int2 left, Int2 right)
520  {
521  Int2 result;
522  Max(ref left, ref right, out result);
523  return result;
524  }
525 
526  /// <summary>
527  /// Returns a vector containing the smallest components of the specified vectors.
528  /// </summary>
529  /// <param name="left">The first source vector.</param>
530  /// <param name="right">The second source vector.</param>
531  /// <param name="result">When the method completes, contains an new vector composed of the smallest components of the source vectors.</param>
532  public static void Min(ref Int2 left, ref Int2 right, out Int2 result)
533  {
534  result.X = (left.X < right.X) ? left.X : right.X;
535  result.Y = (left.Y < right.Y) ? left.Y : right.Y;
536  }
537 
538  /// <summary>
539  /// Returns a vector containing the smallest components of the specified vectors.
540  /// </summary>
541  /// <param name="left">The first source vector.</param>
542  /// <param name="right">The second source vector.</param>
543  /// <returns>A vector containing the smallest components of the source vectors.</returns>
544  public static Int2 Min(Int2 left, Int2 right)
545  {
546  Int2 result;
547  Min(ref left, ref right, out result);
548  return result;
549  }
550 
551  /// <summary>
552  /// Adds two vectors.
553  /// </summary>
554  /// <param name="left">The first vector to add.</param>
555  /// <param name="right">The second vector to add.</param>
556  /// <returns>The sum of the two vectors.</returns>
557  public static Int2 operator +(Int2 left, Int2 right)
558  {
559  return new Int2(left.X + right.X, left.Y + right.Y);
560  }
561 
562  /// <summary>
563  /// Assert a vector (return it unchanged).
564  /// </summary>
565  /// <param name="value">The vector to assert (unchange).</param>
566  /// <returns>The asserted (unchanged) vector.</returns>
567  public static Int2 operator +(Int2 value)
568  {
569  return value;
570  }
571 
572  /// <summary>
573  /// Subtracts two vectors.
574  /// </summary>
575  /// <param name="left">The first vector to subtract.</param>
576  /// <param name="right">The second vector to subtract.</param>
577  /// <returns>The difference of the two vectors.</returns>
578  public static Int2 operator -(Int2 left, Int2 right)
579  {
580  return new Int2(left.X - right.X, left.Y - right.Y);
581  }
582 
583  /// <summary>
584  /// Reverses the direction of a given vector.
585  /// </summary>
586  /// <param name="value">The vector to negate.</param>
587  /// <returns>A vector facing in the opposite direction.</returns>
588  public static Int2 operator -(Int2 value)
589  {
590  return new Int2(-value.X, -value.Y);
591  }
592 
593  /// <summary>
594  /// Scales a vector by the given value.
595  /// </summary>
596  /// <param name="value">The vector to scale.</param>
597  /// <param name="scale">The amount by which to scale the vector.</param>
598  /// <returns>The scaled vector.</returns>
599  public static Int2 operator *(float scale, Int2 value)
600  {
601  return new Int2((int)(value.X * scale), (int)(value.Y * scale));
602  }
603 
604  /// <summary>
605  /// Scales a vector by the given value.
606  /// </summary>
607  /// <param name="value">The vector to scale.</param>
608  /// <param name="scale">The amount by which to scale the vector.</param>
609  /// <returns>The scaled vector.</returns>
610  public static Int2 operator *(Int2 value, float scale)
611  {
612  return new Int2((int)(value.X * scale), (int)(value.Y * scale));
613  }
614 
615  /// <summary>
616  /// Scales a vector by the given value.
617  /// </summary>
618  /// <param name="value">The vector to scale.</param>
619  /// <param name="scale">The amount by which to scale the vector.</param>
620  /// <returns>The scaled vector.</returns>
621  public static Int2 operator /(Int2 value, float scale)
622  {
623  return new Int2((int)(value.X / scale), (int)(value.Y / scale));
624  }
625 
626  /// <summary>
627  /// Tests for equality between two objects.
628  /// </summary>
629  /// <param name="left">The first value to compare.</param>
630  /// <param name="right">The second value to compare.</param>
631  /// <returns><c>true</c> if <paramref name="left"/> has the same value as <paramref name="right"/>; otherwise, <c>false</c>.</returns>
632  public static bool operator ==(Int2 left, Int2 right)
633  {
634  return left.Equals(right);
635  }
636 
637  /// <summary>
638  /// Tests for inequality between two objects.
639  /// </summary>
640  /// <param name="left">The first value to compare.</param>
641  /// <param name="right">The second value to compare.</param>
642  /// <returns><c>true</c> if <paramref name="left"/> has a different value than <paramref name="right"/>; otherwise, <c>false</c>.</returns>
643  public static bool operator !=(Int2 left, Int2 right)
644  {
645  return !left.Equals(right);
646  }
647 
648  /// <summary>
649  /// Performs an explicit conversion from <see cref="SiliconStudio.Core.Mathematics.Int2"/> to <see cref="Vector2"/>.
650  /// </summary>
651  /// <param name="value">The value.</param>
652  /// <returns>The result of the conversion.</returns>
653  public static explicit operator Vector2(Int2 value)
654  {
655  return new Vector2(value.X, value.Y);
656  }
657 
658  /// <summary>
659  /// Performs an explicit conversion from <see cref="SiliconStudio.Core.Mathematics.Int2"/> to <see cref="Vector4"/>.
660  /// </summary>
661  /// <param name="value">The value.</param>
662  /// <returns>The result of the conversion.</returns>
663  public static explicit operator Vector4(Int2 value)
664  {
665  return new Vector4(value.X, value.Y, 0, 0);
666  }
667 
668  /// <summary>
669  /// Returns a <see cref="System.String"/> that represents this instance.
670  /// </summary>
671  /// <returns>
672  /// A <see cref="System.String"/> that represents this instance.
673  /// </returns>
674  public override string ToString()
675  {
676  return string.Format(CultureInfo.CurrentCulture, "X:{0} Y:{1}", X, Y);
677  }
678 
679  /// <summary>
680  /// Returns a <see cref="System.String"/> that represents this instance.
681  /// </summary>
682  /// <param name="format">The format.</param>
683  /// <returns>
684  /// A <see cref="System.String"/> that represents this instance.
685  /// </returns>
686  public string ToString(string format)
687  {
688  if (format == null)
689  return ToString();
690 
691  return string.Format(CultureInfo.CurrentCulture, "X:{0} Y:{1}", X.ToString(format, CultureInfo.CurrentCulture), Y.ToString(format, CultureInfo.CurrentCulture));
692  }
693 
694  /// <summary>
695  /// Returns a <see cref="System.String"/> that represents this instance.
696  /// </summary>
697  /// <param name="formatProvider">The format provider.</param>
698  /// <returns>
699  /// A <see cref="System.String"/> that represents this instance.
700  /// </returns>
701  public string ToString(IFormatProvider formatProvider)
702  {
703  return string.Format(formatProvider, "X:{0} Y:{1}", X, Y);
704  }
705 
706  /// <summary>
707  /// Returns a <see cref="System.String"/> that represents this instance.
708  /// </summary>
709  /// <param name="format">The format.</param>
710  /// <param name="formatProvider">The format provider.</param>
711  /// <returns>
712  /// A <see cref="System.String"/> that represents this instance.
713  /// </returns>
714  public string ToString(string format, IFormatProvider formatProvider)
715  {
716  if (format == null)
717  return ToString(formatProvider);
718 
719  return string.Format(formatProvider, "X:{0} Y:{1}", X.ToString(format, formatProvider), Y.ToString(format, formatProvider));
720  }
721 
722  /// <summary>
723  /// Returns a hash code for this instance.
724  /// </summary>
725  /// <returns>
726  /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
727  /// </returns>
728  public override int GetHashCode()
729  {
730  return X.GetHashCode() + Y.GetHashCode();
731  }
732 
733  /// <summary>
734  /// Determines whether the specified <see cref="SiliconStudio.Core.Mathematics.Int2"/> is equal to this instance.
735  /// </summary>
736  /// <param name="other">The <see cref="SiliconStudio.Core.Mathematics.Int2"/> to compare with this instance.</param>
737  /// <returns>
738  /// <c>true</c> if the specified <see cref="SiliconStudio.Core.Mathematics.Int2"/> is equal to this instance; otherwise, <c>false</c>.
739  /// </returns>
740  public bool Equals(Int2 other)
741  {
742  return ((float)Math.Abs(other.X - X) < MathUtil.ZeroTolerance &&
743  (float)Math.Abs(other.Y - Y) < MathUtil.ZeroTolerance);
744  }
745 
746  /// <summary>
747  /// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
748  /// </summary>
749  /// <param name="value">The <see cref="System.Object"/> to compare with this instance.</param>
750  /// <returns>
751  /// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
752  /// </returns>
753  public override bool Equals(object value)
754  {
755  if (value == null)
756  return false;
757 
758  if (value.GetType() != GetType())
759  return false;
760 
761  return Equals((Int2)value);
762  }
763 #if WPFInterop
764  /// <summary>
765  /// Performs an implicit conversion from <see cref="SiliconStudio.Core.Mathematics.Int2"/> to <see cref="System.Windows.Media.Media3D.Int3D"/>.
766  /// </summary>
767  /// <param name="value">The value.</param>
768  /// <returns>The result of the conversion.</returns>
769  public static implicit operator System.Windows.Media.Media3D.Int3D(Int2 value)
770  {
771  return new System.Windows.Media.Media3D.Int3D(value.X, value.Y, 0.0f);
772  }
773 
774  /// <summary>
775  /// Performs an explicit conversion from <see cref="System.Windows.Media.Media3D.Int3D"/> to <see cref="SiliconStudio.Core.Mathematics.Int2"/>.
776  /// </summary>
777  /// <param name="value">The value.</param>
778  /// <returns>The result of the conversion.</returns>
779  public static explicit operator Int2(System.Windows.Media.Media3D.Int3D value)
780  {
781  return new Int2((float)value.X, (float)value.Y);
782  }
783 #endif
784 
785 #if XnaInterop
786  /// <summary>
787  /// Performs an implicit conversion from <see cref="SiliconStudio.Core.Mathematics.Int2"/> to <see cref="Microsoft.Xna.Framework.Int2"/>.
788  /// </summary>
789  /// <param name="value">The value.</param>
790  /// <returns>The result of the conversion.</returns>
791  public static implicit operator Microsoft.Xna.Framework.Int2(Int2 value)
792  {
793  return new Microsoft.Xna.Framework.Int2(value.X, value.Y);
794  }
795 
796  /// <summary>
797  /// Performs an implicit conversion from <see cref="Microsoft.Xna.Framework.Int2"/> to <see cref="SiliconStudio.Core.Mathematics.Int2"/>.
798  /// </summary>
799  /// <param name="value">The value.</param>
800  /// <returns>The result of the conversion.</returns>
801  public static implicit operator Int2(Microsoft.Xna.Framework.Int2 value)
802  {
803  return new Int2(value.X, value.Y);
804  }
805 #endif
806  }
807 }
Int2(int x, int y)
Initializes a new instance of the SiliconStudio.Core.Mathematics.Int2 struct.
Definition: Int2.cs:95
static int Dot(Int2 left, Int2 right)
Calculates the dot product of two vectors.
Definition: Int2.cs:407
SiliconStudio.Paradox.Games.Mathematics.Vector2 Vector2
FbxDouble3 operator*(double factor, FbxDouble3 vector)
static Int2 Modulate(Int2 left, Int2 right)
Modulates a vector with another by performing component-wise multiplication.
Definition: Int2.cs:309
string ToString(string format)
Returns a System.String that represents this instance.
Definition: Int2.cs:686
static void Min(ref Int2 left, ref Int2 right, out Int2 result)
Returns a vector containing the smallest components of the specified vectors.
Definition: Int2.cs:532
Represents a two dimensional mathematical vector.
Definition: Vector2.cs:42
static void Negate(ref Int2 value, out Int2 result)
Reverses the direction of a given vector.
Definition: Int2.cs:341
Int2(Vector2 value)
Initializes a new instance of the SiliconStudio.Core.Mathematics.Int2 struct.
Definition: Int2.cs:105
static void Normalize(ref Int2 value, out Int2 result)
Converts the vector into a unit vector.
Definition: Int2.cs:417
static Int2 Min(Int2 left, Int2 right)
Returns a vector containing the smallest components of the specified vectors.
Definition: Int2.cs:544
static Int2 Max(Int2 left, Int2 right)
Returns a vector containing the largest components of the specified vectors.
Definition: Int2.cs:519
static void Max(ref Int2 left, ref Int2 right, out Int2 result)
Returns a vector containing the smallest components of the specified vectors.
Definition: Int2.cs:507
static Int2 Normalize(Int2 value)
Converts the vector into a unit vector.
Definition: Int2.cs:428
int Y
The Y component of the vector.
Definition: Int2.cs:78
int X
The X component of the vector.
Definition: Int2.cs:72
void Normalize()
Converts the vector into a unit vector.
Definition: Int2.cs:196
int[] ToArray()
Creates an array containing the elements of the vector.
Definition: Int2.cs:221
static void Multiply(ref Int2 value, int scale, out Int2 result)
Scales a vector by the given value.
Definition: Int2.cs:276
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ float size_t y
Definition: DirectXTexP.h:191
const float ZeroTolerance
The value for which all absolute numbers smaller than are considered equal to zero.
Definition: MathUtil.cs:38
static Int2 Subtract(Int2 left, Int2 right)
Subtracts two vectors.
Definition: Int2.cs:265
static void Divide(ref Int2 value, int scale, out Int2 result)
Scales a vector by the given value.
Definition: Int2.cs:320
static void Dot(ref Int2 left, ref Int2 right, out int result)
Calculates the dot product of two vectors.
Definition: Int2.cs:396
static Int2 Multiply(Int2 value, int scale)
Scales a vector by the given value.
Definition: Int2.cs:287
static Int2 Divide(Int2 value, int scale)
Scales a vector by the given value.
Definition: Int2.cs:331
static Int2 Add(Int2 left, Int2 right)
Adds two vectors.
Definition: Int2.cs:243
Represents a four dimensional mathematical vector.
Definition: Vector4.cs:42
override string ToString()
Returns a System.String that represents this instance.
Definition: Int2.cs:674
static void Lerp(ref Int2 start, ref Int2 end, float amount, out Int2 result)
Performs a linear interpolation between two vectors.
Definition: Int2.cs:446
int LengthSquared()
Calculates the squared length of the vector.
Definition: Int2.cs:188
static void Modulate(ref Int2 left, ref Int2 right, out Int2 result)
Modulates a vector with another by performing component-wise multiplication.
Definition: Int2.cs:298
static void Add(ref Int2 left, ref Int2 right, out Int2 result)
Adds two vectors.
Definition: Int2.cs:232
void Pow(int exponent)
Raises the exponent for each components.
Definition: Int2.cs:211
Represents a three dimensional mathematical vector.
Definition: Int2.cs:41
float X
The X component of the vector.
Definition: Vector2.cs:73
static Int2 Negate(Int2 value)
Reverses the direction of a given vector.
Definition: Int2.cs:351
string ToString(IFormatProvider formatProvider)
Returns a System.String that represents this instance.
Definition: Int2.cs:701
static Int2 SmoothStep(Int2 start, Int2 end, float amount)
Performs a cubic interpolation between two vectors.
Definition: Int2.cs:494
override int GetHashCode()
Returns a hash code for this instance.
Definition: Int2.cs:728
_In_ size_t _In_ size_t _In_ DXGI_FORMAT format
Definition: DirectXTexP.h:175
int Length()
Calculates the length of the vector.
Definition: Int2.cs:175
Int2(int[] values)
Initializes a new instance of the SiliconStudio.Core.Mathematics.Int2 struct.
Definition: Int2.cs:117
Int2(int value)
Initializes a new instance of the SiliconStudio.Core.Mathematics.Int2 struct.
Definition: Int2.cs:84
static Int2 Lerp(Int2 start, Int2 end, float amount)
Performs a linear interpolation between two vectors.
Definition: Int2.cs:464
static void SmoothStep(ref Int2 start, ref Int2 end, float amount, out Int2 result)
Performs a cubic interpolation between two vectors.
Definition: Int2.cs:478
static Int2 Clamp(Int2 value, Int2 min, Int2 max)
Restricts a value to be within a specified range.
Definition: Int2.cs:383
static void Subtract(ref Int2 left, ref Int2 right, out Int2 result)
Subtracts two vectors.
Definition: Int2.cs:254
string ToString(string format, IFormatProvider formatProvider)
Returns a System.String that represents this instance.
Definition: Int2.cs:714
static void Clamp(ref Int2 value, ref Int2 min, ref Int2 max, out Int2 result)
Restricts a value to be within a specified range.
Definition: Int2.cs:363
DataStyle
Specifies the style used for textual serialization when an array/list or a dictionary/map must be ser...
Definition: DataStyle.cs:9
override bool Equals(object value)
Determines whether the specified System.Object is equal to this instance.
Definition: Int2.cs:753
bool Equals(Int2 other)
Determines whether the specified SiliconStudio.Core.Mathematics.Int2 is equal to this instance...
Definition: Int2.cs:740