2014-08-26 15:52:37 +08:00
|
|
|
#include <SFML/System/Vector2.hpp>
|
|
|
|
|
2021-12-29 13:03:15 +08:00
|
|
|
#include <doctest.h>
|
|
|
|
|
2022-07-05 00:20:58 +08:00
|
|
|
#include <SystemUtil.hpp>
|
|
|
|
#include <cmath>
|
|
|
|
#include <type_traits>
|
|
|
|
|
2022-02-18 00:22:00 +08:00
|
|
|
using namespace sf::Literals;
|
|
|
|
|
|
|
|
// Use sf::Vector2i for tests (except for float vector algebra).
|
|
|
|
// Test coverage is given, as there are no template specializations.
|
|
|
|
|
2014-08-26 15:52:37 +08:00
|
|
|
|
2021-12-24 21:31:27 +08:00
|
|
|
TEST_CASE("sf::Vector2 class template - [system]")
|
2014-08-26 15:52:37 +08:00
|
|
|
{
|
2021-12-24 21:31:27 +08:00
|
|
|
SUBCASE("Construction")
|
2014-08-26 15:52:37 +08:00
|
|
|
{
|
2021-12-24 21:31:27 +08:00
|
|
|
SUBCASE("Default constructor")
|
2014-08-26 15:52:37 +08:00
|
|
|
{
|
2015-04-26 21:24:49 +08:00
|
|
|
sf::Vector2i vector;
|
2014-08-26 15:52:37 +08:00
|
|
|
CHECK(vector.x == 0);
|
|
|
|
CHECK(vector.y == 0);
|
|
|
|
}
|
|
|
|
|
2021-12-24 21:31:27 +08:00
|
|
|
SUBCASE("(x, y) coordinate constructor")
|
2014-08-26 15:52:37 +08:00
|
|
|
{
|
2015-04-26 21:24:49 +08:00
|
|
|
sf::Vector2i vector(1, 2);
|
2014-08-26 15:52:37 +08:00
|
|
|
CHECK(vector.x == 1);
|
|
|
|
CHECK(vector.y == 2);
|
|
|
|
}
|
|
|
|
|
2021-12-24 21:31:27 +08:00
|
|
|
SUBCASE("Conversion constructor")
|
2014-08-26 15:52:37 +08:00
|
|
|
{
|
2022-02-18 00:41:14 +08:00
|
|
|
const sf::Vector2f sourceVector(1.0f, 2.0f);
|
|
|
|
const sf::Vector2i vector(sourceVector);
|
2014-08-26 15:52:37 +08:00
|
|
|
|
2015-04-26 21:24:49 +08:00
|
|
|
CHECK(vector.x == static_cast<int>(sourceVector.x));
|
|
|
|
CHECK(vector.y == static_cast<int>(sourceVector.y));
|
2014-08-26 15:52:37 +08:00
|
|
|
}
|
2022-04-29 23:37:29 +08:00
|
|
|
|
|
|
|
SUBCASE("Length and angle constructor")
|
|
|
|
{
|
2022-05-03 06:38:33 +08:00
|
|
|
CHECK(sf::Vector2f(0, 0_deg) == sf::Vector2f(0, 0));
|
|
|
|
CHECK(sf::Vector2f(0, 45_deg) == sf::Vector2f(0, 0));
|
|
|
|
CHECK(sf::Vector2f(0, 90_deg) == sf::Vector2f(0, 0));
|
|
|
|
CHECK(sf::Vector2f(0, 135_deg) == sf::Vector2f(0, 0));
|
|
|
|
CHECK(sf::Vector2f(0, 180_deg) == sf::Vector2f(0, 0));
|
|
|
|
CHECK(sf::Vector2f(0, 270_deg) == sf::Vector2f(0, 0));
|
|
|
|
CHECK(sf::Vector2f(0, 360_deg) == sf::Vector2f(0, 0));
|
|
|
|
CHECK(sf::Vector2f(0, -90_deg) == sf::Vector2f(0, 0));
|
|
|
|
CHECK(sf::Vector2f(0, -180_deg) == sf::Vector2f(0, 0));
|
|
|
|
CHECK(sf::Vector2f(0, -270_deg) == sf::Vector2f(0, 0));
|
|
|
|
CHECK(sf::Vector2f(0, -360_deg) == sf::Vector2f(0, 0));
|
|
|
|
|
|
|
|
CHECK(sf::Vector2f(1, 0_deg) == sf::Vector2f(1, 0));
|
|
|
|
CHECK(sf::Vector2f(1, 45_deg) == Approx(sf::Vector2f(std::sqrt(2.f) / 2.f, std::sqrt(2.f) / 2.f)));
|
|
|
|
CHECK(sf::Vector2f(1, 90_deg) == Approx(sf::Vector2f(0, 1)));
|
|
|
|
CHECK(sf::Vector2f(1, 135_deg) == Approx(sf::Vector2f(-std::sqrt(2.f) / 2.f, std::sqrt(2.f) / 2.f)));
|
|
|
|
CHECK(sf::Vector2f(1, 180_deg) == Approx(sf::Vector2f(-1, 0)));
|
|
|
|
CHECK(sf::Vector2f(1, 270_deg) == Approx(sf::Vector2f(0, -1)));
|
|
|
|
CHECK(sf::Vector2f(1, 360_deg) == Approx(sf::Vector2f(1, 0)));
|
|
|
|
CHECK(sf::Vector2f(1, -90_deg) == Approx(sf::Vector2f(0, -1)));
|
|
|
|
CHECK(sf::Vector2f(1, -180_deg) == Approx(sf::Vector2f(-1, 0)));
|
|
|
|
CHECK(sf::Vector2f(1, -270_deg) == Approx(sf::Vector2f(0, 1)));
|
|
|
|
CHECK(sf::Vector2f(1, -360_deg) == Approx(sf::Vector2f(1, 0)));
|
|
|
|
|
|
|
|
CHECK(sf::Vector2f(-1, 0_deg) == sf::Vector2f(-1, 0));
|
|
|
|
CHECK(sf::Vector2f(-1, 45_deg) == Approx(sf::Vector2f(-std::sqrt(2.f) / 2.f, -std::sqrt(2.f) / 2.f)));
|
|
|
|
CHECK(sf::Vector2f(-1, 90_deg) == Approx(sf::Vector2f(0, -1)));
|
|
|
|
CHECK(sf::Vector2f(-1, 135_deg) == Approx(sf::Vector2f(std::sqrt(2.f) / 2.f, -std::sqrt(2.f) / 2.f)));
|
|
|
|
CHECK(sf::Vector2f(-1, 180_deg) == Approx(sf::Vector2f(1, 0)));
|
|
|
|
CHECK(sf::Vector2f(-1, 270_deg) == Approx(sf::Vector2f(0, 1)));
|
|
|
|
CHECK(sf::Vector2f(-1, 360_deg) == Approx(sf::Vector2f(-1, 0)));
|
|
|
|
CHECK(sf::Vector2f(-1, -90_deg) == Approx(sf::Vector2f(0, 1)));
|
|
|
|
CHECK(sf::Vector2f(-1, -180_deg) == Approx(sf::Vector2f(1, 0)));
|
|
|
|
CHECK(sf::Vector2f(-1, -270_deg) == Approx(sf::Vector2f(0, -1)));
|
|
|
|
CHECK(sf::Vector2f(-1, -360_deg) == Approx(sf::Vector2f(-1, 0)));
|
|
|
|
|
|
|
|
CHECK(sf::Vector2f(4.2f, 0_deg) == sf::Vector2f(4.2f, 0));
|
2022-07-05 00:20:58 +08:00
|
|
|
CHECK(sf::Vector2f(4.2f, 45_deg) ==
|
|
|
|
Approx(sf::Vector2f(4.2f * std::sqrt(2.f) / 2.f, 4.2f * std::sqrt(2.f) / 2.f)));
|
2022-05-03 06:38:33 +08:00
|
|
|
CHECK(sf::Vector2f(4.2f, 90_deg) == Approx(sf::Vector2f(0, 4.2f)));
|
2022-07-05 00:20:58 +08:00
|
|
|
CHECK(sf::Vector2f(4.2f, 135_deg) ==
|
|
|
|
Approx(sf::Vector2f(-4.2f * std::sqrt(2.f) / 2.f, 4.2f * std::sqrt(2.f) / 2.f)));
|
2022-05-03 06:38:33 +08:00
|
|
|
CHECK(sf::Vector2f(4.2f, 180_deg) == Approx(sf::Vector2f(-4.2f, 0)));
|
|
|
|
CHECK(sf::Vector2f(4.2f, 270_deg) == Approx(sf::Vector2f(0, -4.2f)));
|
|
|
|
CHECK(sf::Vector2f(4.2f, 360_deg) == Approx(sf::Vector2f(4.2f, 0)));
|
|
|
|
CHECK(sf::Vector2f(4.2f, -90_deg) == Approx(sf::Vector2f(0, -4.2f)));
|
|
|
|
CHECK(sf::Vector2f(4.2f, -180_deg) == Approx(sf::Vector2f(-4.2f, 0)));
|
|
|
|
CHECK(sf::Vector2f(4.2f, -270_deg) == Approx(sf::Vector2f(0, 4.2f)));
|
|
|
|
CHECK(sf::Vector2f(4.2f, -360_deg) == Approx(sf::Vector2f(4.2f, 0)));
|
2022-04-29 23:37:29 +08:00
|
|
|
}
|
2014-08-26 15:52:37 +08:00
|
|
|
}
|
|
|
|
|
2021-12-24 21:31:27 +08:00
|
|
|
SUBCASE("Unary operations")
|
2014-08-26 15:52:37 +08:00
|
|
|
{
|
2021-12-24 21:31:27 +08:00
|
|
|
SUBCASE("-vector")
|
2014-08-26 15:52:37 +08:00
|
|
|
{
|
2022-02-18 00:41:14 +08:00
|
|
|
const sf::Vector2i vector(1, 2);
|
|
|
|
const sf::Vector2i negatedVector = -vector;
|
2014-08-26 15:52:37 +08:00
|
|
|
|
2015-04-26 21:24:49 +08:00
|
|
|
CHECK(negatedVector.x == -1);
|
|
|
|
CHECK(negatedVector.y == -2);
|
2014-08-26 15:52:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-24 21:31:27 +08:00
|
|
|
SUBCASE("Arithmetic operations between two vectors")
|
2014-08-26 15:52:37 +08:00
|
|
|
{
|
2022-07-05 00:20:58 +08:00
|
|
|
sf::Vector2i firstVector(2, 5);
|
2022-02-18 00:41:14 +08:00
|
|
|
const sf::Vector2i secondVector(8, 3);
|
2014-08-26 15:52:37 +08:00
|
|
|
|
2021-12-24 21:31:27 +08:00
|
|
|
SUBCASE("vector += vector")
|
2014-08-26 15:52:37 +08:00
|
|
|
{
|
|
|
|
firstVector += secondVector;
|
|
|
|
|
2015-04-26 21:24:49 +08:00
|
|
|
CHECK(firstVector.x == 10);
|
|
|
|
CHECK(firstVector.y == 8);
|
2014-08-26 15:52:37 +08:00
|
|
|
}
|
|
|
|
|
2021-12-24 21:31:27 +08:00
|
|
|
SUBCASE("vector -= vector")
|
2014-08-26 15:52:37 +08:00
|
|
|
{
|
|
|
|
firstVector -= secondVector;
|
|
|
|
|
2015-04-26 21:24:49 +08:00
|
|
|
CHECK(firstVector.x == -6);
|
|
|
|
CHECK(firstVector.y == 2);
|
2014-08-26 15:52:37 +08:00
|
|
|
}
|
|
|
|
|
2021-12-24 21:31:27 +08:00
|
|
|
SUBCASE("vector + vector")
|
2014-08-26 15:52:37 +08:00
|
|
|
{
|
2022-02-18 00:41:14 +08:00
|
|
|
const sf::Vector2i result = firstVector + secondVector;
|
2014-08-26 15:52:37 +08:00
|
|
|
|
2015-04-26 21:24:49 +08:00
|
|
|
CHECK(result.x == 10);
|
|
|
|
CHECK(result.y == 8);
|
2014-08-26 15:52:37 +08:00
|
|
|
}
|
|
|
|
|
2021-12-24 21:31:27 +08:00
|
|
|
SUBCASE("vector - vector")
|
2014-08-26 15:52:37 +08:00
|
|
|
{
|
2022-02-18 00:41:14 +08:00
|
|
|
const sf::Vector2i result = firstVector - secondVector;
|
2014-08-26 15:52:37 +08:00
|
|
|
|
2015-04-26 21:24:49 +08:00
|
|
|
CHECK(result.x == -6);
|
|
|
|
CHECK(result.y == 2);
|
2014-08-26 15:52:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-24 21:31:27 +08:00
|
|
|
SUBCASE("Arithmetic operations between vector and scalar value")
|
2014-08-26 15:52:37 +08:00
|
|
|
{
|
|
|
|
sf::Vector2i vector(26, 12);
|
2022-07-05 00:20:58 +08:00
|
|
|
int scalar = 2;
|
2014-08-26 15:52:37 +08:00
|
|
|
|
2021-12-24 21:31:27 +08:00
|
|
|
SUBCASE("vector * scalar")
|
2014-08-26 15:52:37 +08:00
|
|
|
{
|
2022-02-18 00:41:14 +08:00
|
|
|
const sf::Vector2i result = vector * scalar;
|
2014-08-26 15:52:37 +08:00
|
|
|
|
2015-04-26 21:24:49 +08:00
|
|
|
CHECK(result.x == 52);
|
|
|
|
CHECK(result.y == 24);
|
2014-08-26 15:52:37 +08:00
|
|
|
}
|
|
|
|
|
2021-12-24 21:31:27 +08:00
|
|
|
SUBCASE("scalar * vector")
|
2014-08-26 15:52:37 +08:00
|
|
|
{
|
2022-02-18 00:41:14 +08:00
|
|
|
const sf::Vector2i result = scalar * vector;
|
2014-08-26 15:52:37 +08:00
|
|
|
|
2015-04-26 21:24:49 +08:00
|
|
|
CHECK(result.x == 52);
|
|
|
|
CHECK(result.y == 24);
|
2014-08-26 15:52:37 +08:00
|
|
|
}
|
|
|
|
|
2021-12-24 21:31:27 +08:00
|
|
|
SUBCASE("vector *= scalar")
|
2014-08-26 15:52:37 +08:00
|
|
|
{
|
|
|
|
vector *= scalar;
|
|
|
|
|
2015-04-26 21:24:49 +08:00
|
|
|
CHECK(vector.x == 52);
|
|
|
|
CHECK(vector.y == 24);
|
2014-08-26 15:52:37 +08:00
|
|
|
}
|
|
|
|
|
2021-12-24 21:31:27 +08:00
|
|
|
SUBCASE("vector / scalar")
|
2014-08-26 15:52:37 +08:00
|
|
|
{
|
2022-02-18 00:41:14 +08:00
|
|
|
const sf::Vector2i result = vector / scalar;
|
2014-08-26 15:52:37 +08:00
|
|
|
|
2015-04-26 21:24:49 +08:00
|
|
|
CHECK(result.x == 13);
|
|
|
|
CHECK(result.y == 6);
|
2014-08-26 15:52:37 +08:00
|
|
|
}
|
|
|
|
|
2021-12-24 21:31:27 +08:00
|
|
|
SUBCASE("vector /= scalar")
|
2014-08-26 15:52:37 +08:00
|
|
|
{
|
|
|
|
vector /= scalar;
|
|
|
|
|
2015-04-26 21:24:49 +08:00
|
|
|
CHECK(vector.x == 13);
|
|
|
|
CHECK(vector.y == 6);
|
2014-08-26 15:52:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-24 21:31:27 +08:00
|
|
|
SUBCASE("Comparison operations (two equal and one different vector)")
|
2014-08-26 15:52:37 +08:00
|
|
|
{
|
2022-02-18 00:41:14 +08:00
|
|
|
const sf::Vector2i firstEqualVector(1, 5);
|
|
|
|
const sf::Vector2i secondEqualVector(1, 5);
|
|
|
|
const sf::Vector2i differentVector(6, 9);
|
2014-08-26 15:52:37 +08:00
|
|
|
|
2021-12-24 21:31:27 +08:00
|
|
|
SUBCASE("vector == vector")
|
2014-08-26 15:52:37 +08:00
|
|
|
{
|
2015-04-26 21:24:49 +08:00
|
|
|
CHECK(firstEqualVector == secondEqualVector);
|
|
|
|
CHECK_FALSE(firstEqualVector == differentVector);
|
2014-08-26 15:52:37 +08:00
|
|
|
}
|
|
|
|
|
2021-12-24 21:31:27 +08:00
|
|
|
SUBCASE("vector != vector")
|
2014-08-26 15:52:37 +08:00
|
|
|
{
|
2015-04-26 21:24:49 +08:00
|
|
|
CHECK(firstEqualVector != differentVector);
|
|
|
|
CHECK_FALSE(firstEqualVector != secondEqualVector);
|
2014-08-26 15:52:37 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-17 00:58:53 +08:00
|
|
|
|
2021-12-24 21:31:27 +08:00
|
|
|
SUBCASE("Structured bindings")
|
2021-12-17 00:58:53 +08:00
|
|
|
{
|
|
|
|
sf::Vector2i vector(1, 2);
|
|
|
|
|
2021-12-24 21:31:27 +08:00
|
|
|
SUBCASE("destructure by value")
|
2021-12-17 00:58:53 +08:00
|
|
|
{
|
|
|
|
auto [x, y] = vector;
|
|
|
|
|
|
|
|
CHECK(x == 1);
|
|
|
|
CHECK(y == 2);
|
|
|
|
|
|
|
|
static_assert(std::is_same_v<decltype(x), decltype(vector.x)>);
|
|
|
|
|
|
|
|
x = 3;
|
|
|
|
|
|
|
|
CHECK(x == 3);
|
|
|
|
CHECK(vector.x == 1);
|
|
|
|
}
|
|
|
|
|
2021-12-24 21:31:27 +08:00
|
|
|
SUBCASE("destructure by ref")
|
2021-12-17 00:58:53 +08:00
|
|
|
{
|
|
|
|
auto& [x, y] = vector;
|
|
|
|
|
|
|
|
CHECK(x == 1);
|
|
|
|
CHECK(y == 2);
|
|
|
|
|
|
|
|
static_assert(std::is_same_v<decltype(x), decltype(vector.x)>);
|
|
|
|
|
|
|
|
x = 3;
|
|
|
|
|
|
|
|
CHECK(x == 3);
|
|
|
|
CHECK(vector.x == 3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-18 00:22:00 +08:00
|
|
|
SUBCASE("Length and normalization")
|
|
|
|
{
|
2022-02-18 00:41:14 +08:00
|
|
|
const sf::Vector2f v(2.4f, 3.0f);
|
2022-02-18 00:22:00 +08:00
|
|
|
|
2022-05-03 06:38:33 +08:00
|
|
|
CHECK(v.length() == Approx(3.84187f));
|
|
|
|
CHECK(v.lengthSq() == Approx(14.7599650969f));
|
|
|
|
CHECK(v.normalized() == Approx(sf::Vector2f(0.624695f, 0.780869f)));
|
2022-02-18 00:22:00 +08:00
|
|
|
|
2022-02-18 00:41:14 +08:00
|
|
|
const sf::Vector2f w(-0.7f, -2.2f);
|
2022-06-24 07:33:33 +08:00
|
|
|
|
2022-05-03 06:38:33 +08:00
|
|
|
CHECK(w.length() == Approx(2.30868f));
|
|
|
|
CHECK(w.lengthSq() == Approx(5.3300033f));
|
|
|
|
CHECK(w.normalized() == Approx(sf::Vector2f(-0.303204f, -0.952926f)));
|
2022-02-18 00:22:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SUBCASE("Rotations and angles")
|
|
|
|
{
|
2022-02-18 00:41:14 +08:00
|
|
|
const sf::Vector2f v(2.4f, 3.0f);
|
2022-06-24 07:33:33 +08:00
|
|
|
|
2022-05-03 06:38:33 +08:00
|
|
|
CHECK(v.angle() == Approx(51.3402_deg));
|
|
|
|
CHECK(sf::Vector2f::UnitX.angleTo(v) == Approx(51.3402_deg));
|
|
|
|
CHECK(sf::Vector2f::UnitY.angleTo(v) == Approx(-38.6598_deg));
|
2022-02-18 00:22:00 +08:00
|
|
|
|
2022-02-18 00:41:14 +08:00
|
|
|
const sf::Vector2f w(-0.7f, -2.2f);
|
2022-02-18 00:22:00 +08:00
|
|
|
|
2022-05-03 06:38:33 +08:00
|
|
|
CHECK(w.angle() == Approx(-107.65_deg));
|
|
|
|
CHECK(sf::Vector2f::UnitX.angleTo(w) == Approx(-107.65_deg));
|
|
|
|
CHECK(sf::Vector2f::UnitY.angleTo(w) == Approx(162.35_deg));
|
2022-02-18 00:22:00 +08:00
|
|
|
|
2022-05-03 06:38:33 +08:00
|
|
|
CHECK(v.angleTo(w) == Approx(-158.9902_deg));
|
|
|
|
CHECK(w.angleTo(v) == Approx(158.9902_deg));
|
2022-02-18 00:22:00 +08:00
|
|
|
|
2022-02-18 00:41:14 +08:00
|
|
|
const float ratio = w.length() / v.length();
|
2022-07-05 00:20:58 +08:00
|
|
|
CHECK(v.rotatedBy(-158.9902_deg) * ratio == Approx(w));
|
2022-05-03 06:38:33 +08:00
|
|
|
CHECK(w.rotatedBy(158.9902_deg) / ratio == Approx(v));
|
2022-02-18 00:22:00 +08:00
|
|
|
|
|
|
|
CHECK(v.perpendicular() == sf::Vector2f(-3.0f, 2.4f));
|
|
|
|
CHECK(v.perpendicular().perpendicular().perpendicular().perpendicular() == v);
|
|
|
|
|
2022-05-03 06:38:33 +08:00
|
|
|
CHECK(v.rotatedBy(90_deg) == Approx(sf::Vector2f(-3.0f, 2.4f)));
|
|
|
|
CHECK(v.rotatedBy(27.14_deg) == Approx(sf::Vector2f(0.767248f, 3.76448f)));
|
|
|
|
CHECK(v.rotatedBy(-36.11_deg) == Approx(sf::Vector2f(3.70694f, 1.00925f)));
|
2022-02-18 00:22:00 +08:00
|
|
|
}
|
2022-06-24 07:33:33 +08:00
|
|
|
|
2022-02-18 00:22:00 +08:00
|
|
|
SUBCASE("Products and quotients")
|
|
|
|
{
|
2022-02-18 00:41:14 +08:00
|
|
|
const sf::Vector2f v(2.4f, 3.0f);
|
|
|
|
const sf::Vector2f w(-0.7f, -2.2f);
|
2022-02-18 00:22:00 +08:00
|
|
|
|
2022-05-03 06:38:33 +08:00
|
|
|
CHECK(v.dot(w) == Approx(-8.28f));
|
|
|
|
CHECK(w.dot(v) == Approx(-8.28f));
|
2022-02-18 00:22:00 +08:00
|
|
|
|
2022-05-03 06:38:33 +08:00
|
|
|
CHECK(v.cross(w) == Approx(-3.18f));
|
|
|
|
CHECK(w.cross(v) == Approx(+3.18f));
|
2022-06-24 07:33:33 +08:00
|
|
|
|
2022-05-03 06:38:33 +08:00
|
|
|
CHECK(v.cwiseMul(w) == Approx(sf::Vector2f(-1.68f, -6.6f)));
|
|
|
|
CHECK(w.cwiseMul(v) == Approx(sf::Vector2f(-1.68f, -6.6f)));
|
|
|
|
CHECK(v.cwiseDiv(w) == Approx(sf::Vector2f(-3.428571f, -1.363636f)));
|
|
|
|
CHECK(w.cwiseDiv(v) == Approx(sf::Vector2f(-0.291666f, -0.733333f)));
|
2022-02-18 00:22:00 +08:00
|
|
|
}
|
2022-06-24 07:33:33 +08:00
|
|
|
|
2022-02-18 00:22:00 +08:00
|
|
|
SUBCASE("Projection")
|
|
|
|
{
|
2022-02-18 00:41:14 +08:00
|
|
|
const sf::Vector2f v(2.4f, 3.0f);
|
|
|
|
const sf::Vector2f w(-0.7f, -2.2f);
|
2022-06-24 07:33:33 +08:00
|
|
|
|
2022-05-03 06:38:33 +08:00
|
|
|
CHECK(v.projectedOnto(w) == Approx(sf::Vector2f(1.087430f, 3.417636f)));
|
|
|
|
CHECK(v.projectedOnto(w) == Approx(sf::Vector2f(-1.55347f * w)));
|
2022-02-18 00:22:00 +08:00
|
|
|
|
2022-05-03 06:38:33 +08:00
|
|
|
CHECK(w.projectedOnto(v) == Approx(sf::Vector2f(-1.346342f, -1.682927f)));
|
|
|
|
CHECK(w.projectedOnto(v) == Approx(sf::Vector2f(-0.560976f * v)));
|
2022-02-18 00:22:00 +08:00
|
|
|
|
2022-05-03 06:38:33 +08:00
|
|
|
CHECK(v.projectedOnto(sf::Vector2f::UnitX) == Approx(sf::Vector2f(2.4f, 0.0f)));
|
|
|
|
CHECK(v.projectedOnto(sf::Vector2f::UnitY) == Approx(sf::Vector2f(0.0f, 3.0f)));
|
2022-02-18 00:22:00 +08:00
|
|
|
}
|
|
|
|
|
2021-12-24 21:31:27 +08:00
|
|
|
SUBCASE("Constexpr support")
|
2021-12-17 00:58:53 +08:00
|
|
|
{
|
2022-02-18 00:22:00 +08:00
|
|
|
constexpr sf::Vector2i v(1, 2);
|
|
|
|
constexpr sf::Vector2i w(2, -3);
|
|
|
|
|
|
|
|
static_assert(v.x == 1);
|
|
|
|
static_assert(v.y == 2);
|
|
|
|
static_assert(v + w == sf::Vector2i(3, -1));
|
|
|
|
|
|
|
|
static_assert(v.lengthSq() == 5);
|
|
|
|
static_assert(v.perpendicular() == sf::Vector2i(-2, 1));
|
|
|
|
|
|
|
|
static_assert(v.dot(w) == -4);
|
|
|
|
static_assert(v.cross(w) == -7);
|
|
|
|
static_assert(v.cwiseMul(w) == sf::Vector2i(2, -6));
|
|
|
|
static_assert(w.cwiseDiv(v) == sf::Vector2i(2, -1));
|
2021-12-17 00:58:53 +08:00
|
|
|
}
|
2014-08-26 15:52:37 +08:00
|
|
|
}
|