Skip to content

Commit

Permalink
Create Round(), Rounded() functions for Vector2.hh
Browse files Browse the repository at this point in the history
- Round(): Rounds to nearest whole number inplace
- Rounded(): Returns a rounded version of the vector

Created unit tests for both functions.

colcon build --merge-install [PASSED]
colcon test --merge-install [PASSED]

Intend to solve issue ignitionrobotics#71

Signed-off-by: akshatpandya <[email protected]>
  • Loading branch information
akshatpandya committed Oct 17, 2020
1 parent dbcd671 commit ed89d37
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
18 changes: 18 additions & 0 deletions include/ignition/math/Vector2.hh
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,24 @@ namespace ignition
return result;
}

/// \brief Round to near whole number, return the result.
/// \return the result
public: Vector2 Round()
{
this->data[0] = nearbyint(this->data[0]);
this->data[1] = nearbyint(this->data[1]);
return *this;
}

/// \brief Get a rounded version of this vector
/// \return a rounded vector
public: Vector2 Rounded() const
{
Vector2<T> result = *this;
result.Round();
return result;
}

/// \brief Set the contents of the vector
/// \param[in] _x value along x
/// \param[in] _y value along y
Expand Down
8 changes: 8 additions & 0 deletions src/Vector2_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ TEST(Vector2Test, Vector2)
v.Normalize();
EXPECT_TRUE(v == math::Vector2d(0.447214, 0.894427));

// ::Rounded
v.Set(3.55, 8.49);
EXPECT_TRUE(v.Rounded() == math::Vector2d(4, 8));

// ::Round
v.Round();
EXPECT_TRUE(v == math::Vector2d(4, 8));

// ::Set
v.Set(4, 5);
EXPECT_TRUE(v == math::Vector2d(4, 5));
Expand Down

0 comments on commit ed89d37

Please sign in to comment.