Skip to content

Commit

Permalink
[Issue #71] Update vector2 fuctions from vector3 (#130)
Browse files Browse the repository at this point in the history
Signed-off-by: pxalcantara <[email protected]>
  • Loading branch information
pxalcantara authored Jul 7, 2020
1 parent fdbd226 commit 0c34c7e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
16 changes: 16 additions & 0 deletions include/ignition/math/Vector2.hh
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ namespace ignition
/// \brief Destructor
public: virtual ~Vector2() {}

/// \brief Return the sum of the values
/// \return the sum
public: T Sum() const
{
return this->data[0] + this->data[1];
}

/// \brief Calc distance to the given point
/// \param[in] _pt The point to measure to
/// \return the distance
Expand Down Expand Up @@ -101,6 +108,15 @@ namespace ignition
}
}

/// \brief Returns a normalized vector
/// \return unit length vector
public: Vector2 Normalized() const
{
Vector2<T> result = *this;
result.Normalize();
return result;
}

/// \brief Set the contents of the vector
/// \param[in] _x value along x
/// \param[in] _y value along y
Expand Down
32 changes: 32 additions & 0 deletions src/Vector2_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,38 @@ TEST(Vector2Test, Vector2)
EXPECT_DOUBLE_EQ(7, v[1]);
}

/////////////////////////////////////////////////
TEST(Vector2Test, TestSum)
{
math::Vector2 vec1(0, 0);
math::Vector2 vec2(1.0, 2.5);
math::Vector2 vec3(-2, -4);

int sum1 = vec1.Sum();
float sum2 = vec2.Sum();
int sum3 = vec3.Sum();

EXPECT_EQ(sum1, 0);
EXPECT_FLOAT_EQ(sum2, 3.5);
EXPECT_EQ(sum3, -6);
}

/////////////////////////////////////////////////
TEST(Vector2Test, TestNormalized)
{
math::Vector2d vec1(0, 0);
math::Vector2d vec2(1, 2);

math::Vector2d vec3 = vec1.Normalized();

// O zero vector should be equal to the normalized vector
EXPECT_EQ(vec1, vec3);
EXPECT_NE(vec2, vec3);

vec3 = vec2.Normalized();
EXPECT_EQ(vec3, math::Vector2d(0.447213, 0.894427));
}

/////////////////////////////////////////////////
TEST(Vector2Test, NoException)
{
Expand Down

0 comments on commit 0c34c7e

Please sign in to comment.