| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- //
- // shape_test.cxx
- // math
- //
- // Created by Sam Jaffe on 5/4/19.
- // Copyright © 2019 Sam Jaffe. All rights reserved.
- //
- #include <gmock/gmock.h>
- #include "game/math/shape.hpp"
- #include "test_printers.h"
- using namespace math::dim2;
- using namespace testing;
- struct FromOriginTest : TestWithParam<line> {};
- TEST_P(FromOriginTest, IntersectsAtOrigin) {
- line l1 = {GetParam().first, {{0, 0}}};
- line l2 = {{{0, 0}}, GetParam().second};
- EXPECT_THAT(math::lines::intersection(l1, l2), Eq(point{{0, 0}}));
- }
- std::vector<line> const point_pairs{
- {{{1, 1}}, {{0, 0}}}, // 0 length
- {{{1, 1}}, {{1, 0}}}, // -45deg
- {{{1, 1}}, {{0, 1}}}, // +45deg
- {{{1, 1}}, {{1, 1}}}, // +0deg
- {{{1, 1}}, {{2, 1}}}, // -18deg (approx)
- {{{1, 1}}, {{1, 2}}}, // +18deg (approx)
- {{{2, 3}}, {{1, 2}}}, //
- {{{1, 1}}, {{-1, 0}}}, // +135deg
- {{{1, 1}}, {{0, -1}}}, // -135deg
- {{{1, 1}}, {{-1, -1}}}, // +180deg
- {{{1, 1}}, {{1, -1}}}, // +90deg
- {{{1, 1}}, {{-1, 1}}}, // -90deg
- };
- INSTANTIATE_TEST_CASE_P(LineIntersection, FromOriginTest,
- ValuesIn(point_pairs));
- struct UnitLineTest : TestWithParam<point> {};
- TEST_P(UnitLineTest, OrthoOnIntersection) {
- line const ln{{{0, 0}}, {{1, 0}}};
- point const pt = GetParam();
- line const expected{pt, {{pt[0], 0}}};
- EXPECT_THAT(math::lines::orthogonal(ln, pt), Eq(expected));
- }
- TEST_P(UnitLineTest, OrthoOnIntersectionY) {
- line const ln{{{0, 0}}, {{0, 1}}};
- point const pt = GetParam();
- line const expected{pt, {{pt[0], 0}}};
- EXPECT_THAT(math::lines::orthogonal(ln, pt), Eq(expected));
- }
- std::vector<point> x_orthos{
- {{0, 1}}, {{1, 1}}, {{1, 0}}, {{-1, 0}}, {{0, -1}}, {{-1, -1}},
- {{0, 2}}, {{2, 2}}, {{2, 0}}, {{-2, 0}}, {{0, -2}}, {{-2, -2}},
- {{2, 1}}, {{1, 2}}, {{-2, 1}}, {{-1, 2}}, {{1, -2}}, {{2, -1}}};
- INSTANTIATE_TEST_CASE_P(LineOrthogonal, UnitLineTest, ValuesIn(x_orthos));
- struct DiagonalTest : TestWithParam<std::pair<point, float>> {};
- TEST_P(DiagonalTest, OrthoOnIntersection) {
- line const ln{{{0, 0}}, {{1, 1}}};
- point const pt = GetParam().first;
- line const expected{pt, {{GetParam().second, GetParam().second}}};
- EXPECT_THAT(math::lines::orthogonal(ln, pt), Eq(expected));
- }
- std::vector<std::pair<point, float>> diag_orthos{
- {{{0, 1}}, 0.5f}, {{{1, 1}}, 1.f}, {{{1, 0}}, 0.5f},
- {{{-1, 0}}, -0.5f}, {{{0, -1}}, -0.5f}, {{{-1, -1}}, -1.f},
- {{{0, 2}}, 1.f}, {{{2, 2}}, 2.f}, {{{2, 0}}, 1.f},
- {{{-2, 0}}, -1.f}, {{{0, -2}}, -1.f}, {{{-2, -2}}, -2.f},
- {{{2, 1}}, 1.5f}, {{{1, 2}}, 1.5f}, {{{-2, 1}}, -0.5f},
- {{{-1, 2}}, 0.5f}, {{{1, -2}}, -0.5f}, {{{2, -1}}, 0.5f}};
- INSTANTIATE_TEST_CASE_P(LineOrthogonal, DiagonalTest, ValuesIn(diag_orthos));
|