Skip to content

Math/vector implementation#30

Merged
DRossen merged 15 commits intodevelopfrom
math/vector
Sep 15, 2025
Merged

Math/vector implementation#30
DRossen merged 15 commits intodevelopfrom
math/vector

Conversation

@DRossen
Copy link
Copy Markdown
Collaborator

@DRossen DRossen commented Sep 14, 2025

Cleaned up implementation of Vector3 and Vector3 float/int versions.
Also added some tests.

Comment on lines +19 to +23
float arr[3] = { 7.0f, 8.0f, 9.0f };
Vector3 v3(arr);
EXPECT_FLOAT_EQ(v3.x, 7.0f);
EXPECT_FLOAT_EQ(v3.y, 8.0f);
EXPECT_FLOAT_EQ(v3.z, 9.0f);
Copy link
Copy Markdown
Owner

@OlleKReutercrona OlleKReutercrona Sep 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference between v1 and v3 in this case? Array construction 👍
A good habit to have in these cases is to re-use the values from the array as a expected result, especially when dealing with strings.
EXPECT_FLOAT_EQ(v3.x, arr[0]);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, add the other constructors to this test

TEST(Vector2Tests, DotProduct) {
Vector2 a(1.0f, 2.0f);
Vector2 b(3.0f, 4.0f);
EXPECT_FLOAT_EQ(a.Dot(b), 11.0f); // 1*3 + 2*4
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice that's you're explaining the math in the expected result 👍

Vector2 n = v.Normalized();
EXPECT_NEAR(n.Length(), 1.0f, 1e-6f);
EXPECT_NEAR(n.x, 0.6f, 1e-6f);
EXPECT_NEAR(n.y, 0.8f, 1e-6f);
Copy link
Copy Markdown
Owner

@OlleKReutercrona OlleKReutercrona Sep 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1e-6f can be a global constexpr to be shared in the relevant places

Comment on lines +39 to +49
TEST(Vector2Tests, OperatorsArithmetic) {
Vector2 a(2, 3);
Vector2 b(1, -1);

EXPECT_EQ(a + b, Vector2(3, 2));
EXPECT_EQ(a - b, Vector2(1, 4));

EXPECT_EQ(a * 2.0f, Vector2(4, 6));
EXPECT_EQ(2.0f * a, Vector2(4, 6));
EXPECT_EQ(a / 2.0f, Vector2(1, 1.5f));
}
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice but is missing the assignment operators too?
+=
-=
/=
*=

Comment on lines +4 to +9
TEST(Vector2Tests, ConstructorFromArray) {
float arr[2] = { 3.5f, -2.0f };
Vector2 v(arr);
EXPECT_FLOAT_EQ(v.x, 3.5f);
EXPECT_FLOAT_EQ(v.y, -2.0f);
}
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice but you should also test all the other constructors too

Comment on lines +80 to +85
EXPECT_EQ(a + b, Vector2i(3, 2));
EXPECT_EQ(a - b, Vector2i(1, 4));

EXPECT_EQ(a * 2, Vector2i(4, 6));
EXPECT_EQ(2 * a, Vector2i(4, 6));
EXPECT_EQ(a / 2, Vector2i(1, 1));
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is also missing assignment operators

Comment on lines +53 to +64
TEST(Vector3Tests, ScalarMultiplicationDivision) {
Vector3 v(2, -4, 6);
Vector3 scaled = v * 2.0f;
EXPECT_FLOAT_EQ(scaled.x, 4);
EXPECT_FLOAT_EQ(scaled.y, -8);
EXPECT_FLOAT_EQ(scaled.z, 12);

Vector3 divided = scaled / 2.0f;
EXPECT_FLOAT_EQ(divided.x, 2);
EXPECT_FLOAT_EQ(divided.y, -4);
EXPECT_FLOAT_EQ(divided.z, 6);
}
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also make tests to make sure that we catch division by zero. You can check how I implemented "death-tests" in matrix3x3

Comment on lines +137 to +153
TEST(Vector3iTests, ConstructorAndEquality) {
Vector3i v1(1, 2, 3);
EXPECT_EQ(v1.x, 1);
EXPECT_EQ(v1.y, 2);
EXPECT_EQ(v1.z, 3);

Vector3i v2 = Vector3i::Zero;
EXPECT_EQ(v2.x, 0);
EXPECT_EQ(v2.y, 0);
EXPECT_EQ(v2.z, 0);

int arr[3] = { 7, 8, 9 };
Vector3i v(arr);
EXPECT_EQ(v.x, 7);
EXPECT_EQ(v.y, 8);
EXPECT_EQ(v.z, 9);
}
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is missing some constructors

Comment on lines +73 to +93
friend Vector2 operator+(Vector2 a, const Vector2 &b) noexcept {
a += b;
return a;
}
friend Vector2 operator-(Vector2 a, const Vector2 &b) noexcept {
a -= b;
return a;
}
friend Vector2 operator/(Vector2 a, float s) noexcept {
assert(s != 0.0f && "Division by zero in Vector2::operator/");
a /= s;
return a;
}
friend Vector2 operator*(Vector2 a, float s) noexcept {
a *= s;
return a;
}
friend Vector2 operator*(float s, Vector2 a) noexcept {
a *= s;
return a;
}
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for these being friend declared? 🤔

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

friend is to allow them to be defined within the class, to keep them along the other operators, it has no other purpose here

return (*this) * (1.0f / sqrtf(lsq));
}
[[nodiscard]] Vector2 Reflect(const Vector2 &normal) const noexcept {
assert(fabs(normal.LengthSquared() - 1.0f) < 1e-3f && "Vector3::Reflect received non-normalized normal");
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Don't forget to create a test for this too

return *this;
}
Vector2i &operator/=(int s) noexcept {
assert(s != 0 && "Division by zero in Vector2i::operator/=");
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Don't forget to test this

Copy link
Copy Markdown
Owner

@OlleKReutercrona OlleKReutercrona left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really nice, glad to see you start writing test! Added some minor comments here and there but I'd like you to implement the missing tests I suggested before you merge 👍
Good job!

@OlleKReutercrona OlleKReutercrona changed the base branch from main to develop September 14, 2025 20:21
@DRossen
Copy link
Copy Markdown
Collaborator Author

DRossen commented Sep 14, 2025

image

Comment on lines +62 to +65

disablewarnings {
"4723" -- division by zero
}
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really nice!

@DRossen DRossen merged commit 8640fc6 into develop Sep 15, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants