52#include <gtest/gtest.h>
54#include <unordered_set>
72 return std::abs(a - b) < tol;
90 EXPECT_EQ(origin.get_x(), 0);
91 EXPECT_EQ(origin.get_y(), 0);
96 EXPECT_EQ(p1.get_x(), 1);
97 EXPECT_EQ(p1.get_y(), 2);
103 EXPECT_EQ(p1, p1_copy);
110 EXPECT_EQ(
sum.get_x(), 4);
111 EXPECT_EQ(
sum.get_y(), 6);
118 EXPECT_EQ(p.
get_x(), 4);
119 EXPECT_EQ(p.
get_y(), 6);
125 EXPECT_EQ(
diff.get_x(), 2);
126 EXPECT_EQ(
diff.get_y(), 2);
133 EXPECT_EQ(p.
get_x(), 2);
134 EXPECT_EQ(p.
get_y(), 2);
139 std::string str = origin.to_string();
140 EXPECT_FALSE(str.empty());
141 EXPECT_NE(str.find(
"0"), std::string::npos);
146 std::string str =
static_cast<std::string
>(p1);
147 EXPECT_FALSE(str.empty());
163 EXPECT_EQ(dist_sq, 25);
173 EXPECT_TRUE(a.is_colinear_with(b, c));
174 EXPECT_FALSE(a.is_colinear_with(b, d));
182 Point right{0.5, -1};
184 EXPECT_TRUE(left.is_left_of(a, b));
185 EXPECT_FALSE(right.is_left_of(a, b));
193 Point right{0.5, -1};
195 EXPECT_TRUE(right.is_to_right_from(a, b));
196 EXPECT_FALSE(left.is_to_right_from(a, b));
203 Point on_line{0.5, 0};
206 EXPECT_TRUE(on_line.is_to_left_on_from(a, b));
207 EXPECT_TRUE(left.is_to_left_on_from(a, b));
216 EXPECT_TRUE(a.is_clockwise_with(b, c));
226 EXPECT_TRUE(
between.is_between(a, b));
227 EXPECT_FALSE(outside.is_between(a, b));
237 EXPECT_EQ(nearest, near);
242 EXPECT_EQ(p1.highest_point(), p1);
243 EXPECT_EQ(p1.lowest_point(), p1);
244 EXPECT_EQ(p1.leftmost_point(), p1);
245 EXPECT_EQ(p1.rightmost_point(), p1);
263 EXPECT_EQ(a.midpoint(b),
Point(5, 5));
277 std::unordered_set<Point> set;
278 set.insert(
Point(1, 2));
279 EXPECT_EQ(set.count(
Point(1, 2)), 1u);
307 Point original{7, 11};
309 Point back{polar_form};
311 EXPECT_TRUE(
approx_equal(back.get_x(), original.get_x()));
312 EXPECT_TRUE(
approx_equal(back.get_y(), original.get_y()));
345 std::string str = polar.to_string();
346 EXPECT_FALSE(str.empty());
347 EXPECT_EQ(str[0],
'[');
353 EXPECT_EQ(pp.
get_r(), 0);
382 EXPECT_EQ(horizontal.get_src_point(), origin);
383 EXPECT_EQ(horizontal.get_tgt_point(), p1);
413 Point mid = diagonal.mid_point();
422 EXPECT_EQ(
h.get_y(), 2);
428 const Point &
l = s.lowest_point();
429 EXPECT_EQ(
l.get_y(), 0);
435 const Point &
l = s.leftmost_point();
436 EXPECT_EQ(
l.get_x(), 0);
442 const Point &
r = s.rightmost_point();
443 EXPECT_EQ(
r.get_x(), 2);
448 Point inside{0.5, 0.5};
451 EXPECT_TRUE(diagonal.contains(inside));
452 EXPECT_FALSE(diagonal.contains(outside));
457 Point colinear{0.5, 0.5};
458 Point not_colinear{0.5, 0.6};
460 EXPECT_TRUE(diagonal.is_colinear_with(colinear));
461 EXPECT_FALSE(diagonal.is_colinear_with(not_colinear));
470 EXPECT_TRUE(s1.is_parallel_with(s2));
471 EXPECT_FALSE(s1.is_parallel_with(s3));
480 EXPECT_TRUE(s1.intersects_with(s2));
481 EXPECT_FALSE(s1.intersects_with(s3));
489 EXPECT_TRUE(s1.intersects_properly_with(s2));
497 Point inter = s1.intersection_with(s2);
507 EXPECT_THROW(s1.intersection_with(s2), std::domain_error);
513 EXPECT_EQ(s.sense(), Segment::E);
519 EXPECT_EQ(s.sense(), Segment::W);
525 EXPECT_EQ(s.sense(), Segment::N);
531 EXPECT_EQ(s.sense(), Segment::S);
537 EXPECT_EQ(s.sense(), Segment::NE);
543 EXPECT_EQ(s.sense(), Segment::SW);
549 EXPECT_EQ(s.sense(), Segment::NW);
555 EXPECT_EQ(s.sense(), Segment::SE);
560 std::string str = diagonal.to_string();
561 EXPECT_FALSE(str.empty());
569 EXPECT_TRUE(
approx_equal(s.get_tgt_point().get_x(), 0.0, 1e-6));
570 EXPECT_TRUE(
approx_equal(s.get_tgt_point().get_y(), 1.0, 1e-6));
587 double angle = s.counterclockwise_angle();
606 EXPECT_EQ(t.get_p1(), p1);
607 EXPECT_EQ(t.get_p2(), p2);
608 EXPECT_EQ(t.get_p3(), p3);
616 EXPECT_EQ(t2.get_p1(), p1);
624 EXPECT_EQ(t2.get_p3(), p3);
633 EXPECT_THROW(
Triangle(a, b, c), std::domain_error);
648 EXPECT_TRUE(t.contains(inside));
649 EXPECT_FALSE(t.contains(outside));
655 EXPECT_EQ(
h.get_y(), 3);
660 const Point &
l = t.lowest_point();
661 EXPECT_EQ(
l.get_y(), 0);
666 const Point &
l = t.leftmost_point();
667 EXPECT_EQ(
l.get_x(), 0);
672 const Point &
r = t.rightmost_point();
673 EXPECT_EQ(
r.get_x(), 4);
685 EXPECT_NE(ccw.is_clockwise(), cw.is_clockwise());
699static_assert(not
noexcept(std::declval<const Rectangle &>().width()));
700static_assert(not
noexcept(std::declval<const Rectangle &>().height()));
701static_assert(not
noexcept(std::declval<const Rectangle &>().area()));
702static_assert(not
noexcept(std::declval<const Rectangle &>().perimeter()));
703static_assert(not
noexcept(std::declval<const Rectangle &>().center()));
704static_assert(not
noexcept(std::declval<const Rectangle &>().corners()));
705static_assert(not
noexcept(std::declval<const Rectangle &>()
706 .distance_squared_to(std::declval<const Point &>())));
719 EXPECT_EQ(
r.get_xmin(), 0);
720 EXPECT_EQ(
r.get_ymin(), 0);
721 EXPECT_EQ(
r.get_xmax(), 4);
722 EXPECT_EQ(
r.get_ymax(), 3);
727 EXPECT_THROW(
Rectangle(4, 0, 0, 3), std::range_error);
728 EXPECT_THROW(
Rectangle(0, 3, 4, 0), std::range_error);
733 EXPECT_EQ(
r.width(), 4);
738 EXPECT_EQ(
r.height(), 3);
747 EXPECT_TRUE(
r.contains(inside));
748 EXPECT_FALSE(
r.contains(outside));
749 EXPECT_TRUE(
r.contains(on_edge));
757 EXPECT_TRUE(
r.intersects(r2));
758 EXPECT_FALSE(
r.intersects(r3));
766 EXPECT_EQ(
r.distance_squared_to(inside), 0);
767 EXPECT_EQ(
r.distance_squared_to(outside), 1);
791 EXPECT_THROW(r_mod.
set_rect(3, 2, 1, 4), std::range_error);
814 EXPECT_EQ(ellipse.get_center(), center);
815 EXPECT_EQ(ellipse.get_hradius(), 2);
816 EXPECT_EQ(ellipse.get_vradius(), 1);
822 EXPECT_EQ(e2.get_center(), ellipse.get_center());
823 EXPECT_EQ(e2.get_hradius(), ellipse.get_hradius());
824 EXPECT_EQ(e2.get_vradius(), ellipse.get_vradius());
830 EXPECT_EQ(
h.get_x(), 0);
831 EXPECT_EQ(
h.get_y(), 1);
836 Point l = ellipse.lowest_point();
837 EXPECT_EQ(
l.get_x(), 0);
838 EXPECT_EQ(
l.get_y(), -1);
843 Point l = ellipse.leftmost_point();
844 EXPECT_EQ(
l.get_x(), -2);
845 EXPECT_EQ(
l.get_y(), 0);
850 Point r = ellipse.rightmost_point();
851 EXPECT_EQ(
r.get_x(), 2);
852 EXPECT_EQ(
r.get_y(), 0);
857 EXPECT_TRUE(circle.contains(center));
862 Point inside{0.5, 0.5};
863 EXPECT_TRUE(circle.contains(inside));
869 EXPECT_FALSE(circle.contains(outside));
874 Point on_border{1, 0};
875 EXPECT_TRUE(circle.contains(on_border));
880 Point on_border{1, 0};
881 EXPECT_TRUE(circle.intersects_with(on_border));
891 Point inside{0.5, 0};
892 EXPECT_TRUE(inside.is_inside(circle));
964 EXPECT_FALSE(ellipse.to_string().empty());
965 EXPECT_EQ(ellipse,
Ellipse(center, 2, 1));
972TEST(HelperFunctionTest, AreaOfParallelogram)
982TEST(HelperFunctionTest, AreaOfParallelogramNegative)
992TEST(HelperFunctionTest, EuclideanDistance)
998TEST(HelperFunctionTest, EuclideanDistanceZero)
1028TEST(HelperFunctionTest, SquareRoot)
1034TEST(HelperFunctionTest, GeomNumberToDouble)
1045TEST(TextTest, AproximateStringSizeSimple)
1047 std::string str =
"hello";
1052TEST(TextTest, AproximateStringSizeWithLatex)
1054 std::string str =
"\\alpha";
1059TEST(TextTest, AproximateStringSizeWithDollarSigns)
1061 std::string str =
"$x$";
1066TEST(TextTest, AproximateStringSizeWithBraces)
1068 std::string str =
"{ab}";
1078 EXPECT_EQ(t.get_point(), p);
1079 EXPECT_EQ(t.get_str(),
"Hello");
1080 EXPECT_EQ(t.len(), 5);
1083TEST(TextTest, TextDefaultConstruction)
1094 EXPECT_EQ(t.highest_point(), p);
1095 EXPECT_EQ(t.lowest_point(), p);
1096 EXPECT_EQ(t.leftmost_point(), p);
1097 EXPECT_EQ(t.rightmost_point(), p);
1104TEST(GeomObjectTest, VirtualDestructor)
1119 EXPECT_GT(slope, 1e10);
1126 EXPECT_LT(slope, -1e10);
1135 Segment s{src, slope, length};
1145 Segment parallel{original, dist};
1148 EXPECT_TRUE(
approx_equal(parallel.length(), original.length(), 1e-6));
1159 EXPECT_GT(s.length(), original_size);
1170 EXPECT_GT(s.length(), original_size);
1193 Point mid = s.mid_point();
1202 EXPECT_THROW(s.mid_perpendicular(1), std::domain_error);
1209TEST(SegmentTriangleTest, SegmentIntersectsTriangle)
1214 EXPECT_TRUE(s.intersects_with(t));
1217TEST(SegmentTriangleTest, SegmentDoesNotIntersectTriangle)
1222 EXPECT_FALSE(s.intersects_with(t));
1225TEST(SegmentTriangleTest, SegmentClippingUsesClosedFilledRegion)
1230 EXPECT_TRUE(inside.intersects_with(t));
1231 EXPECT_EQ(inside.intersection_with(t), inside);
1249 EXPECT_EQ(crossing.intersection_with(t),
1253 EXPECT_EQ(reverse_crossing.intersection_with(t),
1257 EXPECT_EQ(boundary_overlap.intersection_with(t),
1261 EXPECT_EQ(reverse_boundary_overlap.intersection_with(t),
1265TEST(SegmentTriangleTest, DegenerateSegmentClippingDistinguishesInsideAndOutside)
1271 EXPECT_TRUE(inside.intersects_with(t));
1272 EXPECT_EQ(inside.intersection_with(t), inside);
1273 EXPECT_FALSE(outside.intersects_with(t));
1274 EXPECT_THROW(outside.intersection_with(t), std::domain_error);
1283 testing::InitGoogleTest(&argc, argv);
1284 return RUN_ALL_TESTS();
bool intersects_with(const Segment &s) const
Checks if a segment intersects the ellipse.
static bool is_clockwise()
Returns the orientation of the ellipse.
Segment intersection_with(const Segment &sg) const
Computes the intersection segment between a line segment and this ellipse.
Represents a point with rectangular coordinates in a 2D plane.
Geom_Number norm() const
Euclidean norm (vector magnitude).
const Geom_Number & get_x() const noexcept
Gets the x-coordinate value.
const Geom_Number & get_y() const noexcept
Gets the y-coordinate value.
Geom_Number distance_to(const Point &p) const
Calculates the Euclidean distance to another point.
const Point & highest_point() const
Returns the highest point (largest y-coordinate).
const Point & nearest_point(const Point &p1, const Point &p2) const
Returns which of the two points, p1 or p2, is nearer to this point.
Point rotate(const Geom_Number &angle) const
Rotates the point around the origin by a given angle.
Point normalize() const
Returns a normalized copy of this vector (magnitude 1).
Polar representation of a 2D point.
const Geom_Number & get_theta() const
Gets the angle (theta) of the polar point.
const Geom_Number & get_r() const
Gets the radius (magnitude) of the polar point.
An axis-aligned rectangle.
const Geom_Number & get_xmin() const
Gets the minimum x-coordinate.
void set_rect(const Geom_Number &xmin, const Geom_Number &ymin, const Geom_Number &xmax, const Geom_Number &ymax)
Sets the coordinates of the rectangle.
const Geom_Number & get_ymax() const
Gets the maximum y-coordinate.
const Geom_Number & get_ymin() const
Gets the minimum y-coordinate.
const Geom_Number & get_xmax() const
Gets the maximum x-coordinate.
Represents a line segment between two points.
Point mid_point() const
Returns the midpoint of this segment.
Point intersection_with(const Segment &s) const
Computes the intersection point of the infinite lines defined by two segments.
Segment mid_perpendicular(const Geom_Number &dist) const
Returns the perpendicular chord of a given length passing through the midpoint.
const Point & get_tgt_point() const noexcept
Gets the target point of the segment.
const Point & get_src_point() const noexcept
Gets the source point of the segment.
Segment get_perpendicular(const Point &p) const
Constructs a segment perpendicular to this that passes through point p.
Represents a text string positioned at a 2D point.
A non-degenerate triangle defined by three points.
bool vertical
If true, use vertical layout (default).
Main namespace for Aleph-w library functions.
Geom_Number area_of_parallelogram(const Point &a, const Point &b, const Point &c)
Compute the signed area of the parallelogram defined by vectors a->b and a->c.
bool on_segment(const Segment &s, const Point &p)
Return true if p lies on segment s (exact).
Geom_Number cosinus(const Geom_Number &x)
Cosine of x (wrapper over mpfr).
Geom_Number square_root(const Geom_Number &x)
Square root of x (wrapper over mpfr).
double geom_number_to_double(const Geom_Number &n)
Converts a Geom_Number to its double precision representation.
bool diff(const C1 &c1, const C2 &c2, Eq e=Eq())
Check if two containers differ.
mpq_class Geom_Number
Numeric type used by the geometry module.
Geom_Number arctan2(const Geom_Number &m, const Geom_Number &n)
Two-argument arc tangent (wrapper over mpfr).
bool between(const Point &a, const Point &b, const Point &c)
size_t approximate_string_size(const std::string &str)
Estimate the count of printable characters in a LaTeX string.
Geom_Number arctan(const Geom_Number &m)
Arc tangent of m (wrapper over mpfr).
Geom_Number euclidean_distance(const Geom_Number &x, const Geom_Number &y)
Euclidean distance (hypotenuse) of the vector (x, y).
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Geom_Number sinus(const Geom_Number &x)
Sine of x (wrapper over mpfr).
2D point and geometric utilities.
TEST_F(PointTest, DefaultConstruction)
bool approx_equal(const Geom_Number &a, const Geom_Number &b, double tol=EPSILON)
Base class for all geometric objects.