19#include <gtest/gtest.h>
34TEST(ParallelogramTest, midpoint)
36 Rect r(-0.5, -0.5, 5.5, 5.5);
37 auto center =
Point(2.5, 2.5);
40 for (
double angle : { 0, 1, 25, 45, 90, 135 }) {
42 auto rotated_center = center *
Rotate(angle / 180.0 * M_PI);
43 EXPECT_TRUE(
Geom::are_near(rotated_rect.midpoint(), rotated_center, 1e-6)) <<
"Angle = " << angle <<
" deg";
47TEST(ParallelogramTest, containsPoint1)
50 auto rotated_rect = r;
51 EXPECT_TRUE(rotated_rect.contains(
Point(0, 0)));
52 EXPECT_TRUE(rotated_rect.contains(
Point(1, 1)));
53 EXPECT_TRUE(rotated_rect.contains(
Point(0.5, 0.5)));
54 EXPECT_FALSE(rotated_rect.contains(
Point(1.1, 0.5)));
55 EXPECT_FALSE(rotated_rect.contains(
Point(0.5, 1.1)));
58TEST(ParallelogramTest, containsPoint2)
62 EXPECT_TRUE(rotated_rect.contains(
Point(0, 0)));
63 EXPECT_TRUE(rotated_rect.contains(
Point(0, 1.2)));
64 EXPECT_TRUE(rotated_rect.contains(
Point(0.5, 0.9)));
65 EXPECT_FALSE(rotated_rect.contains(
Point(1, 1)));
66 EXPECT_FALSE(rotated_rect.contains(
Point(0.1, 0)));
69TEST(ParallelogramTest, intersects_aligned)
72 auto rotated_rect = r;
74 EXPECT_TRUE(rotated_rect.intersects(
Rect(-1, -1, 2, 2)));
75 EXPECT_TRUE(rotated_rect.intersects(
Rect(0.1, 0.1, 0.2, 0.2)));
76 EXPECT_TRUE(rotated_rect.intersects(
Rect(-0.1, -0.1, 0.1, 0.1)));
77 EXPECT_FALSE(rotated_rect.intersects(
Rect(-0.2, -0.2, -0.1, -0.1)));
78 EXPECT_FALSE(rotated_rect.intersects(
Rect(1.1, 1.1, 1.2, 1.2)));
80 EXPECT_TRUE(rotated_rect.intersects(
Rect(0.5, -0.1, 0.6, 1.2)));
81 EXPECT_TRUE(rotated_rect.intersects(
Rect(-0.1, 0.5, 1.2, 0.6)));
86 auto r = Rect::from_xywh(1.260, 0.547, 8.523, 11.932);
88 auto bbox = rrect.bounds();
89 auto expected_bbox = Rect::from_xywh(-0.186, -0.378, 11.415, 13.783);
90 for (
int i = 0; i < 4; i++) {
91 EXPECT_TRUE(
Geom::are_near(bbox.corner(i), expected_bbox.corner(i), 1e-3));
95TEST(ParallelogramTest, isSheared)
109 EXPECT_DOUBLE_EQ(p.
area(), r.
area());
111 EXPECT_DOUBLE_EQ(p.
area(), r.
area());
113 EXPECT_DOUBLE_EQ(p.
area(), r.
area());
115 EXPECT_DOUBLE_EQ(p.
area(), r.
area() * 4);
118class ParallelogramTest
119 :
public testing::TestWithParam<std::tuple<Rect , double , bool >> {
121 void SetUp()
override { target = Rect::from_xywh(0, 0, 11, 13); }
132 std::tie(rect, degrees,
intersects) = GetParam();
134 <<
"ERROR: rect {" << rect <<
"} rotated by {" << degrees <<
"} degrees " << (!
intersects ?
"" :
"NOT ")
135 <<
"intersects with {" << target <<
"} but MUST " << (
intersects ?
"" :
"NOT");
141 std::make_tuple(Rect::from_xywh(10.456, -4.479, 7, 5), 0,
true),
142 std::make_tuple(Rect::from_xywh(10.456, -4.479, 7, 5), 15,
false),
143 std::make_tuple(Rect::from_xywh(9.929, 12.313, 7, 5), 93.2,
false),
144 std::make_tuple(Rect::from_xywh(9.929, 12.313, 7, 5), 91.37,
true),
145 std::make_tuple(Rect::from_xywh(-1, 4, 13, 3), 0,
true),
146 std::make_tuple(Rect::from_xywh(4, -2, 3, 16), 0,
true),
147 std::make_tuple(Rect::from_xywh(-5.113, -3.283, 5.000, 7.000), 11.81,
false),
148 std::make_tuple(Rect::from_xywh(-5.113, -3.283, 5.000, 7.000), 13.35,
true),
149 std::make_tuple(Rect::from_xywh(1.260, 0.547, 8.523, 11.932), 15.59,
true),
150 std::make_tuple(Rect::from_xywh(5.328, 0.404, 11, 2), 28.16,
true),
151 std::make_tuple(Rect::from_xywh(4.853, 10.691, 11, 2), -30.4,
true),
152 std::make_tuple(Rect::from_xywh(-4.429, 10.752, 11, 2), 29.7,
true),
153 std::make_tuple(Rect::from_xywh(-4.538, 0.314, 11, 2), -34.19,
true),
154 std::make_tuple(Rect::from_xywh(8.398, -3.790, 2, 11), -34,
true),
155 std::make_tuple(Rect::from_xywh(8.614, 6.163, 2, 11), 30.38,
true),
156 std::make_tuple(Rect::from_xywh(0.492, 6.904, 2, 11), -37.29,
true),
157 std::make_tuple(Rect::from_xywh(0.202, -3.148, 2, 11), 31.12,
true)));
3x3 matrix representing an affine transformation.
C area() const
Compute the rectangle's area.
CPoint midpoint() const
Get the point in the geometric center of the rectangle.
Paralellogram, representing a linear transformation of a rectangle.
bool isSheared(Coord eps=EPSILON) const
True if this parallelogram does not have right angles.
Coord area() const
Area (non-negative)
Two-dimensional point that doubles as a vector.
Axis aligned, non-empty rectangle.
Rotation around the origin.
Integral and real coordinate types and some basic utilities.
Various utility functions.
static double area(Geom::Point a, Geom::Point b, Geom::Point c)
TEST(AffineTest, Equality)
bool are_near(Affine const &a1, Affine const &a2, Coord eps=EPSILON)
TEST_P(ParallelogramTest, intersects)
INSTANTIATE_TEST_CASE_P(intesect_non_aligned, ParallelogramTest, testing::Values(std::make_tuple(Rect::from_xywh(10.456, -4.479, 7, 5), 0, true), std::make_tuple(Rect::from_xywh(10.456, -4.479, 7, 5), 15, false), std::make_tuple(Rect::from_xywh(9.929, 12.313, 7, 5), 93.2, false), std::make_tuple(Rect::from_xywh(9.929, 12.313, 7, 5), 91.37, true), std::make_tuple(Rect::from_xywh(-1, 4, 13, 3), 0, true), std::make_tuple(Rect::from_xywh(4, -2, 3, 16), 0, true), std::make_tuple(Rect::from_xywh(-5.113, -3.283, 5.000, 7.000), 11.81, false), std::make_tuple(Rect::from_xywh(-5.113, -3.283, 5.000, 7.000), 13.35, true), std::make_tuple(Rect::from_xywh(1.260, 0.547, 8.523, 11.932), 15.59, true), std::make_tuple(Rect::from_xywh(5.328, 0.404, 11, 2), 28.16, true), std::make_tuple(Rect::from_xywh(4.853, 10.691, 11, 2), -30.4, true), std::make_tuple(Rect::from_xywh(-4.429, 10.752, 11, 2), 29.7, true), std::make_tuple(Rect::from_xywh(-4.538, 0.314, 11, 2), -34.19, true), std::make_tuple(Rect::from_xywh(8.398, -3.790, 2, 11), -34, true), std::make_tuple(Rect::from_xywh(8.614, 6.163, 2, 11), 30.38, true), std::make_tuple(Rect::from_xywh(0.492, 6.904, 2, 11), -37.29, true), std::make_tuple(Rect::from_xywh(0.202, -3.148, 2, 11), 31.12, true)))
static Parallelogram parallelogram_from_rect_rotate(Rect const &rect, Rotate const &rotate, Point const &point)