Skip to content

Commit

Permalink
Merge pull request #248 from dbrats/vector_constness
Browse files Browse the repository at this point in the history
Fix const correctness for Vector2, Vector3, and Vector4
  • Loading branch information
RobLoach authored Sep 3, 2023
2 parents 4fba82c + 9e8b448 commit 1e8b821
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 31 deletions.
18 changes: 9 additions & 9 deletions include/Vector2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ class Vector2 : public ::Vector2 {
/**
* Determine whether or not the vectors are equal.
*/
bool operator==(const ::Vector2& other) {
bool operator==(const ::Vector2& other) const {
return x == other.x
&& y == other.y;
}

/**
* Determines if the vectors are not equal.
*/
bool operator!=(const ::Vector2& other) {
bool operator!=(const ::Vector2& other) const {
return !(*this == other);
}

Expand Down Expand Up @@ -211,7 +211,7 @@ class Vector2 : public ::Vector2 {
/**
* Transforms a Vector2 by a given Matrix
*/
inline Vector2 Transform(::Matrix mat) {
inline Vector2 Transform(::Matrix mat) const {
return ::Vector2Transform(*this, mat);
}

Expand Down Expand Up @@ -246,28 +246,28 @@ class Vector2 : public ::Vector2 {
/**
* Invert the given vector
*/
inline Vector2 Invert() {
inline Vector2 Invert() const {
return ::Vector2Invert(*this);
}

/**
* Clamp the components of the vector between
*/
inline Vector2 Clamp(::Vector2 min, ::Vector2 max) {
inline Vector2 Clamp(::Vector2 min, ::Vector2 max) const {
return ::Vector2Clamp(*this, min, max);
}

/**
* // Clamp the magnitude of the vector between two min and max values
*/
inline Vector2 Clamp(float min, float max) {
inline Vector2 Clamp(float min, float max) const {
return ::Vector2ClampValue(*this, min, max);
}

/**
* Check whether two given vectors are almost equal
*/
inline int Equals(::Vector2 q) {
inline int Equals(::Vector2 q) const {
return ::Vector2Equals(*this, q);
}

Expand Down Expand Up @@ -302,7 +302,7 @@ class Vector2 : public ::Vector2 {
/**
* Calculate square distance between two vectors
*/
inline float DistanceSqr(::Vector2 v2) {
inline float DistanceSqr(::Vector2 v2) const {
return ::Vector2DistanceSqr(*this, v2);
}

Expand Down Expand Up @@ -418,7 +418,7 @@ class Vector2 : public ::Vector2 {
/**
* Check if point belongs to line created between two points [p1] and [p2] with defined margin in pixels [threshold]
*/
inline bool CheckCollisionPointLine(::Vector2 p1, ::Vector2 p2, int threshold = 1) {
inline bool CheckCollisionPointLine(::Vector2 p1, ::Vector2 p2, int threshold = 1) const {
return ::CheckCollisionPointLine(*this, p1, p2, threshold);
}

Expand Down
28 changes: 14 additions & 14 deletions include/Vector3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,28 @@ class Vector3 : public ::Vector3 {
return *this;
}

bool operator==(const ::Vector3& other) {
bool operator==(const ::Vector3& other) const {
return x == other.x
&& y == other.y
&& z == other.z;
}

bool operator!=(const ::Vector3& other) {
bool operator!=(const ::Vector3& other) const {
return !(*this == other);
}

#ifndef RAYLIB_CPP_NO_MATH
/**
* Add two vectors
*/
inline Vector3 Add(const ::Vector3& vector3) {
inline Vector3 Add(const ::Vector3& vector3) const {
return Vector3Add(*this, vector3);
}

/**
* Add two vectors
*/
inline Vector3 operator+(const ::Vector3& vector3) {
inline Vector3 operator+(const ::Vector3& vector3) const {
return Vector3Add(*this, vector3);
}

Expand All @@ -69,14 +69,14 @@ class Vector3 : public ::Vector3 {
/**
* Subtract two vectors.
*/
inline Vector3 Subtract(const ::Vector3& vector3) {
inline Vector3 Subtract(const ::Vector3& vector3) const {
return Vector3Subtract(*this, vector3);
}

/**
* Subtract two vectors.
*/
inline Vector3 operator-(const ::Vector3& vector3) {
inline Vector3 operator-(const ::Vector3& vector3) const {
return Vector3Subtract(*this, vector3);
}

Expand All @@ -89,14 +89,14 @@ class Vector3 : public ::Vector3 {
/**
* Negate provided vector (invert direction)
*/
inline Vector3 Negate() {
inline Vector3 Negate() const {
return Vector3Negate(*this);
}

/**
* Negate provided vector (invert direction)
*/
inline Vector3 operator-() {
inline Vector3 operator-() const {
return Vector3Negate(*this);
}

Expand Down Expand Up @@ -207,7 +207,7 @@ class Vector3 : public ::Vector3 {
return Vector3Normalize(*this);
}

inline float DotProduct(const ::Vector3& vector3) {
inline float DotProduct(const ::Vector3& vector3) const {
return Vector3DotProduct(*this, vector3);
}

Expand Down Expand Up @@ -235,23 +235,23 @@ class Vector3 : public ::Vector3 {
return Vector3Transform(*this, matrix);
}

inline Vector3 RotateByQuaternion(const ::Quaternion& quaternion) {
inline Vector3 RotateByQuaternion(const ::Quaternion& quaternion) const {
return Vector3RotateByQuaternion(*this, quaternion);
}

inline Vector3 Reflect(const ::Vector3& normal) const {
return Vector3Reflect(*this, normal);
}

inline Vector3 Min(const ::Vector3& vector3) {
inline Vector3 Min(const ::Vector3& vector3) const {
return Vector3Min(*this, vector3);
}

inline Vector3 Max(const ::Vector3& vector3) {
inline Vector3 Max(const ::Vector3& vector3) const {
return Vector3Max(*this, vector3);
}

inline Vector3 Barycenter(const ::Vector3& a, const ::Vector3& b, const ::Vector3& c) {
inline Vector3 Barycenter(const ::Vector3& a, const ::Vector3& b, const ::Vector3& c) const {
return Vector3Barycenter(*this, a, b, c);
}

Expand Down Expand Up @@ -325,7 +325,7 @@ class Vector3 : public ::Vector3 {
/**
* Detect collision between two spheres
*/
inline bool CheckCollision(float radius1, const ::Vector3& center2, float radius2) {
inline bool CheckCollision(float radius1, const ::Vector3& center2, float radius2) const {
return CheckCollisionSpheres(*this, radius1, center2, radius2);
}

Expand Down
16 changes: 8 additions & 8 deletions include/Vector4.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@ class Vector4 : public ::Vector4 {
return *this;
}

bool operator==(const ::Vector4& other) {
bool operator==(const ::Vector4& other) const {
return x == other.x
&& y == other.y
&& z == other.z
&& w == other.w;
}

bool operator!=(const ::Vector4& other) {
bool operator!=(const ::Vector4& other) const {
return !(*this == other);
}

inline ::Rectangle ToRectangle() {
inline ::Rectangle ToRectangle() const {
return {x, y, z, w};
}

Expand Down Expand Up @@ -95,22 +95,22 @@ class Vector4 : public ::Vector4 {
return QuaternionInvert(*this);
}

inline void ToAxisAngle(::Vector3 *outAxis, float *outAngle) {
inline void ToAxisAngle(::Vector3 *outAxis, float *outAngle) const {
QuaternionToAxisAngle(*this, outAxis, outAngle);
}

/**
* Get the rotation angle and axis for a given quaternion
*/
std::pair<Vector3, float> ToAxisAngle() {
std::pair<Vector3, float> ToAxisAngle() const {
Vector3 outAxis;
float outAngle;
QuaternionToAxisAngle(*this, &outAxis, &outAngle);

return std::pair<Vector3, float>(outAxis, outAngle);
}

inline Vector4 Transform(const ::Matrix& matrix) {
inline Vector4 Transform(const ::Matrix& matrix) const {
return ::QuaternionTransform(*this, matrix);
}

Expand Down Expand Up @@ -138,7 +138,7 @@ class Vector4 : public ::Vector4 {
return ::QuaternionFromEuler(vector3.x, vector3.y, vector3.z);
}

inline Vector3 ToEuler() {
inline Vector3 ToEuler() const {
return ::QuaternionToEuler(*this);
}
#endif
Expand All @@ -147,7 +147,7 @@ class Vector4 : public ::Vector4 {
return ::ColorFromNormalized(*this);
}

operator Color() {
operator Color() const {
return ColorFromNormalized();
}

Expand Down

0 comments on commit 1e8b821

Please sign in to comment.