30 using System.Globalization;
31 using System.Runtime.InteropServices;
32 using System.ComponentModel;
34 namespace SiliconStudio.Core.Mathematics
39 [DataContract(
"float2")]
41 [StructLayout(LayoutKind.Sequential, Pack = 4)]
47 public static readonly
int SizeInBytes = Marshal.SizeOf(typeof(
Vector2));
111 throw new ArgumentNullException(
"values");
112 if (values.Length != 2)
113 throw new ArgumentOutOfRangeException(
"values",
"There must be two and only two input values for Vector2.");
122 public bool IsNormalized
134 public float this[
int index]
144 throw new ArgumentOutOfRangeException(
"index",
"Indices for Vector2 run from 0 to 1, inclusive.");
151 case 0: X = value;
break;
152 case 1: Y = value;
break;
153 default:
throw new ArgumentOutOfRangeException(
"index",
"Indices for Vector2 run from 0 to 1, inclusive.");
168 return (
float)Math.Sqrt((X * X) + (Y * Y));
181 return (X * X) + (Y * Y);
189 float length = Length();
192 float inv = 1.0f / length;
204 return new float[] { X, Y };
215 result =
new Vector2(left.X + right.X, left.Y + right.Y);
226 return new Vector2(left.
X + right.
X, left.
Y + right.
Y);
237 result =
new Vector2(left.X - right.X, left.Y - right.Y);
248 return new Vector2(left.
X - right.
X, left.
Y - right.
Y);
259 result =
new Vector2(value.X * scale, value.Y * scale);
270 return new Vector2(value.
X * scale, value.
Y * scale);
281 result =
new Vector2(left.X * right.X, left.Y * right.Y);
292 return new Vector2(left.
X * right.
X, left.
Y * right.
Y);
303 result =
new Vector2(value.X / scale, value.Y / scale);
314 return new Vector2(value.
X / scale, value.
Y / scale);
325 result =
new Vector2(left.X / right.X, left.Y / right.Y);
336 return new Vector2(left.
X / right.
X, left.
Y / right.
Y);
346 result =
new Vector2(-value.X, -value.Y);
370 result =
new Vector2((value1.X + (amount1 * (value2.X - value1.X))) + (amount2 * (value3.X - value1.X)),
371 (value1.Y + (amount1 * (value2.Y - value1.Y))) + (amount2 * (value3.Y - value1.Y)));
386 Barycentric(ref value1, ref value2, ref value3, amount1, amount2, out result);
400 x = (x > max.X) ? max.X : x;
401 x = (x < min.X) ? min.X : x;
404 y = (y > max.Y) ? max.Y : y;
405 y = (y < min.Y) ? min.Y :
y;
420 Clamp(ref value, ref min, ref max, out result);
436 float x = value1.X - value2.X;
437 float y = value1.Y - value2.Y;
439 result = (float)Math.Sqrt((x * x) + (y * y));
454 float x = value1.X - value2.X;
455 float y = value1.Y - value2.Y;
457 return (
float)Math.Sqrt((x * x) + (y * y));
475 float x = value1.X - value2.X;
476 float y = value1.Y - value2.Y;
478 result = (x * x) + (y * y);
496 float x = value1.X - value2.X;
497 float y = value1.Y - value2.Y;
499 return (x * x) + (y *
y);
510 result = (left.X * right.X) + (left.Y * right.Y);
521 return (left.
X * right.
X) + (left.Y * right.Y);
560 result.X = start.X + ((end.X - start.X) * amount);
561 result.Y = start.Y + ((end.Y - start.Y) * amount);
579 Lerp(ref start, ref end, amount, out result);
592 amount = (amount > 1.0f) ? 1.0f : ((amount < 0.0f) ? 0.0f : amount);
593 amount = (amount * amount) * (3.0f - (2.0f * amount));
595 result.X = start.X + ((end.X - start.X) * amount);
596 result.Y = start.Y + ((end.Y - start.Y) * amount);
609 SmoothStep(ref start, ref end, amount, out result);
624 float squared = amount * amount;
625 float cubed = amount * squared;
626 float part1 = ((2.0f * cubed) - (3.0f * squared)) + 1.0f;
627 float part2 = (-2.0f * cubed) + (3.0f * squared);
628 float part3 = (cubed - (2.0f * squared)) + amount;
629 float part4 = cubed - squared;
631 result.X = (((value1.X * part1) + (value2.X * part2)) + (tangent1.X * part3)) + (tangent2.X * part4);
632 result.Y = (((value1.Y * part1) + (value2.Y * part2)) + (tangent1.Y * part3)) + (tangent2.Y * part4);
647 Hermite(ref value1, ref tangent1, ref value2, ref tangent2, amount, out result);
662 float squared = amount * amount;
663 float cubed = amount * squared;
665 result.X = 0.5f * ((((2.0f * value2.X) + ((-value1.X + value3.X) * amount)) +
666 (((((2.0f * value1.X) - (5.0f * value2.X)) + (4.0f * value3.X)) - value4.X) * squared)) +
667 ((((-value1.X + (3.0f * value2.X)) - (3.0f * value3.X)) + value4.X) * cubed));
669 result.Y = 0.5f * ((((2.0f * value2.Y) + ((-value1.Y + value3.Y) * amount)) +
670 (((((2.0f * value1.Y) - (5.0f * value2.Y)) + (4.0f * value3.Y)) - value4.Y) * squared)) +
671 ((((-value1.Y + (3.0f * value2.Y)) - (3.0f * value3.Y)) + value4.Y) * cubed));
686 CatmullRom(ref value1, ref value2, ref value3, ref value4, amount, out result);
698 result.X = (left.X > right.X) ? left.X : right.X;
699 result.Y = (left.Y > right.Y) ? left.Y : right.Y;
711 Max(ref left, ref right, out result);
723 result.X = (left.X < right.X) ? left.X : right.X;
724 result.Y = (left.Y < right.Y) ? left.Y : right.Y;
736 Min(ref left, ref right, out result);
750 float dot = (vector.X * normal.X) + (vector.Y * normal.Y);
752 result.X = vector.X - ((2.0f * dot) * normal.X);
753 result.Y = vector.Y - ((2.0f * dot) * normal.Y);
767 Reflect(ref vector, ref normal, out result);
797 throw new ArgumentNullException(
"source");
798 if (destination == null)
799 throw new ArgumentNullException(
"destination");
800 if (destination.
Length < source.Length)
801 throw new ArgumentOutOfRangeException(
"destination",
"The destination array must be of same length or larger length than the source array.");
803 for (
int i = 0; i < source.Length; ++i)
807 for (
int r = 0; r < i; ++r)
809 newvector -= (Vector2.Dot(destination[r], newvector) /
Vector2.
Dot(destination[r], destination[r])) * destination[r];
812 destination[i] = newvector;
844 throw new ArgumentNullException(
"source");
845 if (destination == null)
846 throw new ArgumentNullException(
"destination");
847 if (destination.
Length < source.Length)
848 throw new ArgumentOutOfRangeException(
"destination",
"The destination array must be of same length or larger length than the source array.");
850 for (
int i = 0; i < source.Length; ++i)
854 for (
int r = 0; r < i; ++r)
856 newvector -= Vector2.Dot(destination[r], newvector) * destination[r];
859 newvector.Normalize();
860 destination[i] = newvector;
872 float x = rotation.X + rotation.X;
873 float y = rotation.Y + rotation.Y;
874 float z = rotation.Z + rotation.Z;
875 float wz = rotation.W *
z;
876 float xx = rotation.X * x;
877 float xy = rotation.X *
y;
878 float yy = rotation.Y *
y;
879 float zz = rotation.Z *
z;
881 result =
new Vector2((vector.X * (1.0f - yy - zz)) + (vector.Y * (xy - wz)), (vector.X * (xy + wz)) + (vector.Y * (1.0f - xx - zz)));
893 Transform(ref vector, ref rotation, out result);
909 throw new ArgumentNullException(
"source");
910 if (destination == null)
911 throw new ArgumentNullException(
"destination");
913 throw new ArgumentOutOfRangeException(
"destination",
"The destination array must be of same length or larger length than the source array.");
915 float x = rotation.X + rotation.X;
916 float y = rotation.Y + rotation.Y;
917 float z = rotation.Z + rotation.Z;
918 float wz = rotation.W *
z;
919 float xx = rotation.X * x;
920 float xy = rotation.X *
y;
921 float yy = rotation.Y *
y;
922 float zz = rotation.Z *
z;
924 float num1 = (1.0f - yy - zz);
925 float num2 = (xy - wz);
926 float num3 = (xy + wz);
927 float num4 = (1.0f - xx - zz);
929 for (
int i = 0; i < source.Length; ++i)
932 (source[i].X * num1) + (source[i].Y * num2),
933 (source[i].X * num3) + (source[i].Y * num4));
946 (vector.X * transform.M11) + (vector.Y * transform.M21) + transform.M41,
947 (vector.X * transform.M12) + (vector.Y * transform.M22) + transform.M42,
948 (vector.X * transform.M13) + (vector.Y * transform.M23) + transform.M43,
949 (vector.X * transform.M14) + (vector.Y * transform.M24) + transform.M44);
961 Transform(ref vector, ref transform, out result);
976 throw new ArgumentNullException(
"source");
977 if (destination == null)
978 throw new ArgumentNullException(
"destination");
980 throw new ArgumentOutOfRangeException(
"destination",
"The destination array must be of same length or larger length than the source array.");
982 for (
int i = 0; i < source.Length; ++i)
984 Transform(ref source[i], ref transform, out destination[i]);
1004 vector.X = (coordinate.X * transform.M11) + (coordinate.Y * transform.M21) + transform.M41;
1005 vector.Y = (coordinate.X * transform.M12) + (coordinate.Y * transform.M22) + transform.M42;
1006 vector.Z = (coordinate.X * transform.M13) + (coordinate.Y * transform.M23) + transform.M43;
1007 vector.W = 1f / ((coordinate.X * transform.M14) + (coordinate.Y * transform.M24) + transform.M44);
1009 result =
new Vector2(vector.
X * vector.
W, vector.
Y * vector.
W);
1028 TransformCoordinate(ref coordinate, ref transform, out result);
1051 throw new ArgumentNullException(
"source");
1052 if (destination == null)
1053 throw new ArgumentNullException(
"destination");
1055 throw new ArgumentOutOfRangeException(
"destination",
"The destination array must be of same length or larger length than the source array.");
1057 for (
int i = 0; i < source.Length; ++i)
1059 TransformCoordinate(ref source[i], ref transform, out destination[i]);
1079 (normal.X * transform.M11) + (normal.Y * transform.M21),
1080 (normal.X * transform.M12) + (normal.Y * transform.M22));
1099 TransformNormal(ref normal, ref transform, out result);
1122 throw new ArgumentNullException(
"source");
1123 if (destination == null)
1124 throw new ArgumentNullException(
"destination");
1126 throw new ArgumentOutOfRangeException(
"destination",
"The destination array must be of same length or larger length than the source array.");
1128 for (
int i = 0; i < source.Length; ++i)
1130 TransformNormal(ref source[i], ref transform, out destination[i]);
1142 return new Vector2(left.
X + right.
X, left.
Y + right.
Y);
1163 return new Vector2(left.
X - right.
X, left.
Y - right.
Y);
1184 return new Vector2(left.
X*right.
X, left.
Y*right.
Y);
1195 return new Vector2(value.
X * scale, value.
Y * scale);
1206 return new Vector2(value.
X * scale, value.
Y * scale);
1217 return new Vector2(value.
X / scale, value.
Y / scale);
1228 return left.Equals(right);
1239 return !left.Equals(right);
1249 return new Vector3(value, 0.0f);
1259 return new Vector4(value, 0.0f, 0.0f);
1270 return string.Format(CultureInfo.CurrentCulture,
"X:{0} Y:{1}", X, Y);
1285 return string.Format(CultureInfo.CurrentCulture,
"X:{0} Y:{1}", X.ToString(
format, CultureInfo.CurrentCulture), Y.ToString(format, CultureInfo.CurrentCulture));
1297 return string.Format(formatProvider,
"X:{0} Y:{1}", X, Y);
1311 ToString(formatProvider);
1313 return string.Format(formatProvider,
"X:{0} Y:{1}", X.ToString(
format, formatProvider), Y.ToString(format, formatProvider));
1324 return X.GetHashCode() + Y.GetHashCode();
1336 return ((
float)Math.Abs(other.
X - X) < MathUtil.ZeroTolerance &&
1337 (float)Math.Abs(other.
Y - Y) < MathUtil.ZeroTolerance);
1352 if (value.GetType() != GetType())
1355 return Equals((
Vector2)value);
1364 public static implicit
operator System.Windows.Point(
Vector2 value)
1366 return new System.Windows.Point(value.X, value.Y);
1374 public static explicit operator Vector2(System.Windows.Point value)
1376 return new Vector2((
float)value.X, (
float)value.Y);
1386 public static implicit
operator Microsoft.Xna.Framework.Vector2(
Vector2 value)
1388 return new Microsoft.Xna.Framework.Vector2(value.X, value.Y);
1396 public static implicit
operator Vector2(Microsoft.Xna.Framework.Vector2 value)
1398 return new Vector2(value.X, value.Y);
static void Clamp(ref Vector2 value, ref Vector2 min, ref Vector2 max, out Vector2 result)
Restricts a value to be within a specified range.
static Vector2 Hermite(Vector2 value1, Vector2 tangent1, Vector2 value2, Vector2 tangent2, float amount)
Performs a Hermite spline interpolation.
static float Distance(Vector2 value1, Vector2 value2)
Calculates the distance between two vectors.
SiliconStudio.Paradox.Games.Mathematics.Vector2 Vector2
FbxDouble3 operator*(double factor, FbxDouble3 vector)
float W
The W component of the vector.
static Vector2 Transform(Vector2 vector, Quaternion rotation)
Transforms a 2D vector by the given SiliconStudio.Core.Mathematics.Quaternion rotation.
static void Barycentric(ref Vector2 value1, ref Vector2 value2, ref Vector2 value3, float amount1, float amount2, out Vector2 result)
Returns a SiliconStudio.Core.Mathematics.Vector2 containing the 2D Cartesian coordinates of a point s...
Represents a two dimensional mathematical vector.
static Vector2 Min(Vector2 left, Vector2 right)
Returns a vector containing the smallest components of the specified vectors.
static Vector2 SmoothStep(Vector2 start, Vector2 end, float amount)
Performs a cubic interpolation between two vectors.
static void TransformCoordinate(ref Vector2 coordinate, ref Matrix transform, out Vector2 result)
Performs a coordinate transformation using the given SiliconStudio.Core.Mathematics.Matrix.
static void Orthogonalize(Vector2[] destination, params Vector2[] source)
Orthogonalizes a list of vectors.
void Normalize()
Converts the vector into a unit vector.
float Length()
Calculates the length of the vector.
static Vector2 Lerp(Vector2 start, Vector2 end, float amount)
Performs a linear interpolation between two vectors.
static void DistanceSquared(ref Vector2 value1, ref Vector2 value2, out float result)
Calculates the squared distance between two vectors.
Vector2(float x, float y)
Initializes a new instance of the SiliconStudio.Core.Mathematics.Vector2 struct.
static Vector2 Subtract(Vector2 left, Vector2 right)
Subtracts two vectors.
static Vector2 CatmullRom(Vector2 value1, Vector2 value2, Vector2 value3, Vector2 value4, float amount)
Performs a Catmull-Rom interpolation using the specified positions.
static Vector2 Multiply(Vector2 value, float scale)
Scales a vector by the given value.
float X
The X component of the vector.
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ float size_t y
static void Transform(Vector2[] source, ref Quaternion rotation, Vector2[] destination)
Transforms an array of vectors by the given SiliconStudio.Core.Mathematics.Quaternion rotation...
static Vector2 Divide(Vector2 value, float scale)
Scales a vector by the given value.
Vector2(float[] values)
Initializes a new instance of the SiliconStudio.Core.Mathematics.Vector2 struct.
static void Max(ref Vector2 left, ref Vector2 right, out Vector2 result)
Returns a vector containing the smallest components of the specified vectors.
const float ZeroTolerance
The value for which all absolute numbers smaller than are considered equal to zero.
static void Transform(ref Vector2 vector, ref Quaternion rotation, out Vector2 result)
Transforms a 2D vector by the given SiliconStudio.Core.Mathematics.Quaternion rotation.
Vector2(float value)
Initializes a new instance of the SiliconStudio.Core.Mathematics.Vector2 struct.
string ToString(IFormatProvider formatProvider)
Returns a System.String that represents this instance.
static float Dot(Vector2 left, Vector2 right)
Calculates the dot product of two vectors.
static Vector2 Max(Vector2 left, Vector2 right)
Returns a vector containing the largest components of the specified vectors.
static void Hermite(ref Vector2 value1, ref Vector2 tangent1, ref Vector2 value2, ref Vector2 tangent2, float amount, out Vector2 result)
Performs a Hermite spline interpolation.
static void Lerp(ref Vector2 start, ref Vector2 end, float amount, out Vector2 result)
Performs a linear interpolation between two vectors.
static void Multiply(ref Vector2 value, float scale, out Vector2 result)
Scales a vector by the given value.
static Vector2 Add(Vector2 left, Vector2 right)
Adds two vectors.
static Vector2 TransformNormal(Vector2 normal, Matrix transform)
Performs a normal transformation using the given SiliconStudio.Core.Mathematics.Matrix.
string ToString(string format, IFormatProvider formatProvider)
Returns a System.String that represents this instance.
static Vector2 Negate(Vector2 value)
Reverses the direction of a given vector.
static void TransformCoordinate(Vector2[] source, ref Matrix transform, Vector2[] destination)
Performs a coordinate transformation on an array of vectors using the given SiliconStudio.Core.Mathematics.Matrix.
static void Min(ref Vector2 left, ref Vector2 right, out Vector2 result)
Returns a vector containing the smallest components of the specified vectors.
Represents a four dimensional mathematical vector.
float Y
The Y component of the vector.
Represents a four dimensional mathematical quaternion.
static Vector2 Demodulate(Vector2 left, Vector2 right)
Demodulates a vector with another by performing component-wise division.
override int GetHashCode()
Returns a hash code for this instance.
static void TransformNormal(Vector2[] source, ref Matrix transform, Vector2[] destination)
Performs a normal transformation on an array of vectors using the given SiliconStudio.Core.Mathematics.Matrix.
static void Normalize(ref Vector2 value, out Vector2 result)
Converts the vector into a unit vector.
static void Dot(ref Vector2 left, ref Vector2 right, out float result)
Calculates the dot product of two vectors.
static void Negate(ref Vector2 value, out Vector2 result)
Reverses the direction of a given vector.
float Length()
Calculates the length of the vector.
static void TransformNormal(ref Vector2 normal, ref Matrix transform, out Vector2 result)
Performs a normal transformation using the given SiliconStudio.Core.Mathematics.Matrix.
static void Reflect(ref Vector2 vector, ref Vector2 normal, out Vector2 result)
Returns the reflection of a vector off a surface that has the specified normal.
static Vector2 Reflect(Vector2 vector, Vector2 normal)
Returns the reflection of a vector off a surface that has the specified normal.
float Y
The Y component of the vector.
static void Demodulate(ref Vector2 left, ref Vector2 right, out Vector2 result)
Demodulates a vector with another by performing component-wise division.
static Vector2 Clamp(Vector2 value, Vector2 min, Vector2 max)
Restricts a value to be within a specified range.
static void Modulate(ref Vector2 left, ref Vector2 right, out Vector2 result)
Modulates a vector with another by performing component-wise multiplication.
static Vector4 Transform(Vector2 vector, Matrix transform)
Transforms a 2D vector by the given SiliconStudio.Core.Mathematics.Matrix.
static void Transform(ref Vector2 vector, ref Matrix transform, out Vector4 result)
Transforms a 2D vector by the given SiliconStudio.Core.Mathematics.Matrix.
float X
The X component of the vector.
static Vector2 Normalize(Vector2 value)
Converts the vector into a unit vector.
static void Subtract(ref Vector2 left, ref Vector2 right, out Vector2 result)
Subtracts two vectors.
override bool Equals(object value)
Determines whether the specified System.Object is equal to this instance.
SiliconStudio.Core.Mathematics.Vector3 Vector3
static Vector2 TransformCoordinate(Vector2 coordinate, Matrix transform)
Performs a coordinate transformation using the given SiliconStudio.Core.Mathematics.Matrix.
string ToString(string format)
Returns a System.String that represents this instance.
_In_ size_t _In_ size_t _In_ DXGI_FORMAT format
static Vector2 Modulate(Vector2 left, Vector2 right)
Modulates a vector with another by performing component-wise multiplication.
static void Add(ref Vector2 left, ref Vector2 right, out Vector2 result)
Adds two vectors.
static void Transform(Vector2[] source, ref Matrix transform, Vector4[] destination)
Transforms an array of 2D vectors by the given SiliconStudio.Core.Mathematics.Matrix.
static void Orthonormalize(Vector2[] destination, params Vector2[] source)
Orthonormalizes a list of vectors.
static void Divide(ref Vector2 value, float scale, out Vector2 result)
Scales a vector by the given value.
static void CatmullRom(ref Vector2 value1, ref Vector2 value2, ref Vector2 value3, ref Vector2 value4, float amount, out Vector2 result)
Performs a Catmull-Rom interpolation using the specified positions.
static float DistanceSquared(Vector2 value1, Vector2 value2)
Calculates the squared distance between two vectors.
static void SmoothStep(ref Vector2 start, ref Vector2 end, float amount, out Vector2 result)
Performs a cubic interpolation between two vectors.
DataStyle
Specifies the style used for textual serialization when an array/list or a dictionary/map must be ser...
float LengthSquared()
Calculates the squared length of the vector.
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ float size_t size_t z
override string ToString()
Returns a System.String that represents this instance.
bool Equals(Vector2 other)
Determines whether the specified SiliconStudio.Core.Mathematics.Vector2 is equal to this instance...
static Vector2 Barycentric(Vector2 value1, Vector2 value2, Vector2 value3, float amount1, float amount2)
Returns a SiliconStudio.Core.Mathematics.Vector2 containing the 2D Cartesian coordinates of a point s...
static void Distance(ref Vector2 value1, ref Vector2 value2, out float result)
Calculates the distance between two vectors.
float[] ToArray()
Creates an array containing the elements of the vector.
Represents a 4x4 mathematical matrix.