sincerely Singaporean

If you have not done so, read this full tutorial on how to use SGEXTN to build an application.

SGXVector2

see header file

see source file

class SGXVector2;

part of SGEXTN module SG_Core

2D vector with basic geometry and linear algebra operations builtin

detailed description

list of all including inherited members

implementation details

preprocessor file inclusion directive: #include ‹SGXVector2.h›

CMake target for BuildLah: SGEXTN::SG_Core

see this link for more information about BuildLah

parent class: (none)

children classes: (none)

instance member variables

float x;

float y;

instance member functions

SGXVector2(float x, float y);

[[nodiscard]] float getArgument() const;

[[nodiscard]] float getDistanceToCircle(float a, float b, float r) const;

[[nodiscard]] float getDistanceToLine(SGXVector2 a, SGXVector2 b) const;

[[nodiscard]] float getDistanceToSegment(SGXVector2 a, SGXVector2 b) const;

[[nodiscard]] float getMagnitude() const;

[[nodiscard]] float getMagnitudeSquare() const;

[[nodiscard]] SGXVector2 getNearestPointOnCircle(float a, float b, float r) const;

[[nodiscard]] SGXVector2 getNearestPointOnLine(SGXVector2 a, SGXVector2 b) const;

[[nodiscard]] SGXVector2 getNearestPointOnSegment(SGXVector2 a, SGXVector2 b) const;

[[nodiscard]] SGXString getStringForPrinting() const;

[[nodiscard]] int hash() const;

SGXVector2& invert();

SGXVector2& normalise();

SGXVector2& normaliseGivenMagnitude(float m);

[[nodiscard]] bool operator!=(SGXVector2 x) const;

SGXVector2& operator*=(float m);

[[nodiscard]] SGXVector2 operator*(float m) const;

[[nodiscard]] SGXVector2 operator+(SGXVector2 x) const;

SGXVector2& operator+=(SGXVector2 x);

[[nodiscard]] SGXVector2 operator-(SGXVector2 x) const;

SGXVector2& operator-=(SGXVector2 x);

[[nodiscard]] SGXVector2 operator/(float m) const;

SGXVector2& operator/=(float m);

[[nodiscard]] bool operator‹(SGXVector2 x) const;

[[nodiscard]] bool operator==(SGXVector2 x) const;

[[nodiscard]] bool operator›(SGXVector2 x) const;

SGXVector2& projectToLine(SGXVector2 a, SGXVector2 b);

SGXVector2& projectToX();

SGXVector2& projectToY();

SGXVector2& redirectUsingArgument(float a);

SGXVector2& reflectAcrossLine(SGXVector2 a, SGXVector2 b);

SGXVector2& reflectAcrossPoint(SGXVector2 x);

SGXVector2& reflectAcrossX();

SGXVector2& reflectAcrossY();

SGXVector2& rotate180();

SGXVector2& rotateClockwise(float a);

SGXVector2& rotateClockwise90();

SGXVector2& rotateCounterclockwise(float a);

SGXVector2& rotateCounterclockwise90();

static member variables

static const SGXVector2 origin;

static member functions

static float crossProduct(SGXVector2 a, SGXVector2 b);

static float dotProduct(SGXVector2 a, SGXVector2 b);

static float getAngleBetween(SGXVector2 a, SGXVector2 b);

static float getDistance(SGXVector2 a, SGXVector2 b);

static float getDistanceSquare(SGXVector2 a, SGXVector2 b);

static bool isCollinear(SGXVector2 x, SGXVector2 a, SGXVector2 b, float limit);

static bool isParallel(SGXVector2 a1, SGXVector2 a2, SGXVector2 b1, SGXVector2 b2, float limit);

static bool isPerpendicular(SGXVector2 x, SGXVector2 a, SGXVector2 b, float limit);

static SGXVector2 linearInterpolate(SGXVector2 a, SGXVector2 b, float f);

static SGXVector2 midpoint(SGXVector2 a, SGXVector2 b);

Detailed Description

SGXVector2 represents a 2D vector which depending on context can refer to either the vector itself or the endpoint of the vector. Common geometry and linear algebra operations are builtin to SGXVector2. SGEXTN currently has no plans of building a 3D vector as it is not designed for rendering in 3D.

Implementation Details

Stores 2 floating point numbers, 1 per coordinate.

float x;

X coordinate.

float y;

Y coordinate.

SGXVector2(float x, float y);

Creates a SGXVector2 with x coordinate of x and y coordinate of y.

[[nodiscard]] float getArgument() const;

Returns the argument of this SGXVector2 in degrees, that is its signed angle away from the positive x direction.

If the SGXVector2 is the origin, this is not defined, but it will not crash.

[[nodiscard]] float getDistanceToCircle(float a, float b, float r) const;

Returns the distance from this SGXVector2 to the circle centered at (a, b) with radius r.

This is negative if this SGXVector2 is inside the circle.

a and b may be merged into a single SGXVector2 argument in a future version of SGEXTN.

[[nodiscard]] float getDistanceToLine(SGXVector2 a, SGXVector2 b) const;

Returns the distance from this SGXVector2 to the line formed by joining a with b.

[[nodiscard]] float getDistanceToSegment(SGXVector2 a, SGXVector2 b) const;

Returns the distance from this SGXVector2 to the line segment formed by joining a with b.

[[nodiscard]] float getMagnitude() const;

Returns the magnitude of this SGXVector2.

[[nodiscard]] float getMagnitudeSquare() const;

Returns the square of the magnitude of this SGXVector2.

This is slightly faster than SGXVector2::getMagnitude as it does not involve square root computation.

[[nodiscard]] SGXVector2 getNearestPointOnCircle(float a, float b, float r) const;

Returns the nearest point to this SGXVector2 on the circle centered at (a, b) with radius r.

[[nodiscard]] SGXVector2 getNearestPointOnLine(SGXVector2 a, SGXVector2 b) const;

Returns the nearest point to this SGXVector2 on the line formed by joining a with b.

[[nodiscard]] SGXVector2 getNearestPointOnSegment(SGXVector2 a, SGXVector2 b) const;

Returns the nearest point to this SGXVector2 on the line segment formed by joining a with b.

[[nodiscard]] SGXString getStringForPrinting() const;

Returns SGXString representing this SGXVector2 in the format (x, y).

[[nodiscard]] int hash() const;

Hash function for SGLHash.

SGXVector2& invert();

Multiplies this SGXVector2 by -1 and returns a reference to this SGXVector2 to allow operation chaining.

SGXVector2& normalise();

Normalises this SGXVector2, or sets its magnitude to 1 without changing its direction and returns a reference to this SGXVector2 to allow operation chaining.

SGXVector2& normaliseGivenMagnitude(float m);

Sets the magnitude of this SGXVector2 to m without changing its direction and returns a reference to this SGXVector2 to allow operation chaining.

[[nodiscard]] bool operator!=(SGXVector2 x) const;

Returns if this SGXVector2 is different from x.

SGXVector2& operator*=(float m);

Multiplies this SGXVector2 by m and returns a reference to this SGXVector2.

[[nodiscard]] SGXVector2 operator*(float m) const;

Returns (this SGXVector2 * m).

[[nodiscard]] SGXVector2 operator+(SGXVector2 x) const;

Returns (this SGXVector2 + x).

SGXVector2& operator+=(SGXVector2 x);

Adds x to this SGXVector2 and returns a reference to this SGXVector2.

[[nodiscard]] SGXVector2 operator-(SGXVector2 x) const;

Returns (this SGXVector2 - x).

SGXVector2& operator-=(SGXVector2 x);

Subtracts x from this SGXVector2 and returns a reference to this SGXVector2.

[[nodiscard]] SGXVector2 operator/(float m) const;

Returns (this SGXVector2 / m).

Division by 0 here follows floating point numbers division by 0 rules.

SGXVector2& operator/=(float m);

Divides this SGXVector2 by m and returns a reference to this SGXVector2.

[[nodiscard]] bool operator‹(SGXVector2 x) const;

Lesser than operator with x for SGLLesserThan and SGLMoreThan.

[[nodiscard]] bool operator==(SGXVector2 x) const;

Returns if this SGXVector2 is the same as x.

[[nodiscard]] bool operator›(SGXVector2 x) const;

More than operator with x for SGLLesserThan and SGLMoreThan.

SGXVector2& projectToLine(SGXVector2 a, SGXVector2 b);

Projects this SGXVector2 onto the line formed by joining a and b and returns a reference to this SGXVector2 to allow operation chaining.

SGXVector2& projectToX();

Projects this SGXVector2 onto the x axis and returns a reference to this SGXVector2 to allow operation chaining.

This is equivalent to setting its y coordinate to 0.

SGXVector2& projectToY();

Projects this SGXVector2 onto the y axis and returns a reference to this SGXVector2 to allow operation chaining.

This is equivalent to setting its x coordinate to 0.

SGXVector2& redirectUsingArgument(float a);

Sets the argument of this SGXVector2 to a degrees without changing its magnitude and returns a reference to this SGXVector2 to allow operation chaining.

SGXVector2& reflectAcrossLine(SGXVector2 a, SGXVector2 b);

Reflects this SGXVector2 across the line formed by joining a with b and returns a reference to this SGXVector2 to allow operation chaining.

SGXVector2& reflectAcrossPoint(SGXVector2 x);

Reflects this SGXVector2 across x and returns a reference to this SGXVector2 to allow operation chaining.

SGXVector2& reflectAcrossX();

Reflect this SGXVector2 across the x axis and returns a reference to this SGXVector2 to allow operation chaining.

This is equivalent to multiplying its y coordinate by -1.

SGXVector2& reflectAcrossY();

Reflect this SGXVector2 across the y axis and returns a reference to this SGXVector2 to allow operation chaining.

This is equivalent to multiplying its x coordinate by -1.

SGXVector2& rotate180();

Rotates this SGXVector2 by 180 degrees in either direction and returns a reference to this SGXVector2 to allow operation chaining.

This is equivalent to multiplying the SGXVector2 by -1.

SGXVector2& rotateClockwise(float a);

Rotates this SGXVector2 by a degrees clockwise and returns a reference to this SGXVector2 to allow operation chaining.

SGXVector2& rotateClockwise90();

Rotates this SGXVector2 clockwise by 90 degrees and returns a reference to this SGXVector2 to allow operation chaining.

SGXVector2& rotateCounterclockwise(float a);

Rotates this SGXVector2 by a degrees counterclockwise and returns a reference to this SGXVector2 to allow operation chaining.

SGXVector2& rotateCounterclockwise90();

Rotates this SGXVector2 counterclockwise by 90 degrees and returns a reference to this SGXVector2 to allow operation chaining.

static const SGXVector2 origin;

The origin, which is (0, 0).

static float crossProduct(SGXVector2 a, SGXVector2 b);

Returns the cross product of a with b.

static float dotProduct(SGXVector2 a, SGXVector2 b);

Returns the dot product of a with b.

static float getAngleBetween(SGXVector2 a, SGXVector2 b);

Returns the angle between a and b in degrees.

static float getDistance(SGXVector2 a, SGXVector2 b);

Returns the distance from a to b.

static float getDistanceSquare(SGXVector2 a, SGXVector2 b);

Returns the squared distance from a to b.

This is slightly faster than SGXVector2::getDistance as it does not involve square root computation.

static bool isCollinear(SGXVector2 x, SGXVector2 a, SGXVector2 b, float limit);

Returns if x, a, and b are (almost) collinear with angle AXB at most limit degrees away from 0 or 180.

If any 2 points overlap, the behaviour is not defined but it will not crash.

static bool isParallel(SGXVector2 a1, SGXVector2 a2, SGXVector2 b1, SGXVector2 b2, float limit);

Returns if the line formed by joining a1 with a2 and the line formed by joining b1 with b2 are (almost) parallel with the angle formed between them at most limit degrees.

If any 2 points overlap, the behaviour is not defined but it will not crash.

static bool isPerpendicular(SGXVector2 x, SGXVector2 a, SGXVector2 b, float limit);

Returns if the line formed by joining this x with a is (almost) perpendicular with the line formed by joining x with b with angle AXB at most limit degrees away from 90.

If any 2 points overlap, the behaviour is not defined but it will not crash.

static SGXVector2 linearInterpolate(SGXVector2 a, SGXVector2 b, float f);

Returns f * a + (1 - f) * b.

It is ok for f to be below 0 or above 1.

static SGXVector2 midpoint(SGXVector2 a, SGXVector2 b);

Returns the midpoint of a and b.

©2025 05524F.sg (Singapore)

contact 05524F / report a bug / make a suggestion

about 05524F SINGAPORE values

list of 05524F projects