Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Max and Min function to Vector2.hh #133

Merged
merged 9 commits into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions include/ignition/math/Vector2.hh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#ifndef IGNITION_MATH_VECTOR2_HH_
#define IGNITION_MATH_VECTOR2_HH_

#include <algorithm>

#include <ignition/math/Helpers.hh>
#include <ignition/math/config.hh>

Expand Down Expand Up @@ -167,6 +169,38 @@ namespace ignition
this->data[1] = 0;
}

/// \brief Set this vector's components to the maximum of itself and the
/// passed in vector
/// \param[in] _v the maximum clamping vector
public: void Max(const Vector2<T> &_v)
{
this->data[0] = std::max(_v[0], this->data[0]);
this->data[1] = std::max(_v[1], this->data[1]);
}

/// \brief Set this vector's components to the minimum of itself and the
/// passed in vector
/// \param[in] _v the minimum clamping vector
public: void Min(const Vector2<T> &_v)
{
this->data[0] = std::min(_v[0], this->data[0]);
this->data[1] = std::min(_v[1], this->data[1]);
}

/// \brief Get the maximum value in the vector
/// \return the maximum element
public: T Max() const
{
return std::max(this->data[0], this->data[1]);
}

/// \brief Get the minimum value in the vector
/// \return the minimum element
public: T Min() const
{
return std::min(this->data[0], this->data[1]);
}

/// \brief Assignment operator
/// \param[in] _v a value for x and y element
/// \return this
Expand Down
4 changes: 4 additions & 0 deletions src/Vector2.i
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ namespace ignition
public: void Normalize();
public: void Set(T _x, T _y);
public: T Dot(const Vector2<T> &_v) const;
public: void Max(const Vector2<T> &_v);
public: void Min(const Vector2<T> &_v);
public: T Max() const;
public: T Min() const;
public: Vector2 operator+(const Vector2 &_v) const;
public: inline Vector2<T> operator+(const T _s) const;
public: inline Vector2 operator-() const;
Expand Down
34 changes: 34 additions & 0 deletions src/Vector2_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,40 @@ TEST(Vector2Test, TestNormalized)
EXPECT_EQ(vec3, math::Vector2d(0.447213, 0.894427));
}

/////////////////////////////////////////////////
TEST(Vector2Test, Max)
{
math::Vector2d vec1(0.1, 0.2);
math::Vector2d vec2(0.3, 0.5);
math::Vector2d vec3(0.4, 0.2);

EXPECT_DOUBLE_EQ(vec1.Max(), 0.2);
EXPECT_DOUBLE_EQ(vec3.Max(), 0.4);

vec1.Max(vec2);
EXPECT_EQ(vec1, math::Vector2d(0.3, 0.5));

vec1.Max(vec3);
EXPECT_EQ(vec1, math::Vector2d(0.4, 0.5));
}

/////////////////////////////////////////////////
TEST(Vector2Test, Min)
{
math::Vector2d vec1(0.3, 0.5);
math::Vector2d vec2(0.1, 0.2);
math::Vector2d vec3(0.05, 0.1);

EXPECT_DOUBLE_EQ(vec1.Min(), 0.3);
EXPECT_DOUBLE_EQ(vec3.Min(), 0.05);

vec1.Min(vec2);
EXPECT_EQ(vec1, math::Vector2d(0.1, 0.2));

vec1.Min(vec3);
EXPECT_EQ(vec1, math::Vector2d(0.05, 0.1));
}

/////////////////////////////////////////////////
TEST(Vector2Test, NoException)
{
Expand Down
34 changes: 34 additions & 0 deletions src/Vector2_TEST.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,40 @@ def test_equal_tolerance
"Zero should equal 1 with 1.1 tolerance")
end

def test_max
vec1 = Ignition::Math::Vector2d.new(0.1, 0.2)
vec2 = Ignition::Math::Vector2d.new(0.3, 0.5)
vec3 = Ignition::Math::Vector2d.new(0.4, 0.2)

assert((vec1.Max() - 0.2).abs() < 1e-10,
"Vector2 vec1.Max should equal 0.2")

vec1.Max(vec2)
assert(vec1 == Ignition::Math::Vector2d.new(0.3, 0.5),
"Vector2 vec1 should equal [0.3, 0.5]")

vec1.Max(vec3)
assert(vec1 == Ignition::Math::Vector2d.new(0.4, 0.5),
"Vector2 vec1 should equal [0.4, 0.5]")
end

def test_min
vec1 = Ignition::Math::Vector2d.new(0.3, 0.5)
vec2 = Ignition::Math::Vector2d.new(0.1, 0.2)
vec3 = Ignition::Math::Vector2d.new(0.05, 0.1)

assert((vec1.Min() - 0.3).abs() < 1e-10,
"Vector2 vec1.Min should equal 0.3")

vec1.Min(vec2)
assert(vec1 == Ignition::Math::Vector2d.new(0.1, 0.2),
"Vector2 vec1 should equal [0.1, 0.2]")

vec1.Max(vec3)
assert(vec1 == Ignition::Math::Vector2d.new(0.05, 0.1),
"Vector2 vec1 should equal [0.05, 0.1]")
end

def test_dot
v = Ignition::Math::Vector2d.new(1, 2)

Expand Down