record TVector4

DescriptionHierarchyFieldsMethodsProperties

Unit

Declaration

type TVector4 = record

Description

A 4-dimensional vector. Can be used for a variety of purposes:

  • To represent points or vectors in 4D space, using the X, Y, Z and W fields.

  • To represent colors with alpha channel information (using the R, G, B and A fields, which are just aliases for X, Y, Z and W)

  • To represent texture coordinates (S, T, P and Q, again just aliases for X, Y, Z and W).

  • To group 4 arbitrary values together in an array (C[0]..C[3], also just aliases for X, Y, Z and W)

TVector4 is compatible with TVector3D in the Delphi RTL. You can typecast between these two types or implicitly convert from one to the other through assignment (eg. MyVector4 := MyPoint3D).

Overview

Fields

X: Single
Y: Single
Z: Single
W: Single
R: Single
G: Single
B: Single
A: Single
S: Single
T: Single
P: Single
Q: Single
C: array [0..3] of Single

Methods

procedure Init; overload; inline;
procedure Init(const A: Single); overload; inline;
procedure Init(const A1, A2, A3, A4: Single); overload; inline;
procedure Init(const A1: TVector2; const A2, A3: Single); overload; inline;
procedure Init(const A1: Single; const A2: TVector2; const A3: Single); overload; inline;
procedure Init(const A1, A2: Single; const A3: TVector2); overload; inline;
procedure Init(const A1, A2: TVector2); overload; inline;
procedure Init(const A1: TVector3; const A2: Single); overload; inline;
procedure Init(const A1: Single; const A2: TVector3); overload; inline;
class operator Implicit(const A: TVector3D): TVector4; inline;
class operator Implicit(const A: TVector4): TVector3D; inline;
class operator Equal(const A, B: TVector4): Boolean; inline;
class operator NotEqual(const A, B: TVector4): Boolean; inline;
class operator Negative(const A: TVector4): TVector4;
class operator Add(const A: TVector4; const B: Single): TVector4;
class operator Add(const A: Single; const B: TVector4): TVector4;
class operator Add(const A, B: TVector4): TVector4;
class operator Subtract(const A: TVector4; const B: Single): TVector4;
class operator Subtract(const A: Single; const B: TVector4): TVector4;
class operator Subtract(const A, B: TVector4): TVector4;
class operator Multiply(const A: TVector4; const B: Single): TVector4;
class operator Multiply(const A: Single; const B: TVector4): TVector4;
class operator Multiply(const A, B: TVector4): TVector4;
class operator Divide(const A: TVector4; const B: Single): TVector4;
class operator Divide(const A: Single; const B: TVector4): TVector4;
class operator Divide(const A, B: TVector4): TVector4;
function Equals(const AOther: TVector4; const ATolerance: Single = SINGLE_TOLERANCE): Boolean; inline;
function Distance(const AOther: TVector4): Single;
function DistanceSquared(const AOther: TVector4): Single;
function Dot(const AOther: TVector4): Single; inline;
procedure Offset(const ADeltaX, ADeltaY, ADeltaZ, ADeltaW: Single); overload; inline;
procedure Offset(const ADelta: TVector4); overload; inline;
function Normalize: TVector4; inline;
procedure SetNormalized; inline;
function NormalizeFast: TVector4;
procedure SetNormalizedFast;
function FaceForward(const I, NRef: TVector4): TVector4;
function Reflect(const N: TVector4): TVector4;
function Refract(const N: TVector4; const Eta: Single): TVector4;
function Limit(const AMaxLength: Single): TVector4; inline;
procedure SetLimit(const AMaxLength: Single); inline;
function LimitSquared(const AMaxLengthSquared: Single): TVector4;
procedure SetLimitSquared(const AMaxLengthSquared: Single);
function Clamp(const AMinLength, AMaxLength: Single): TVector4;
procedure SetClamped(const AMinLength, AMaxLength: Single);
function Lerp(const ATarget: TVector4; const AAlpha: Single): TVector4; inline;
procedure SetLerp(const ATarget: TVector4; 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: TVector4): Boolean; inline;
function HasOppositeDirection(const AOther: TVector4): Boolean; inline;
function IsParallel(const AOther: TVector4; const ATolerance: Single = SINGLE_TOLERANCE): Boolean;
function IsCollinear(const AOther: TVector4; const ATolerance: Single = SINGLE_TOLERANCE): Boolean;
function IsCollinearOpposite(const AOther: TVector4; const ATolerance: Single = SINGLE_TOLERANCE): Boolean;
function IsPerpendicular(const AOther: TVector4; 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;

Description

Fields

X: Single

X, Y, Z and W components of the vector. Aliases for C[0], C[1], C[2] and C[3].

Y: Single

X, Y, Z and W components of the vector. Aliases for C[0], C[1], C[2] and C[3].

Z: Single

X, Y, Z and W components of the vector. Aliases for C[0], C[1], C[2] and C[3].

W: Single

X, Y, Z and W components of the vector. Aliases for C[0], C[1], C[2] and C[3].

R: Single

Red, Green, Blue and Alpha components of the vector. Aliases for C[0], C[1], C[2] and C[3].

G: Single

Red, Green, Blue and Alpha components of the vector. Aliases for C[0], C[1], C[2] and C[3].

B: Single

Red, Green, Blue and Alpha components of the vector. Aliases for C[0], C[1], C[2] and C[3].

A: Single

Red, Green, Blue and Alpha components of the vector. Aliases for C[0], C[1], C[2] and C[3].

S: Single

S, T, P and Q components of the vector. Aliases for C[0], C[1], C[2] and C[3].

T: Single

S, T, P and Q components of the vector. Aliases for C[0], C[1], C[2] and C[3].

P: Single

S, T, P and Q components of the vector. Aliases for C[0], C[1], C[2] and C[3].

Q: Single

S, T, P and Q components of the vector. Aliases for C[0], C[1], C[2] and C[3].

C: array [0..3] of Single

The four components of the vector.

Methods

procedure Init; overload; inline;

Sets the four elements (X, Y, Z and W) to 0.

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

Sets the four elements (X, Y, Z and W) to A.

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

Sets the four elements (X, Y, Z and W) to A1, A2, A3 and A4 respectively.

Parameters
A1
the value to set the first element to.
A2
the value to set the second element to.
A3
the value to set the third element to.
A4
the value to set the fourth element to.
procedure Init(const A1: TVector2; const A2, A3: Single); overload; inline;

Sets the first two elements from a 2D vector, and the last two elements from two scalars.

Parameters
A1
the vector to use for the first two elements.
A2
the value to set the third element to.
A3
the value to set the fourth element to.
procedure Init(const A1: Single; const A2: TVector2; const A3: Single); overload; inline;

Sets the first and last elements from two scalars, and the middle two elements from a 2D vector.

Parameters
A1
the value to set the first element to.
A2
the vector to use for the second and third elements.
A3
the value to set the fourth element to.
procedure Init(const A1, A2: Single; const A3: TVector2); overload; inline;

Sets the first two elements from two scalars and the last two elements from a 2D vector.

Parameters
A1
the value to set the first element to.
A2
the value to set the second element to.
A3
the vector to use for the last two elements.
procedure Init(const A1, A2: TVector2); overload; inline;

Sets the first two elements and last two elements from two 2D vectors.

Parameters
A1
the vector to use for the first two elements.
A2
the vector to use for the last two elements.
procedure Init(const A1: TVector3; const A2: Single); overload; inline;

Sets the first three elements from a 3D vector, and the fourth element from a scalar.

Parameters
A1
the vector to use for the first three elements.
A2
the value to set the fourth element to.
procedure Init(const A1: Single; const A2: TVector3); overload; inline;

Sets the first element from a scaler, and the last three elements from a 3D vector.

Parameters
A1
the value to set the first element to.
A2
the vector to use for the last three elements.
class operator Implicit(const A: TVector3D): TVector4; inline;

Implicitly converts a TVector3D to a TVector4.

class operator Implicit(const A: TVector4): TVector3D; inline;

Implicitly converts a TVector4 to a TVector3D.

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

Checks two vectors for equality.

Returns

True if the two vectors match each other exactly.

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

Checks two vectors for inequality.

Returns

True if the two vectors are not equal.

class operator Negative(const A: TVector4): TVector4;

Negates a vector.

Returns

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

class operator Add(const A: TVector4; const B: Single): TVector4;

Adds a scalar value to a vector.

Returns

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

class operator Add(const A: Single; const B: TVector4): TVector4;

Adds a vector to a scalar value.

Returns

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

class operator Add(const A, B: TVector4): TVector4;

Adds two vectors.

Returns

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

class operator Subtract(const A: TVector4; const B: Single): TVector4;

Subtracts a scalar value from a vector.

Returns

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

class operator Subtract(const A: Single; const B: TVector4): TVector4;

Subtracts a vector from a scalar value.

Returns

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

class operator Subtract(const A, B: TVector4): TVector4;

Subtracts two vectors.

Returns

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

class operator Multiply(const A: TVector4; const B: Single): TVector4;

Multiplies a vector with a scalar value.

Returns

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

class operator Multiply(const A: Single; const B: TVector4): TVector4;

Multiplies a scalar value with a vector.

Returns

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

class operator Multiply(const A, B: TVector4): TVector4;

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

Returns

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

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

Divides a vector by a scalar value.

Returns

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

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

Divides a scalar value by a vector.

Returns

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

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

Divides two vectors component-wise.

Returns

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

function Equals(const AOther: TVector4; 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: TVector4): Single;

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: TVector4): Single;

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: TVector4): Single; inline;

Calculates the dot product between this vector and another vector.

Parameters
AOther
the other vector.
Returns

The dot product between this vector and AOther.

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

Offsets this vector in a certain direction.

Parameters
ADeltaX
the delta X direction.
ADeltaY
the delta Y direction.
ADeltaZ
the delta Z direction.
ADeltaW
the delta W direction.
procedure Offset(const ADelta: TVector4); overload; inline;

Offsets this vector in a certain direction.

Note: this is equivalent to adding two vectors together.

Parameters
ADelta
the delta.
function Normalize: TVector4; 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: TVector4;

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: TVector4): TVector4;

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: TVector4): TVector4;

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: TVector4; const Eta: Single): TVector4;

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): TVector4; 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): TVector4;

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);

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): TVector4;

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);

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 Lerp(const ATarget: TVector4; const AAlpha: Single): TVector4; 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: TVector4; 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, Y, Z and W 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: TVector4): Boolean; inline;

Whether this vector has a similar direction compared to another vector. The test is performed in 3 dimensions (that is, the W-component is ignored).

Parameters
AOther
the other vector.
Returns

True if the normalized dot product (ignoring the W-component) is greater than 0.

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

Whether this vector has an opposite direction compared to another vector. The test is performed in 3 dimensions (that is, the W-component is ignored).

Parameters
AOther
the other vector.
Returns

True if the normalized dot product (ignoring the W-component) is less than 0.

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

Whether this vector runs parallel to another vector (either in the same or the opposite direction). The test is performed in 3 dimensions (that is, the W-component is ignored).

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 and ignoring the W-component)

function IsCollinear(const AOther: TVector4; 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. The test is performed in 3 dimensions (that is, the W-component is ignored).

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 and ignoring the W-component)

function IsCollinearOpposite(const AOther: TVector4; 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. The test is performed in 3 dimensions (that is, the W-component is ignored).

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 and ignoring the W-component)

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

Whether this vector is perpendicular to another vector. The test is performed in 3 dimensions (that is, the W-component is ignored).

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 and ignoring the W-component)

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-3). 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.


Generated by PasDocEx, based on PasDoc 0.14.0.