record TVector2

DescriptionHierarchyFieldsMethodsProperties

Unit

Declaration

type TVector2 = record

Description

A 2-dimensional vector. Can be used to represent points or vectors in 2D space, using the X and Y fields. You can also use it to represent texture coordinates, by using S and T (these are just aliases for X and Y). It can also be used to group 2 arbitrary values together in an array (using C[0] and C[1], which are also just aliases for X and Y).

TVector2 is compatible with TPointF in the Delphi RTL. You can typecast between these two types or implicitly convert from one to the other through assignment (eg. MyVector2 := MyPointF).

Overview

Fields

X: Single
Y: Single
R: Single
G: Single
S: Single
T: Single
C: array [0..1] of Single

Methods

procedure Init; overload; inline;
procedure Init(const A: Single); overload; inline;
procedure Init(const A1, A2: Single); overload; inline;
procedure Init(const APoint: TPoint); overload; inline;
class operator Implicit(const A: TPointF): TVector2; inline;
class operator Implicit(const A: TVector2): TPointF; inline;
class operator Equal(const A, B: TVector2): Boolean; inline;
class operator NotEqual(const A, B: TVector2): Boolean; inline;
class operator Negative(const A: TVector2): TVector2; inline;
class operator Add(const A: TVector2; const B: Single): TVector2; inline;
class operator Add(const A: Single; const B: TVector2): TVector2; inline;
class operator Add(const A, B: TVector2): TVector2; inline;
class operator Subtract(const A: TVector2; const B: Single): TVector2; inline;
class operator Subtract(const A: Single; const B: TVector2): TVector2; inline;
class operator Subtract(const A, B: TVector2): TVector2; inline;
class operator Multiply(const A: TVector2; const B: Single): TVector2; inline;
class operator Multiply(const A: Single; const B: TVector2): TVector2; inline;
class operator Multiply(const A, B: TVector2): TVector2; inline;
class operator Divide(const A: TVector2; const B: Single): TVector2; inline;
class operator Divide(const A: Single; const B: TVector2): TVector2;
class operator Divide(const A, B: TVector2): TVector2;
function Equals(const AOther: TVector2; const ATolerance: Single = SINGLE_TOLERANCE): Boolean; inline;
function Distance(const AOther: TVector2): Single; inline;
function DistanceSquared(const AOther: TVector2): Single; inline;
function Dot(const AOther: TVector2): Single;
function Cross(const AOther: TVector2): Single;
procedure Offset(const ADeltaX, ADeltaY: Single); overload; inline;
procedure Offset(const ADelta: TVector2); overload; inline;
function Normalize: TVector2; inline;
procedure SetNormalized; inline;
function NormalizeFast: TVector2;
procedure SetNormalizedFast;
function FaceForward(const I, NRef: TVector2): TVector2; inline;
function Reflect(const N: TVector2): TVector2; inline;
function Refract(const N: TVector2; const Eta: Single): TVector2; inline;
function Limit(const AMaxLength: Single): TVector2; inline;
procedure SetLimit(const AMaxLength: Single); inline;
function LimitSquared(const AMaxLengthSquared: Single): TVector2;
procedure SetLimitSquared(const AMaxLengthSquared: Single); inline;
function Clamp(const AMinLength, AMaxLength: Single): TVector2;
procedure SetClamped(const AMinLength, AMaxLength: Single); inline;
function Rotate(const ARadians: Single): TVector2;
procedure SetRotated(const ARadians: Single); inline;
function Rotate90CCW: TVector2;
procedure SetRotated90CCW; inline;
function Rotate90CW: TVector2;
procedure SetRotated90CW; inline;
function AngleTo(const ATarget: TVector2): Single;
function Lerp(const ATarget: TVector2; const AAlpha: Single): TVector2; inline;
procedure SetLerp(const ATarget: TVector2; const AAlpha: Single); inline;
function IsNormalized: Boolean; overload; inline;
function IsNormalized(const AErrorMargin: Single): Boolean; overload;
function IsZero: Boolean; overload; inline;
function IsZero(const AErrorMargin: Single): Boolean; overload;
function HasSameDirection(const AOther: TVector2): Boolean; inline;
function HasOppositeDirection(const AOther: TVector2): Boolean; inline;
function IsParallel(const AOther: TVector2; const ATolerance: Single = SINGLE_TOLERANCE): Boolean;
function IsCollinear(const AOther: TVector2; const ATolerance: Single = SINGLE_TOLERANCE): Boolean;
function IsCollinearOpposite(const AOther: TVector2; const ATolerance: Single = SINGLE_TOLERANCE): Boolean;
function IsPerpendicular(const AOther: TVector2; const ATolerance: Single = SINGLE_TOLERANCE): Boolean;

Properties

property Components[constAIndex:Integer]: Single read GetComponent write SetComponent;
property Length: Single read GetLength write SetLength;
property LengthSquared: Single read GetLengthSquared write SetLengthSquared;
property Angle: Single read GetAngle write SetAngle;

Description

Fields

X: Single

X and Y components of the vector. Aliases for C[0] and C[1].

Y: Single

X and Y components of the vector. Aliases for C[0] and C[1].

R: Single

Red and Green components of the vector. Aliases for C[0] and C[1].

G: Single

Red and Green components of the vector. Aliases for C[0] and C[1].

S: Single

S and T components of the vector. Aliases for C[0] and C[1].

T: Single

S and T components of the vector. Aliases for C[0] and C[1].

C: array [0..1] of Single

The two components of the vector.

Methods

procedure Init; overload; inline;

Sets the two elements (X and Y) to 0.

procedure Init(const A: Single); overload; inline;

Sets the two elements (X and Y) to A.

Parameters
A
the value to set the two elements to.
procedure Init(const A1, A2: Single); overload; inline;

Sets the two elements (X and Y) to A1 and A2 respectively.

Parameters
A1
the value to set the first element to.
A2
the value to set the second element to.
procedure Init(const APoint: TPoint); overload; inline;

Converts a TPoint to a TVector2.

Parameters
APoint
the TPoint to convert.
class operator Implicit(const A: TPointF): TVector2; inline;

Implicitly converts a TPointF to a TVector2.

class operator Implicit(const A: TVector2): TPointF; inline;

Implicitly converts a TVector2 to a TPointF.

class operator Equal(const A, B: TVector2): Boolean; inline;

Checks two vectors for equality.

Returns

True if the two vectors match each other exactly.

class operator NotEqual(const A, B: TVector2): Boolean; inline;

Checks two vectors for inequality.

Returns

True if the two vectors are not equal.

class operator Negative(const A: TVector2): TVector2; inline;

Negates a vector.

Returns

The negative value of a vector (eg. (-A.X, -A.Y))

class operator Add(const A: TVector2; const B: Single): TVector2; inline;

Adds a scalar value to a vector.

Returns

(A.X + B, A.Y + B)

class operator Add(const A: Single; const B: TVector2): TVector2; inline;

Adds a vector to a scalar value.

Returns

(A + B.X, A + B.Y)

class operator Add(const A, B: TVector2): TVector2; inline;

Adds two vectors.

Returns

(A.X + B.X, A.Y + B.Y)

class operator Subtract(const A: TVector2; const B: Single): TVector2; inline;

Subtracts a scalar value from a vector.

Returns

(A.X - B, A.Y - B)

class operator Subtract(const A: Single; const B: TVector2): TVector2; inline;

Subtracts a vector from a scalar value.

Returns

(A - B.X, A - B.Y)

class operator Subtract(const A, B: TVector2): TVector2; inline;

Subtracts two vectors.

Returns

(A.X - B.X, A.Y - B.Y)

class operator Multiply(const A: TVector2; const B: Single): TVector2; inline;

Multiplies a vector with a scalar value.

Returns

(A.X * B, A.Y * B)

class operator Multiply(const A: Single; const B: TVector2): TVector2; inline;

Multiplies a scalar value with a vector.

Returns

(A * B.X, A * B.Y)

class operator Multiply(const A, B: TVector2): TVector2; inline;

Multiplies two vectors component-wise. To calculate a dot or cross product instead, use the Dot or Cross function.

Returns

(A.X * B.X, A.Y * B.Y)

class operator Divide(const A: TVector2; const B: Single): TVector2; inline;

Divides a vector by a scalar value.

Returns

(A.X / B, A.Y / B)

class operator Divide(const A: Single; const B: TVector2): TVector2;

Divides a scalar value by a vector.

Returns

(A / B.X, A / B.Y)

class operator Divide(const A, B: TVector2): TVector2;

Divides two vectors component-wise.

Returns

(A.X / B.X, A.Y / B.Y)

function Equals(const AOther: TVector2; const ATolerance: Single = SINGLE_TOLERANCE): Boolean; inline;

Whether this vector equals another vector, within a certain tolerance.

Parameters
AOther
the other vector.
ATolerance
(optional) tolerance. If not specified, a small tolerance is used.
Returns

True if both vectors are equal (within the given tolerance).

function Distance(const AOther: TVector2): Single; inline;

Calculates the distance between this vector and another vector.

Note: If you only want to compare distances, you should use DistanceSquared instead, which is faster.

Parameters
AOther
the other vector.
Returns

The distance between this vector and AOther.

function DistanceSquared(const AOther: TVector2): Single; inline;

Calculates the squared distance between this vector and another vector.

Parameters
AOther
the other vector.
Returns

The squared distance between this vector and AOther.

function Dot(const AOther: TVector2): Single;

Calculates the dot product between this vector and another vector.

Parameters
AOther
the other vector.
Returns

The dot product between this vector and AOther.

function Cross(const AOther: TVector2): Single;

Calculates the cross product between this vector and another vector.

Parameters
AOther
the other vector.
Returns

The cross product between this vector and AOther.

procedure Offset(const ADeltaX, ADeltaY: Single); overload; inline;

Offsets this vector in a certain direction.

Parameters
ADeltaX
the delta X direction.
ADeltaY
the delta Y direction.
procedure Offset(const ADelta: TVector2); overload; inline;

Offsets this vector in a certain direction.

Note: this is equivalent to adding two vectors together.

Parameters
ADelta
the delta.
function Normalize: TVector2; inline;

Calculates a normalized version of this vector.

Note: for a faster, less accurate version, use NormalizeFast.

Note: Does not change this vector. To update this vector itself, use SetNormalized.

Returns

The normalized version of of this vector. That is, a vector in the same direction as A, but with a length of 1.

procedure SetNormalized; inline;

Normalizes this vector. This is, keep the current direction, but set the length to 1.

Note: The SIMD optimized versions of this method use an approximation, resulting in a very small error.

Note: If you do not want to change this vector, but get a normalized version instead, then use Normalize.

function NormalizeFast: TVector2;

Calculates a normalized version of this vector.

Note: this is an SIMD optimized version that uses an approximation, resulting in a small error. For an accurate version, use Normalize.

Note: Does not change this vector. To update this vector itself, use SetNormalizedFast.

Returns

The normalized version of of this vector. That is, a vector in the same direction as A, but with a length of 1.

procedure SetNormalizedFast;

Normalizes this vector. This is, keep the current direction, but set the length to 1.

Note: this is an SIMD optimized version that uses an approximation, resulting in a small error. For an accurate version, use SetNormalized.

Note: If you do not want to change this vector, but get a normalized version instead, then use NormalizeFast.

function FaceForward(const I, NRef: TVector2): TVector2; inline;

Calculates a vector pointing in the same direction as this vector.

Parameters
I
the incident vector.
NRef
the reference vector.
Returns

A vector that points away from the surface as defined by its normal. If NRef.Dot(I) < 0 then it returns this vector, otherwise it returns the negative of this vector.

function Reflect(const N: TVector2): TVector2; inline;

Calculates the reflection direction for this (incident) vector.

Parameters
N
the normal vector. Should be normalized in order to achieve the desired result.
Returns

The reflection direction calculated as Self - 2 * N.Dot(Self) * N.

function Refract(const N: TVector2; const Eta: Single): TVector2; inline;

Calculates the refraction direction for this (incident) vector.

Note: This vector should be normalized in order to achieve the desired result.

Parameters
N
the normal vector. Should be normalized in order to achieve the desired result.
Eta
the ratio of indices of refraction.
Returns

The refraction vector.

function Limit(const AMaxLength: Single): TVector2; inline;

Creates a vector with the same direction as this vector, but with the length limited, based on the desired maximum length.

Note: Does not change this vector. To update this vector itself, use SetLimit.

Parameters
AMaxLength
The desired maximum length of the vector.
Returns

A length-limited version of this vector.

procedure SetLimit(const AMaxLength: Single); inline;

Limits the length of this vector, based on the desired maximum length.

Note: If you do not want to change this vector, but get a length-limited version instead, then use Limit.

Parameters
AMaxLength
The desired maximum length of this vector.
function LimitSquared(const AMaxLengthSquared: Single): TVector2;

Creates a vector with the same direction as this vector, but with the length limited, based on the desired squared maximum length.

Note: Does not change this vector. To update this vector itself, use SetLimitSquared.

Parameters
AMaxLengthSquared
The desired squared maximum length of the vector.
Returns

A length-limited version of this vector.

procedure SetLimitSquared(const AMaxLengthSquared: Single); inline;

Limits the length of this vector, based on the desired squared maximum length.

Note: If you do not want to change this vector, but get a length-limited version instead, then use LimitSquared.

Parameters
AMaxLengthSquared
The desired squared maximum length of this vector.
function Clamp(const AMinLength, AMaxLength: Single): TVector2;

Creates a vector with the same direction as this vector, but with the length clamped between a minimim and maximum length.

Note: Does not change this vector. To update this vector itself, use SetClamped.

Parameters
AMinLength
The minimum length of the vector.
AMaxLength
The maximum length of the vector.
Returns

A length-clamped version of this vector.

procedure SetClamped(const AMinLength, AMaxLength: Single); inline;

Clamps the length of this vector between a minimim and maximum length.

Note: If you do not want to change this vector, but get a length-clamped version instead, then use Clamp.

Parameters
AMinLength
The minimum length of this vector.
AMaxLength
The maximum length of this vector.
function Rotate(const ARadians: Single): TVector2;

Creates a vector by rotating this vector counter-clockwise.

AParameters: ARadians: the rotation angle in radians, counter-clockwise assuming the Y-axis points up.

Note: Does not change this vector. To update this vector itself, use SetRotated.

Returns

A rotated version version of this vector.

procedure SetRotated(const ARadians: Single); inline;

Rotates this vector counter-clockwise.

AParameters: ARadians: the rotation angle in radians, counter-clockwise assuming the Y-axis points up.

Note: If you do not want to change this vector, but get a rotated version instead, then use Rotate.

function Rotate90CCW: TVector2;

Creates a vector by rotating this vector 90 degrees counter-clockwise.

Note: Does not change this vector. To update this vector itself, use SetRotated90CCW.

Returns

A rotated version version of this vector.

procedure SetRotated90CCW; inline;

Rotates this vector 90 degrees counter-clockwise.

Note: If you do not want to change this vector, but get a rotated version instead, then use Rotate90CCW.

function Rotate90CW: TVector2;

Creates a vector by rotating this vector 90 degrees clockwise.

Note: Does not change this vector. To update this vector itself, use SetRotated90CW.

Returns

A rotated version version of this vector.

procedure SetRotated90CW; inline;

Rotates this vector 90 degrees clockwise.

Note: If you do not want to change this vector, but get a rotated version instead, then use Rotate90CW.

function AngleTo(const ATarget: TVector2): Single;

Calculates the angle in radians to rotate this vector/point to a target vector. Angles are towards the positive Y-axis (counter-clockwise).

Parameters
ATarget
the target vector.
Returns

The angle in radians to the target vector, in the range -Pi to Pi.

function Lerp(const ATarget: TVector2; const AAlpha: Single): TVector2; inline;

Linearly interpolates between this vector and a target vector.

Note: Does not change this vector. To update this vector itself, use SetLerp.

Parameters
ATarget
the target vector.
AAlpha
the interpolation coefficient (between 0.0 and 1.0).
Returns

The interpolation result vector.

procedure SetLerp(const ATarget: TVector2; const AAlpha: Single); inline;

Linearly interpolates between this vector and a target vector and stores the result in this vector.

Note: If you do not want to change this vector, but get an interpolated version instead, then use Lerp.

Parameters
ATarget
the target vector.
AAlpha
the interpolation coefficient (between 0.0 and 1.0).
function IsNormalized: Boolean; overload; inline;

Whether the vector is normalized (within a small margin of error).

Returns

True if the length of the vector is (very close to) 1.0

function IsNormalized(const AErrorMargin: Single): Boolean; overload;

Whether the vector is normalized within a given margin of error.

Parameters
AErrorMargin
the allowed margin of error.
Returns

True if the squared length of the vector is 1.0 within the margin of error.

function IsZero: Boolean; overload; inline;

Whether this is a zero vector.

Returns

True if X and Y are exactly 0.0

function IsZero(const AErrorMargin: Single): Boolean; overload;

Whether this is a zero vector within a given margin of error.

Parameters
AErrorMargin
the allowed margin of error.
Returns

True if the squared length is smaller then the margin of error.

function HasSameDirection(const AOther: TVector2): Boolean; inline;

Whether this vector has a similar direction compared to another vector.

Parameters
AOther
the other vector.
Returns

True if the normalized dot product is greater than 0.

function HasOppositeDirection(const AOther: TVector2): Boolean; inline;

Whether this vector has an opposite direction compared to another vector.

Parameters
AOther
the other vector.
Returns

True if the normalized dot product is less than 0.

function IsParallel(const AOther: TVector2; const ATolerance: Single = SINGLE_TOLERANCE): Boolean;

Whether this vector runs parallel to another vector (either in the same or the opposite direction).

Note: every vector is considered to run parallel to a zero vector.

Parameters
AOther
the other vector.
ATolerance
(optional) tolerance. If not specified, a small tolerance is used.
Returns

True if this vector runs parallel to AOther (within the given tolerance)

function IsCollinear(const AOther: TVector2; const ATolerance: Single = SINGLE_TOLERANCE): Boolean;

Whether this vector is collinear with another vector. Two vectors are collinear if they run parallel to each other and point in the same direction.

Parameters
AOther
the other vector.
ATolerance
(optional) tolerance. If not specified, a small tolerance is used.
Returns

True if this vector is collinear to AOther (within the given tolerance)

function IsCollinearOpposite(const AOther: TVector2; const ATolerance: Single = SINGLE_TOLERANCE): Boolean;

Whether this vector is opposite collinear with another vector. Two vectors are opposite collinear if they run parallel to each other and point in opposite directions.

Parameters
AOther
the other vector.
ATolerance
(optional) tolerance. If not specified, a small tolerance is used.
Returns

True if this vector is opposite collinear to AOther (within the given tolerance)

function IsPerpendicular(const AOther: TVector2; const ATolerance: Single = SINGLE_TOLERANCE): Boolean;

Whether this vector is perpendicular to another vector.

Parameters
AOther
the other vector.
ATolerance
(optional) tolerance. If not specified, a small tolerance is used.
Returns

True if this vector is perpendicular to AOther. That is, if the dot product is 0 (within the given tolerance)

Properties

property Components[constAIndex:Integer]: Single read GetComponent write SetComponent;

Returns the components of the vector. This is identical to accessing the C-field, but this property can be used as a default array property.

Parameters
AIndex
index of the component to return (0 or 1). Range is checked with an assertion.
property Length: Single read GetLength write SetLength;

The euclidean length of this vector.

Note: If you only want to compare lengths of vectors, you should use LengthSquared instead, which is faster.

Note: You can also set the length of the vector. In that case, it will keep the current direction.

property LengthSquared: Single read GetLengthSquared write SetLengthSquared;

The squared length of the vector.

Note: This property is faster than Length because it avoids calculating a square root. It is useful for comparing lengths instead of calculating actual lengths.

Note: You can also set the squared length of the vector. In that case, it will keep the current direction.

property Angle: Single read GetAngle write SetAngle;

The angle in radians of this vector/point relative to the X-axis. Angles are towards the positive Y-axis (counter-clockwise).

When getting the angle, the result will be between -Pi and Pi.


Generated by PasDocEx, based on PasDoc 0.14.0.