Show / Hide Table of Contents

Struct Vector2

Represents a 2D vector using two single-precision floating-point numbers.

Implements
IEquatable<Vector2>
Inherited Members
Object.Equals(Object, Object)
Object.ReferenceEquals(Object, Object)
Object.GetType()
Namespace: OpenTK
Assembly: OpenTK.dll
Syntax
[Serializable]
public struct Vector2 : IEquatable<Vector2>
Remarks

The Vector2 structure is suitable for interoperation with unmanaged code requiring two consecutive floats.

Constructors

Vector2(Single)

Constructs a new instance.

Declaration
public Vector2(float value)
Parameters
Type Name Description
Single value

The value that will initialize this instance.

Vector2(Single, Single)

Constructs a new Vector2.

Declaration
public Vector2(float x, float y)
Parameters
Type Name Description
Single x

The x coordinate of the net Vector2.

Single y

The y coordinate of the net Vector2.

Fields

One

Defines an instance with all components set to 1.

Declaration
public static readonly Vector2 One
Field Value
Type Description
Vector2

SizeInBytes

Defines the size of the Vector2 struct in bytes.

Declaration
public static readonly int SizeInBytes
Field Value
Type Description
Int32

UnitX

Defines a unit-length Vector2 that points towards the X-axis.

Declaration
public static readonly Vector2 UnitX
Field Value
Type Description
Vector2

UnitY

Defines a unit-length Vector2 that points towards the Y-axis.

Declaration
public static readonly Vector2 UnitY
Field Value
Type Description
Vector2

X

The X component of the Vector2.

Declaration
public float X
Field Value
Type Description
Single

Y

The Y component of the Vector2.

Declaration
public float Y
Field Value
Type Description
Single

Zero

Defines a zero-length Vector2.

Declaration
public static readonly Vector2 Zero
Field Value
Type Description
Vector2

Properties

Item[Int32]

Gets or sets the value at the index of the Vector.

Declaration
public float this[int index] { get; set; }
Parameters
Type Name Description
Int32 index
Property Value
Type Description
Single

Length

Gets the length (magnitude) of the vector.

Declaration
public readonly float Length { get; }
Property Value
Type Description
Single
See Also
LengthSquared

LengthFast

Gets an approximation of the vector length (magnitude).

Declaration
public readonly float LengthFast { get; }
Property Value
Type Description
Single
Remarks

This property uses an approximation of the square root function to calculate vector magnitude, with an upper error bound of 0.001.

See Also
LengthSquared

LengthSquared

Gets the square of the vector length (magnitude).

Declaration
public readonly float LengthSquared { get; }
Property Value
Type Description
Single
Remarks

This property avoids the costly square root operation required by the Length property. This makes it more suitable for comparisons.

See Also
LengthFast

PerpendicularLeft

Gets the perpendicular vector on the left side of this vector.

Declaration
public readonly Vector2 PerpendicularLeft { get; }
Property Value
Type Description
Vector2

PerpendicularRight

Gets the perpendicular vector on the right side of this vector.

Declaration
public readonly Vector2 PerpendicularRight { get; }
Property Value
Type Description
Vector2

Yx

Gets or sets an OpenTK.Vector2 with the Y and X components of this instance.

Declaration
public Vector2 Yx { get; set; }
Property Value
Type Description
Vector2

Methods

Add(Vector2, Vector2)

Adds two vectors.

Declaration
public static Vector2 Add(Vector2 a, Vector2 b)
Parameters
Type Name Description
Vector2 a

Left operand.

Vector2 b

Right operand.

Returns
Type Description
Vector2

Result of operation.

Add(ref Vector2, ref Vector2, out Vector2)

Adds two vectors.

Declaration
public static void Add(ref Vector2 a, ref Vector2 b, out Vector2 result)
Parameters
Type Name Description
Vector2 a

Left operand.

Vector2 b

Right operand.

Vector2 result

Result of operation.

BaryCentric(Vector2, Vector2, Vector2, Single, Single)

Interpolate 3 Vectors using Barycentric coordinates

Declaration
public static Vector2 BaryCentric(Vector2 a, Vector2 b, Vector2 c, float u, float v)
Parameters
Type Name Description
Vector2 a

First input Vector

Vector2 b

Second input Vector

Vector2 c

Third input Vector

Single u

First Barycentric Coordinate

Single v

Second Barycentric Coordinate

Returns
Type Description
Vector2

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

BaryCentric(ref Vector2, ref Vector2, ref Vector2, Single, Single, out Vector2)

Interpolate 3 Vectors using Barycentric coordinates

Declaration
public static void BaryCentric(ref Vector2 a, ref Vector2 b, ref Vector2 c, float u, float v, out Vector2 result)
Parameters
Type Name Description
Vector2 a

First input Vector.

Vector2 b

Second input Vector.

Vector2 c

Third input Vector.

Single u

First Barycentric Coordinate.

Single v

Second Barycentric Coordinate.

Vector2 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

Clamp(Vector2, Vector2, Vector2)

Clamp a vector to the given minimum and maximum vectors

Declaration
public static Vector2 Clamp(Vector2 vec, Vector2 min, Vector2 max)
Parameters
Type Name Description
Vector2 vec

Input vector

Vector2 min

Minimum vector

Vector2 max

Maximum vector

Returns
Type Description
Vector2

The clamped vector

Clamp(ref Vector2, ref Vector2, ref Vector2, out Vector2)

Clamp a vector to the given minimum and maximum vectors

Declaration
public static void Clamp(ref Vector2 vec, ref Vector2 min, ref Vector2 max, out Vector2 result)
Parameters
Type Name Description
Vector2 vec

Input vector

Vector2 min

Minimum vector

Vector2 max

Maximum vector

Vector2 result

The clamped vector

ComponentMax(Vector2, Vector2)

Returns a vector created from the largest of the corresponding components of the given vectors.

Declaration
public static Vector2 ComponentMax(Vector2 a, Vector2 b)
Parameters
Type Name Description
Vector2 a

First operand

Vector2 b

Second operand

Returns
Type Description
Vector2

The component-wise maximum

ComponentMax(ref Vector2, ref Vector2, out Vector2)

Returns a vector created from the largest of the corresponding components of the given vectors.

Declaration
public static void ComponentMax(ref Vector2 a, ref Vector2 b, out Vector2 result)
Parameters
Type Name Description
Vector2 a

First operand

Vector2 b

Second operand

Vector2 result

The component-wise maximum

ComponentMin(Vector2, Vector2)

Returns a vector created from the smallest of the corresponding components of the given vectors.

Declaration
public static Vector2 ComponentMin(Vector2 a, Vector2 b)
Parameters
Type Name Description
Vector2 a

First operand

Vector2 b

Second operand

Returns
Type Description
Vector2

The component-wise minimum

ComponentMin(ref Vector2, ref Vector2, out Vector2)

Returns a vector created from the smallest of the corresponding components of the given vectors.

Declaration
public static void ComponentMin(ref Vector2 a, ref Vector2 b, out Vector2 result)
Parameters
Type Name Description
Vector2 a

First operand

Vector2 b

Second operand

Vector2 result

The component-wise minimum

Distance(Vector2, Vector2)

Compute the euclidean distance between two vectors.

Declaration
public static float Distance(Vector2 vec1, Vector2 vec2)
Parameters
Type Name Description
Vector2 vec1

The first vector

Vector2 vec2

The second vector

Returns
Type Description
Single

The distance

Distance(ref Vector2, ref Vector2, out Single)

Compute the euclidean distance between two vectors.

Declaration
public static void Distance(ref Vector2 vec1, ref Vector2 vec2, out float result)
Parameters
Type Name Description
Vector2 vec1

The first vector

Vector2 vec2

The second vector

Single result

The distance

DistanceSquared(Vector2, Vector2)

Compute the squared euclidean distance between two vectors.

Declaration
public static float DistanceSquared(Vector2 vec1, Vector2 vec2)
Parameters
Type Name Description
Vector2 vec1

The first vector

Vector2 vec2

The second vector

Returns
Type Description
Single

The squared distance

DistanceSquared(ref Vector2, ref Vector2, out Single)

Compute the squared euclidean distance between two vectors.

Declaration
public static void DistanceSquared(ref Vector2 vec1, ref Vector2 vec2, out float result)
Parameters
Type Name Description
Vector2 vec1

The first vector

Vector2 vec2

The second vector

Single result

The squared distance

Divide(Vector2, Vector2)

Divides a vector by the components of a vector (scale).

Declaration
public static Vector2 Divide(Vector2 vector, Vector2 scale)
Parameters
Type Name Description
Vector2 vector

Left operand.

Vector2 scale

Right operand.

Returns
Type Description
Vector2

Result of the operation.

Divide(Vector2, Single)

Divides a vector by a scalar.

Declaration
public static Vector2 Divide(Vector2 vector, float scale)
Parameters
Type Name Description
Vector2 vector

Left operand.

Single scale

Right operand.

Returns
Type Description
Vector2

Result of the operation.

Divide(ref Vector2, ref Vector2, out Vector2)

Divide a vector by the components of a vector (scale).

Declaration
public static void Divide(ref Vector2 vector, ref Vector2 scale, out Vector2 result)
Parameters
Type Name Description
Vector2 vector

Left operand.

Vector2 scale

Right operand.

Vector2 result

Result of the operation.

Divide(ref Vector2, Single, out Vector2)

Divides a vector by a scalar.

Declaration
public static void Divide(ref Vector2 vector, float scale, out Vector2 result)
Parameters
Type Name Description
Vector2 vector

Left operand.

Single scale

Right operand.

Vector2 result

Result of the operation.

Dot(Vector2, Vector2)

Calculate the dot (scalar) product of two vectors

Declaration
public static float Dot(Vector2 left, Vector2 right)
Parameters
Type Name Description
Vector2 left

First operand

Vector2 right

Second operand

Returns
Type Description
Single

The dot product of the two inputs

Dot(ref Vector2, ref Vector2, out Single)

Calculate the dot (scalar) product of two vectors

Declaration
public static void Dot(ref Vector2 left, ref Vector2 right, out float result)
Parameters
Type Name Description
Vector2 left

First operand

Vector2 right

Second operand

Single result

The dot product of the two inputs

Equals(Vector2)

Indicates whether the current vector is equal to another vector.

Declaration
public bool Equals(Vector2 other)
Parameters
Type Name Description
Vector2 other

A vector to compare with this vector.

Returns
Type Description
Boolean

true if the current vector is equal to the vector parameter; otherwise, false.

Equals(Object)

Indicates whether this instance and a specified object are equal.

Declaration
public override bool Equals(object obj)
Parameters
Type Name Description
Object obj

The object to compare to.

Returns
Type Description
Boolean

True if the instances are equal; false otherwise.

Overrides
ValueType.Equals(Object)

GetHashCode()

Returns the hashcode for this instance.

Declaration
public override int GetHashCode()
Returns
Type Description
Int32

A System.Int32 containing the unique hashcode for this instance.

Overrides
ValueType.GetHashCode()

Lerp(Vector2, Vector2, Single)

Returns a new Vector that is the linear blend of the 2 given Vectors

Declaration
public static Vector2 Lerp(Vector2 a, Vector2 b, float blend)
Parameters
Type Name Description
Vector2 a

First input vector

Vector2 b

Second input vector

Single blend

The blend factor. a when blend=0, b when blend=1.

Returns
Type Description
Vector2

a when blend=0, b when blend=1, and a linear combination otherwise

Lerp(ref Vector2, ref Vector2, Single, out Vector2)

Returns a new Vector that is the linear blend of the 2 given Vectors

Declaration
public static void Lerp(ref Vector2 a, ref Vector2 b, float blend, out Vector2 result)
Parameters
Type Name Description
Vector2 a

First input vector

Vector2 b

Second input vector

Single blend

The blend factor. a when blend=0, b when blend=1.

Vector2 result

a when blend=0, b when blend=1, and a linear combination otherwise

MagnitudeMax(Vector2, Vector2)

Returns the Vector2 with the maximum magnitude. If the magnitudes are equal, the first vector is selected.

Declaration
public static Vector2 MagnitudeMax(Vector2 left, Vector2 right)
Parameters
Type Name Description
Vector2 left

Left operand

Vector2 right

Right operand

Returns
Type Description
Vector2

The maximum Vector2

MagnitudeMax(ref Vector2, ref Vector2, out Vector2)

Returns the Vector2 with the maximum magnitude. If the magnitudes are equal, the first vector is selected.

Declaration
public static void MagnitudeMax(ref Vector2 left, ref Vector2 right, out Vector2 result)
Parameters
Type Name Description
Vector2 left

Left operand

Vector2 right

Right operand

Vector2 result

The magnitude-wise maximum

MagnitudeMin(Vector2, Vector2)

Returns the Vector2 with the minimum magnitude. If the magnitudes are equal, the second vector is selected.

Declaration
public static Vector2 MagnitudeMin(Vector2 left, Vector2 right)
Parameters
Type Name Description
Vector2 left

Left operand

Vector2 right

Right operand

Returns
Type Description
Vector2

The minimum Vector2

MagnitudeMin(ref Vector2, ref Vector2, out Vector2)

Returns the Vector2 with the minimum magnitude. If the magnitudes are equal, the second vector is selected.

Declaration
public static void MagnitudeMin(ref Vector2 left, ref Vector2 right, out Vector2 result)
Parameters
Type Name Description
Vector2 left

Left operand

Vector2 right

Right operand

Vector2 result

The magnitude-wise minimum

Max(Vector2, Vector2)

Returns the Vector3 with the minimum magnitude

Declaration
[Obsolete("Use MagnitudeMax() instead.")]
public static Vector2 Max(Vector2 left, Vector2 right)
Parameters
Type Name Description
Vector2 left

Left operand

Vector2 right

Right operand

Returns
Type Description
Vector2

The minimum Vector3

Min(Vector2, Vector2)

Returns the Vector3 with the minimum magnitude

Declaration
[Obsolete("Use MagnitudeMin() instead.")]
public static Vector2 Min(Vector2 left, Vector2 right)
Parameters
Type Name Description
Vector2 left

Left operand

Vector2 right

Right operand

Returns
Type Description
Vector2

The minimum Vector3

Multiply(Vector2, Vector2)

Multiplies a vector by the components a vector (scale).

Declaration
public static Vector2 Multiply(Vector2 vector, Vector2 scale)
Parameters
Type Name Description
Vector2 vector

Left operand.

Vector2 scale

Right operand.

Returns
Type Description
Vector2

Result of the operation.

Multiply(Vector2, Single)

Multiplies a vector by a scalar.

Declaration
public static Vector2 Multiply(Vector2 vector, float scale)
Parameters
Type Name Description
Vector2 vector

Left operand.

Single scale

Right operand.

Returns
Type Description
Vector2

Result of the operation.

Multiply(ref Vector2, ref Vector2, out Vector2)

Multiplies a vector by the components of a vector (scale).

Declaration
public static void Multiply(ref Vector2 vector, ref Vector2 scale, out Vector2 result)
Parameters
Type Name Description
Vector2 vector

Left operand.

Vector2 scale

Right operand.

Vector2 result

Result of the operation.

Multiply(ref Vector2, Single, out Vector2)

Multiplies a vector by a scalar.

Declaration
public static void Multiply(ref Vector2 vector, float scale, out Vector2 result)
Parameters
Type Name Description
Vector2 vector

Left operand.

Single scale

Right operand.

Vector2 result

Result of the operation.

Normalize()

Scales the Vector2 to unit length.

Declaration
public void Normalize()

Normalize(Vector2)

Scale a vector to unit length

Declaration
public static Vector2 Normalize(Vector2 vec)
Parameters
Type Name Description
Vector2 vec

The input vector

Returns
Type Description
Vector2

The normalized vector

Normalize(ref Vector2, out Vector2)

Scale a vector to unit length

Declaration
public static void Normalize(ref Vector2 vec, out Vector2 result)
Parameters
Type Name Description
Vector2 vec

The input vector

Vector2 result

The normalized vector

Normalized()

Returns a copy of the Vector2 scaled to unit length.

Declaration
public Vector2 Normalized()
Returns
Type Description
Vector2

NormalizeFast()

Scales the Vector2 to approximately unit length.

Declaration
public void NormalizeFast()

NormalizeFast(Vector2)

Scale a vector to approximately unit length

Declaration
public static Vector2 NormalizeFast(Vector2 vec)
Parameters
Type Name Description
Vector2 vec

The input vector

Returns
Type Description
Vector2

The normalized vector

NormalizeFast(ref Vector2, out Vector2)

Scale a vector to approximately unit length

Declaration
public static void NormalizeFast(ref Vector2 vec, out Vector2 result)
Parameters
Type Name Description
Vector2 vec

The input vector

Vector2 result

The normalized vector

PerpDot(Vector2, Vector2)

Calculate the perpendicular dot (scalar) product of two vectors

Declaration
public static float PerpDot(Vector2 left, Vector2 right)
Parameters
Type Name Description
Vector2 left

First operand

Vector2 right

Second operand

Returns
Type Description
Single

The perpendicular dot product of the two inputs

PerpDot(ref Vector2, ref Vector2, out Single)

Calculate the perpendicular dot (scalar) product of two vectors

Declaration
public static void PerpDot(ref Vector2 left, ref Vector2 right, out float result)
Parameters
Type Name Description
Vector2 left

First operand

Vector2 right

Second operand

Single result

The perpendicular dot product of the two inputs

Subtract(Vector2, Vector2)

Subtract one Vector from another

Declaration
public static Vector2 Subtract(Vector2 a, Vector2 b)
Parameters
Type Name Description
Vector2 a

First operand

Vector2 b

Second operand

Returns
Type Description
Vector2

Result of subtraction

Subtract(ref Vector2, ref Vector2, out Vector2)

Subtract one Vector from another

Declaration
public static void Subtract(ref Vector2 a, ref Vector2 b, out Vector2 result)
Parameters
Type Name Description
Vector2 a

First operand

Vector2 b

Second operand

Vector2 result

Result of subtraction

ToString()

Returns a System.String that represents the current Vector2.

Declaration
public override string ToString()
Returns
Type Description
String
Overrides
ValueType.ToString()

Transform(Vector2, Quaternion)

Transforms a vector by a quaternion rotation.

Declaration
public static Vector2 Transform(Vector2 vec, Quaternion quat)
Parameters
Type Name Description
Vector2 vec

The vector to transform.

Quaternion quat

The quaternion to rotate the vector by.

Returns
Type Description
Vector2

The result of the operation.

Transform(ref Vector2, ref Quaternion, out Vector2)

Transforms a vector by a quaternion rotation.

Declaration
public static void Transform(ref Vector2 vec, ref Quaternion quat, out Vector2 result)
Parameters
Type Name Description
Vector2 vec

The vector to transform.

Quaternion quat

The quaternion to rotate the vector by.

Vector2 result

The result of the operation.

Operators

Addition(Vector2, Vector2)

Adds the specified instances.

Declaration
public static Vector2 operator +(Vector2 left, Vector2 right)
Parameters
Type Name Description
Vector2 left

Left operand.

Vector2 right

Right operand.

Returns
Type Description
Vector2

Result of addition.

Division(Vector2, Single)

Divides the specified instance by a scalar.

Declaration
public static Vector2 operator /(Vector2 vec, float scale)
Parameters
Type Name Description
Vector2 vec

Left operand

Single scale

Right operand

Returns
Type Description
Vector2

Result of the division.

Equality(Vector2, Vector2)

Compares the specified instances for equality.

Declaration
public static bool operator ==(Vector2 left, Vector2 right)
Parameters
Type Name Description
Vector2 left

Left operand.

Vector2 right

Right operand.

Returns
Type Description
Boolean

True if both instances are equal; false otherwise.

Inequality(Vector2, Vector2)

Compares the specified instances for inequality.

Declaration
public static bool operator !=(Vector2 left, Vector2 right)
Parameters
Type Name Description
Vector2 left

Left operand.

Vector2 right

Right operand.

Returns
Type Description
Boolean

True if both instances are not equal; false otherwise.

Multiply(Vector2, Vector2)

Component-wise multiplication between the specified instance by a scale vector.

Declaration
public static Vector2 operator *(Vector2 vec, Vector2 scale)
Parameters
Type Name Description
Vector2 vec

Right operand.

Vector2 scale

Left operand.

Returns
Type Description
Vector2

Result of multiplication.

Multiply(Vector2, Single)

Multiplies the specified instance by a scalar.

Declaration
public static Vector2 operator *(Vector2 vec, float scale)
Parameters
Type Name Description
Vector2 vec

Left operand.

Single scale

Right operand.

Returns
Type Description
Vector2

Result of multiplication.

Multiply(Single, Vector2)

Multiplies the specified instance by a scalar.

Declaration
public static Vector2 operator *(float scale, Vector2 vec)
Parameters
Type Name Description
Single scale

Left operand.

Vector2 vec

Right operand.

Returns
Type Description
Vector2

Result of multiplication.

Subtraction(Vector2, Vector2)

Subtracts the specified instances.

Declaration
public static Vector2 operator -(Vector2 left, Vector2 right)
Parameters
Type Name Description
Vector2 left

Left operand.

Vector2 right

Right operand.

Returns
Type Description
Vector2

Result of subtraction.

UnaryNegation(Vector2)

Negates the specified instance.

Declaration
public static Vector2 operator -(Vector2 vec)
Parameters
Type Name Description
Vector2 vec

Operand.

Returns
Type Description
Vector2

Result of negation.

Implements

System.IEquatable<T>
In This Article
Back to top Generated by DocFX