test_geometry_2d.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. /**************************************************************************/
  2. /* test_geometry_2d.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef TEST_GEOMETRY_2D_H
  31. #define TEST_GEOMETRY_2D_H
  32. #include "core/math/geometry_2d.h"
  33. #include "thirdparty/doctest/doctest.h"
  34. namespace TestGeometry2D {
  35. TEST_CASE("[Geometry2D] Point in circle") {
  36. CHECK(Geometry2D::is_point_in_circle(Vector2(0, 0), Vector2(0, 0), 1.0));
  37. CHECK(Geometry2D::is_point_in_circle(Vector2(0, 0), Vector2(11.99, 0), 12));
  38. CHECK(Geometry2D::is_point_in_circle(Vector2(-11.99, 0), Vector2(0, 0), 12));
  39. CHECK_FALSE(Geometry2D::is_point_in_circle(Vector2(0, 0), Vector2(12.01, 0), 12));
  40. CHECK_FALSE(Geometry2D::is_point_in_circle(Vector2(-12.01, 0), Vector2(0, 0), 12));
  41. CHECK(Geometry2D::is_point_in_circle(Vector2(7, -42), Vector2(4, -40), 3.7));
  42. CHECK_FALSE(Geometry2D::is_point_in_circle(Vector2(7, -42), Vector2(4, -40), 3.5));
  43. // This tests points on the edge of the circle. They are treated as being inside the circle.
  44. CHECK(Geometry2D::is_point_in_circle(Vector2(1.0, 0.0), Vector2(0, 0), 1.0));
  45. CHECK(Geometry2D::is_point_in_circle(Vector2(0.0, -1.0), Vector2(0, 0), 1.0));
  46. }
  47. TEST_CASE("[Geometry2D] Point in triangle") {
  48. CHECK(Geometry2D::is_point_in_triangle(Vector2(0, 0), Vector2(-1, 1), Vector2(0, -1), Vector2(1, 1)));
  49. CHECK_FALSE(Geometry2D::is_point_in_triangle(Vector2(-1.01, 1.0), Vector2(-1, 1), Vector2(0, -1), Vector2(1, 1)));
  50. CHECK(Geometry2D::is_point_in_triangle(Vector2(3, 2.5), Vector2(1, 4), Vector2(3, 2), Vector2(5, 4)));
  51. CHECK(Geometry2D::is_point_in_triangle(Vector2(-3, -2.5), Vector2(-1, -4), Vector2(-3, -2), Vector2(-5, -4)));
  52. CHECK_FALSE(Geometry2D::is_point_in_triangle(Vector2(0, 0), Vector2(1, 4), Vector2(3, 2), Vector2(5, 4)));
  53. // This tests points on the edge of the triangle. They are treated as being outside the triangle.
  54. // In `is_point_in_circle` and `is_point_in_polygon` they are treated as being inside, so in order the make
  55. // the behavior consistent this may change in the future (see issue #44717 and PR #44274).
  56. CHECK_FALSE(Geometry2D::is_point_in_triangle(Vector2(1, 1), Vector2(-1, 1), Vector2(0, -1), Vector2(1, 1)));
  57. CHECK_FALSE(Geometry2D::is_point_in_triangle(Vector2(0, 1), Vector2(-1, 1), Vector2(0, -1), Vector2(1, 1)));
  58. }
  59. TEST_CASE("[Geometry2D] Point in polygon") {
  60. Vector<Vector2> p;
  61. CHECK_FALSE(Geometry2D::is_point_in_polygon(Vector2(0, 0), p));
  62. p.push_back(Vector2(-88, 120));
  63. p.push_back(Vector2(-74, -38));
  64. p.push_back(Vector2(135, -145));
  65. p.push_back(Vector2(425, 70));
  66. p.push_back(Vector2(68, 112));
  67. p.push_back(Vector2(-120, 370));
  68. p.push_back(Vector2(-323, -145));
  69. CHECK_FALSE(Geometry2D::is_point_in_polygon(Vector2(-350, 0), p));
  70. CHECK_FALSE(Geometry2D::is_point_in_polygon(Vector2(-110, 60), p));
  71. CHECK_FALSE(Geometry2D::is_point_in_polygon(Vector2(412, 96), p));
  72. CHECK_FALSE(Geometry2D::is_point_in_polygon(Vector2(83, 130), p));
  73. CHECK_FALSE(Geometry2D::is_point_in_polygon(Vector2(-320, -153), p));
  74. CHECK(Geometry2D::is_point_in_polygon(Vector2(0, 0), p));
  75. CHECK(Geometry2D::is_point_in_polygon(Vector2(-230, 0), p));
  76. CHECK(Geometry2D::is_point_in_polygon(Vector2(130, -110), p));
  77. CHECK(Geometry2D::is_point_in_polygon(Vector2(370, 55), p));
  78. CHECK(Geometry2D::is_point_in_polygon(Vector2(-160, 190), p));
  79. // This tests points on the edge of the polygon. They are treated as being inside the polygon.
  80. int c = p.size();
  81. for (int i = 0; i < c; i++) {
  82. const Vector2 &p1 = p[i];
  83. CHECK(Geometry2D::is_point_in_polygon(p1, p));
  84. const Vector2 &p2 = p[(i + 1) % c];
  85. Vector2 midpoint((p1 + p2) * 0.5);
  86. CHECK(Geometry2D::is_point_in_polygon(midpoint, p));
  87. }
  88. }
  89. TEST_CASE("[Geometry2D] Polygon clockwise") {
  90. Vector<Vector2> p;
  91. CHECK_FALSE(Geometry2D::is_polygon_clockwise(p));
  92. p.push_back(Vector2(5, -5));
  93. p.push_back(Vector2(-1, -5));
  94. p.push_back(Vector2(-5, -1));
  95. p.push_back(Vector2(-1, 3));
  96. p.push_back(Vector2(1, 5));
  97. CHECK(Geometry2D::is_polygon_clockwise(p));
  98. p.reverse();
  99. CHECK_FALSE(Geometry2D::is_polygon_clockwise(p));
  100. }
  101. TEST_CASE("[Geometry2D] Line intersection") {
  102. Vector2 r;
  103. CHECK(Geometry2D::line_intersects_line(Vector2(2, 0), Vector2(0, 1), Vector2(0, 2), Vector2(1, 0), r));
  104. CHECK(r.is_equal_approx(Vector2(2, 2)));
  105. CHECK(Geometry2D::line_intersects_line(Vector2(-1, 1), Vector2(1, -1), Vector2(4, 1), Vector2(-1, -1), r));
  106. CHECK(r.is_equal_approx(Vector2(1.5, -1.5)));
  107. CHECK(Geometry2D::line_intersects_line(Vector2(-1, 0), Vector2(-1, -1), Vector2(1, 0), Vector2(1, -1), r));
  108. CHECK(r.is_equal_approx(Vector2(0, 1)));
  109. CHECK_FALSE_MESSAGE(
  110. Geometry2D::line_intersects_line(Vector2(-1, 1), Vector2(1, -1), Vector2(0, 1), Vector2(1, -1), r),
  111. "Parallel lines should not intersect.");
  112. }
  113. TEST_CASE("[Geometry2D] Segment intersection") {
  114. Vector2 r;
  115. CHECK(Geometry2D::segment_intersects_segment(Vector2(-1, 1), Vector2(1, -1), Vector2(1, 1), Vector2(-1, -1), &r));
  116. CHECK(r.is_equal_approx(Vector2(0, 0)));
  117. CHECK_FALSE(Geometry2D::segment_intersects_segment(Vector2(-1, 1), Vector2(1, -1), Vector2(1, 1), Vector2(0.1, 0.1), &r));
  118. CHECK_FALSE(Geometry2D::segment_intersects_segment(Vector2(-1, 1), Vector2(1, -1), Vector2(0.1, 0.1), Vector2(1, 1), &r));
  119. CHECK_FALSE_MESSAGE(
  120. Geometry2D::segment_intersects_segment(Vector2(-1, 1), Vector2(1, -1), Vector2(0, 1), Vector2(2, -1), &r),
  121. "Parallel segments should not intersect.");
  122. CHECK_FALSE_MESSAGE(
  123. Geometry2D::segment_intersects_segment(Vector2(1, 2), Vector2(3, 2), Vector2(0, 2), Vector2(-2, 2), &r),
  124. "Non-overlapping collinear segments should not intersect.");
  125. CHECK_MESSAGE(
  126. Geometry2D::segment_intersects_segment(Vector2(0, 0), Vector2(0, 1), Vector2(0, 0), Vector2(1, 0), &r),
  127. "Touching segments should intersect.");
  128. CHECK(r.is_equal_approx(Vector2(0, 0)));
  129. CHECK_MESSAGE(
  130. Geometry2D::segment_intersects_segment(Vector2(0, 1), Vector2(0, 0), Vector2(0, 0), Vector2(1, 0), &r),
  131. "Touching segments should intersect.");
  132. CHECK(r.is_equal_approx(Vector2(0, 0)));
  133. }
  134. TEST_CASE("[Geometry2D] Segment intersection with circle") {
  135. real_t minus_one = -1.0;
  136. real_t zero = 0.0;
  137. real_t one_quarter = 0.25;
  138. real_t three_quarters = 0.75;
  139. real_t one = 1.0;
  140. CHECK_MESSAGE(
  141. Geometry2D::segment_intersects_circle(Vector2(0, 0), Vector2(4, 0), Vector2(0, 0), 1.0) == doctest::Approx(one_quarter),
  142. "Segment from inside to outside of circle should intersect it.");
  143. CHECK_MESSAGE(
  144. Geometry2D::segment_intersects_circle(Vector2(4, 0), Vector2(0, 0), Vector2(0, 0), 1.0) == doctest::Approx(three_quarters),
  145. "Segment from outside to inside of circle should intersect it.");
  146. CHECK_MESSAGE(
  147. Geometry2D::segment_intersects_circle(Vector2(-2, 0), Vector2(2, 0), Vector2(0, 0), 1.0) == doctest::Approx(one_quarter),
  148. "Segment running through circle should intersect it.");
  149. CHECK_MESSAGE(
  150. Geometry2D::segment_intersects_circle(Vector2(2, 0), Vector2(-2, 0), Vector2(0, 0), 1.0) == doctest::Approx(one_quarter),
  151. "Segment running through circle should intersect it.");
  152. CHECK_MESSAGE(
  153. Geometry2D::segment_intersects_circle(Vector2(0, 0), Vector2(1, 0), Vector2(0, 0), 1.0) == doctest::Approx(one),
  154. "Segment starting inside the circle and ending on the circle should intersect it");
  155. CHECK_MESSAGE(
  156. Geometry2D::segment_intersects_circle(Vector2(1, 0), Vector2(0, 0), Vector2(0, 0), 1.0) == doctest::Approx(zero),
  157. "Segment starting on the circle and going inwards should intersect it");
  158. CHECK_MESSAGE(
  159. Geometry2D::segment_intersects_circle(Vector2(1, 0), Vector2(2, 0), Vector2(0, 0), 1.0) == doctest::Approx(zero),
  160. "Segment starting on the circle and going outwards should intersect it");
  161. CHECK_MESSAGE(
  162. Geometry2D::segment_intersects_circle(Vector2(2, 0), Vector2(1, 0), Vector2(0, 0), 1.0) == doctest::Approx(one),
  163. "Segment starting outside the circle and ending on the circle intersect it");
  164. CHECK_MESSAGE(
  165. Geometry2D::segment_intersects_circle(Vector2(-1, 0), Vector2(1, 0), Vector2(0, 0), 2.0) == doctest::Approx(minus_one),
  166. "Segment completely within the circle should not intersect it");
  167. CHECK_MESSAGE(
  168. Geometry2D::segment_intersects_circle(Vector2(1, 0), Vector2(-1, 0), Vector2(0, 0), 2.0) == doctest::Approx(minus_one),
  169. "Segment completely within the circle should not intersect it");
  170. CHECK_MESSAGE(
  171. Geometry2D::segment_intersects_circle(Vector2(2, 0), Vector2(3, 0), Vector2(0, 0), 1.0) == doctest::Approx(minus_one),
  172. "Segment completely outside the circle should not intersect it");
  173. CHECK_MESSAGE(
  174. Geometry2D::segment_intersects_circle(Vector2(3, 0), Vector2(2, 0), Vector2(0, 0), 1.0) == doctest::Approx(minus_one),
  175. "Segment completely outside the circle should not intersect it");
  176. }
  177. TEST_CASE("[Geometry2D] Segment intersection with polygon") {
  178. Vector<Point2> a;
  179. a.push_back(Point2(-2, 2));
  180. a.push_back(Point2(3, 4));
  181. a.push_back(Point2(1, 1));
  182. a.push_back(Point2(2, -2));
  183. a.push_back(Point2(-1, -1));
  184. CHECK_MESSAGE(
  185. Geometry2D::is_segment_intersecting_polygon(Vector2(0, 2), Vector2(2, 2), a),
  186. "Segment from inside to outside of polygon should intersect it.");
  187. CHECK_MESSAGE(
  188. Geometry2D::is_segment_intersecting_polygon(Vector2(2, 2), Vector2(0, 2), a),
  189. "Segment from outside to inside of polygon should intersect it.");
  190. CHECK_MESSAGE(
  191. Geometry2D::is_segment_intersecting_polygon(Vector2(2, 4), Vector2(3, 3), a),
  192. "Segment running through polygon should intersect it.");
  193. CHECK_MESSAGE(
  194. Geometry2D::is_segment_intersecting_polygon(Vector2(3, 3), Vector2(2, 4), a),
  195. "Segment running through polygon should intersect it.");
  196. CHECK_MESSAGE(
  197. Geometry2D::is_segment_intersecting_polygon(Vector2(0, 0), Vector2(1, 1), a),
  198. "Segment starting inside the polygon and ending on the polygon should intersect it");
  199. CHECK_MESSAGE(
  200. Geometry2D::is_segment_intersecting_polygon(Vector2(1, 1), Vector2(0, 0), a),
  201. "Segment starting on the polygon and going inwards should intersect it");
  202. CHECK_MESSAGE(
  203. Geometry2D::is_segment_intersecting_polygon(Vector2(-2, 2), Vector2(-2, -1), a),
  204. "Segment starting on the polygon and going outwards should intersect it");
  205. CHECK_MESSAGE(
  206. Geometry2D::is_segment_intersecting_polygon(Vector2(-2, 1), Vector2(-2, 2), a),
  207. "Segment starting outside the polygon and ending on the polygon intersect it");
  208. CHECK_FALSE_MESSAGE(
  209. Geometry2D::is_segment_intersecting_polygon(Vector2(-1, 2), Vector2(1, -1), a),
  210. "Segment completely within the polygon should not intersect it");
  211. CHECK_FALSE_MESSAGE(
  212. Geometry2D::is_segment_intersecting_polygon(Vector2(1, -1), Vector2(-1, 2), a),
  213. "Segment completely within the polygon should not intersect it");
  214. CHECK_FALSE_MESSAGE(
  215. Geometry2D::is_segment_intersecting_polygon(Vector2(2, 2), Vector2(2, -1), a),
  216. "Segment completely outside the polygon should not intersect it");
  217. CHECK_FALSE_MESSAGE(
  218. Geometry2D::is_segment_intersecting_polygon(Vector2(2, -1), Vector2(2, 2), a),
  219. "Segment completely outside the polygon should not intersect it");
  220. }
  221. TEST_CASE("[Geometry2D] Closest point to segment") {
  222. Vector2 s[] = { Vector2(-4, -4), Vector2(4, 4) };
  223. CHECK(Geometry2D::get_closest_point_to_segment(Vector2(4.1, 4.1), s).is_equal_approx(Vector2(4, 4)));
  224. CHECK(Geometry2D::get_closest_point_to_segment(Vector2(-4.1, -4.1), s).is_equal_approx(Vector2(-4, -4)));
  225. CHECK(Geometry2D::get_closest_point_to_segment(Vector2(-1, 1), s).is_equal_approx(Vector2(0, 0)));
  226. Vector2 t[] = { Vector2(1, -2), Vector2(1, -2) };
  227. CHECK_MESSAGE(
  228. Geometry2D::get_closest_point_to_segment(Vector2(-3, 4), t).is_equal_approx(Vector2(1, -2)),
  229. "Line segment is only a single point. This point should be the closest.");
  230. }
  231. TEST_CASE("[Geometry2D] Closest point to uncapped segment") {
  232. Vector2 s[] = { Vector2(-4, -4), Vector2(4, 4) };
  233. CHECK(Geometry2D::get_closest_point_to_segment_uncapped(Vector2(-1, 1), s).is_equal_approx(Vector2(0, 0)));
  234. CHECK(Geometry2D::get_closest_point_to_segment_uncapped(Vector2(-4, -6), s).is_equal_approx(Vector2(-5, -5)));
  235. CHECK(Geometry2D::get_closest_point_to_segment_uncapped(Vector2(4, 6), s).is_equal_approx(Vector2(5, 5)));
  236. }
  237. TEST_CASE("[Geometry2D] Closest points between segments") {
  238. Vector2 c1, c2;
  239. Geometry2D::get_closest_points_between_segments(Vector2(2, 2), Vector2(3, 3), Vector2(4, 4), Vector2(4, 5), c1, c2);
  240. CHECK(c1.is_equal_approx(Vector2(3, 3)));
  241. CHECK(c2.is_equal_approx(Vector2(4, 4)));
  242. Geometry2D::get_closest_points_between_segments(Vector2(0, 1), Vector2(-2, -1), Vector2(0, 0), Vector2(2, -2), c1, c2);
  243. CHECK(c1.is_equal_approx(Vector2(-0.5, 0.5)));
  244. CHECK(c2.is_equal_approx(Vector2(0, 0)));
  245. Geometry2D::get_closest_points_between_segments(Vector2(-1, 1), Vector2(1, -1), Vector2(1, 1), Vector2(-1, -1), c1, c2);
  246. CHECK(c1.is_equal_approx(Vector2(0, 0)));
  247. CHECK(c2.is_equal_approx(Vector2(0, 0)));
  248. Geometry2D::get_closest_points_between_segments(Vector2(-3, 4), Vector2(-3, 4), Vector2(-4, 3), Vector2(-2, 3), c1, c2);
  249. CHECK_MESSAGE(
  250. c1.is_equal_approx(Vector2(-3, 4)),
  251. "1st line segment is only a point, this point should be the closest point to the 2nd line segment.");
  252. CHECK_MESSAGE(
  253. c2.is_equal_approx(Vector2(-3, 3)),
  254. "1st line segment is only a point, this should not matter when determining the closest point on the 2nd line segment.");
  255. Geometry2D::get_closest_points_between_segments(Vector2(-4, 3), Vector2(-2, 3), Vector2(-3, 4), Vector2(-3, 4), c1, c2);
  256. CHECK_MESSAGE(
  257. c1.is_equal_approx(Vector2(-3, 3)),
  258. "2nd line segment is only a point, this should not matter when determining the closest point on the 1st line segment.");
  259. CHECK_MESSAGE(
  260. c2.is_equal_approx(Vector2(-3, 4)),
  261. "2nd line segment is only a point, this point should be the closest point to the 1st line segment.");
  262. Geometry2D::get_closest_points_between_segments(Vector2(5, -4), Vector2(5, -4), Vector2(-2, 1), Vector2(-2, 1), c1, c2);
  263. CHECK_MESSAGE(
  264. c1.is_equal_approx(Vector2(5, -4)),
  265. "Both line segments are only a point. On the 1st line segment, that point should be the closest point to the 2nd line segment.");
  266. CHECK_MESSAGE(
  267. c2.is_equal_approx(Vector2(-2, 1)),
  268. "Both line segments are only a point. On the 2nd line segment, that point should be the closest point to the 1st line segment.");
  269. }
  270. TEST_CASE("[Geometry2D] Make atlas") {
  271. Vector<Point2i> result;
  272. Size2i size;
  273. Vector<Size2i> r;
  274. r.push_back(Size2i(2, 2));
  275. Geometry2D::make_atlas(r, result, size);
  276. CHECK(size == Size2i(2, 2));
  277. CHECK(result.size() == r.size());
  278. r.clear();
  279. result.clear();
  280. r.push_back(Size2i(1, 2));
  281. r.push_back(Size2i(3, 4));
  282. r.push_back(Size2i(5, 6));
  283. r.push_back(Size2i(7, 8));
  284. Geometry2D::make_atlas(r, result, size);
  285. CHECK(result.size() == r.size());
  286. }
  287. TEST_CASE("[Geometry2D] Polygon intersection") {
  288. Vector<Point2> a;
  289. Vector<Point2> b;
  290. Vector<Vector<Point2>> r;
  291. a.push_back(Point2(30, 60));
  292. a.push_back(Point2(70, 5));
  293. a.push_back(Point2(200, 40));
  294. a.push_back(Point2(80, 200));
  295. SUBCASE("[Geometry2D] Both polygons are empty") {
  296. r = Geometry2D::intersect_polygons(Vector<Point2>(), Vector<Point2>());
  297. CHECK_MESSAGE(r.is_empty(), "Both polygons are empty. The intersection should also be empty.");
  298. }
  299. SUBCASE("[Geometry2D] One polygon is empty") {
  300. r = Geometry2D::intersect_polygons(a, b);
  301. REQUIRE_MESSAGE(r.is_empty(), "One polygon is empty. The intersection should also be empty.");
  302. }
  303. SUBCASE("[Geometry2D] Basic intersection") {
  304. b.push_back(Point2(200, 300));
  305. b.push_back(Point2(90, 200));
  306. b.push_back(Point2(50, 100));
  307. b.push_back(Point2(200, 90));
  308. r = Geometry2D::intersect_polygons(a, b);
  309. REQUIRE_MESSAGE(r.size() == 1, "The polygons should intersect each other with 1 resulting intersection polygon.");
  310. REQUIRE_MESSAGE(r[0].size() == 3, "The resulting intersection polygon should have 3 vertices.");
  311. CHECK(r[0][0].is_equal_approx(Point2(86.52174, 191.30436)));
  312. CHECK(r[0][1].is_equal_approx(Point2(50, 100)));
  313. CHECK(r[0][2].is_equal_approx(Point2(160.52632, 92.63157)));
  314. }
  315. SUBCASE("[Geometry2D] Intersection with one polygon being completely inside the other polygon") {
  316. b.push_back(Point2(80, 100));
  317. b.push_back(Point2(50, 50));
  318. b.push_back(Point2(150, 50));
  319. r = Geometry2D::intersect_polygons(a, b);
  320. REQUIRE_MESSAGE(r.size() == 1, "The polygons should intersect each other with 1 resulting intersection polygon.");
  321. REQUIRE_MESSAGE(r[0].size() == 3, "The resulting intersection polygon should have 3 vertices.");
  322. CHECK(r[0][0].is_equal_approx(b[0]));
  323. CHECK(r[0][1].is_equal_approx(b[1]));
  324. CHECK(r[0][2].is_equal_approx(b[2]));
  325. }
  326. SUBCASE("[Geometry2D] No intersection with 2 non-empty polygons") {
  327. b.push_back(Point2(150, 150));
  328. b.push_back(Point2(250, 100));
  329. b.push_back(Point2(300, 200));
  330. r = Geometry2D::intersect_polygons(a, b);
  331. REQUIRE_MESSAGE(r.is_empty(), "The polygons should not intersect each other.");
  332. }
  333. SUBCASE("[Geometry2D] Intersection with 2 resulting polygons") {
  334. a.clear();
  335. a.push_back(Point2(70, 5));
  336. a.push_back(Point2(140, 7));
  337. a.push_back(Point2(100, 52));
  338. a.push_back(Point2(170, 50));
  339. a.push_back(Point2(60, 125));
  340. b.push_back(Point2(70, 105));
  341. b.push_back(Point2(115, 55));
  342. b.push_back(Point2(90, 15));
  343. b.push_back(Point2(160, 50));
  344. r = Geometry2D::intersect_polygons(a, b);
  345. REQUIRE_MESSAGE(r.size() == 2, "The polygons should intersect each other with 2 resulting intersection polygons.");
  346. REQUIRE_MESSAGE(r[0].size() == 4, "The resulting intersection polygon should have 4 vertices.");
  347. CHECK(r[0][0].is_equal_approx(Point2(70, 105)));
  348. CHECK(r[0][1].is_equal_approx(Point2(115, 55)));
  349. CHECK(r[0][2].is_equal_approx(Point2(112.894737, 51.63158)));
  350. CHECK(r[0][3].is_equal_approx(Point2(159.509537, 50.299728)));
  351. REQUIRE_MESSAGE(r[1].size() == 3, "The intersection polygon should have 3 vertices.");
  352. CHECK(r[1][0].is_equal_approx(Point2(119.692307, 29.846149)));
  353. CHECK(r[1][1].is_equal_approx(Point2(107.706421, 43.33028)));
  354. CHECK(r[1][2].is_equal_approx(Point2(90, 15)));
  355. }
  356. }
  357. TEST_CASE("[Geometry2D] Merge polygons") {
  358. Vector<Point2> a;
  359. Vector<Point2> b;
  360. Vector<Vector<Point2>> r;
  361. a.push_back(Point2(225, 180));
  362. a.push_back(Point2(160, 230));
  363. a.push_back(Point2(20, 212));
  364. a.push_back(Point2(50, 115));
  365. SUBCASE("[Geometry2D] Both polygons are empty") {
  366. r = Geometry2D::merge_polygons(Vector<Point2>(), Vector<Point2>());
  367. REQUIRE_MESSAGE(r.is_empty(), "Both polygons are empty. The union should also be empty.");
  368. }
  369. SUBCASE("[Geometry2D] One polygon is empty") {
  370. r = Geometry2D::merge_polygons(a, b);
  371. REQUIRE_MESSAGE(r.size() == 1, "One polygon is non-empty. There should be 1 resulting merged polygon.");
  372. REQUIRE_MESSAGE(r[0].size() == 4, "The resulting merged polygon should have 4 vertices.");
  373. CHECK(r[0][0].is_equal_approx(a[0]));
  374. CHECK(r[0][1].is_equal_approx(a[1]));
  375. CHECK(r[0][2].is_equal_approx(a[2]));
  376. CHECK(r[0][3].is_equal_approx(a[3]));
  377. }
  378. SUBCASE("[Geometry2D] Basic merge with 2 polygons") {
  379. b.push_back(Point2(180, 190));
  380. b.push_back(Point2(60, 140));
  381. b.push_back(Point2(160, 80));
  382. r = Geometry2D::merge_polygons(a, b);
  383. REQUIRE_MESSAGE(r.size() == 1, "The merged polygons should result in 1 polygon.");
  384. REQUIRE_MESSAGE(r[0].size() == 7, "The resulting merged polygon should have 7 vertices.");
  385. CHECK(r[0][0].is_equal_approx(Point2(174.791077, 161.350967)));
  386. CHECK(r[0][1].is_equal_approx(Point2(225, 180)));
  387. CHECK(r[0][2].is_equal_approx(Point2(160, 230)));
  388. CHECK(r[0][3].is_equal_approx(Point2(20, 212)));
  389. CHECK(r[0][4].is_equal_approx(Point2(50, 115)));
  390. CHECK(r[0][5].is_equal_approx(Point2(81.911758, 126.852943)));
  391. CHECK(r[0][6].is_equal_approx(Point2(160, 80)));
  392. }
  393. SUBCASE("[Geometry2D] Merge with 2 resulting merged polygons (outline and hole)") {
  394. b.push_back(Point2(180, 190));
  395. b.push_back(Point2(140, 125));
  396. b.push_back(Point2(60, 140));
  397. b.push_back(Point2(160, 80));
  398. r = Geometry2D::merge_polygons(a, b);
  399. REQUIRE_MESSAGE(r.size() == 2, "The merged polygons should result in 2 polygons.");
  400. REQUIRE_MESSAGE(!Geometry2D::is_polygon_clockwise(r[0]), "The merged polygon (outline) should be counter-clockwise.");
  401. REQUIRE_MESSAGE(r[0].size() == 7, "The resulting merged polygon (outline) should have 7 vertices.");
  402. CHECK(r[0][0].is_equal_approx(Point2(174.791077, 161.350967)));
  403. CHECK(r[0][1].is_equal_approx(Point2(225, 180)));
  404. CHECK(r[0][2].is_equal_approx(Point2(160, 230)));
  405. CHECK(r[0][3].is_equal_approx(Point2(20, 212)));
  406. CHECK(r[0][4].is_equal_approx(Point2(50, 115)));
  407. CHECK(r[0][5].is_equal_approx(Point2(81.911758, 126.852943)));
  408. CHECK(r[0][6].is_equal_approx(Point2(160, 80)));
  409. REQUIRE_MESSAGE(Geometry2D::is_polygon_clockwise(r[1]), "The resulting merged polygon (hole) should be clockwise.");
  410. REQUIRE_MESSAGE(r[1].size() == 3, "The resulting merged polygon (hole) should have 3 vertices.");
  411. CHECK(r[1][0].is_equal_approx(Point2(98.083069, 132.859421)));
  412. CHECK(r[1][1].is_equal_approx(Point2(158.689453, 155.370377)));
  413. CHECK(r[1][2].is_equal_approx(Point2(140, 125)));
  414. }
  415. }
  416. TEST_CASE("[Geometry2D] Clip polygons") {
  417. Vector<Point2> a;
  418. Vector<Point2> b;
  419. Vector<Vector<Point2>> r;
  420. a.push_back(Point2(225, 180));
  421. a.push_back(Point2(160, 230));
  422. a.push_back(Point2(20, 212));
  423. a.push_back(Point2(50, 115));
  424. SUBCASE("[Geometry2D] Both polygons are empty") {
  425. r = Geometry2D::clip_polygons(Vector<Point2>(), Vector<Point2>());
  426. CHECK_MESSAGE(r.is_empty(), "Both polygons are empty. The clip should also be empty.");
  427. }
  428. SUBCASE("[Geometry2D] Basic clip with one result polygon") {
  429. b.push_back(Point2(250, 170));
  430. b.push_back(Point2(175, 270));
  431. b.push_back(Point2(120, 260));
  432. b.push_back(Point2(25, 80));
  433. r = Geometry2D::clip_polygons(a, b);
  434. REQUIRE_MESSAGE(r.size() == 1, "The clipped polygons should result in 1 polygon.");
  435. REQUIRE_MESSAGE(r[0].size() == 3, "The resulting clipped polygon should have 3 vertices.");
  436. CHECK(r[0][0].is_equal_approx(Point2(100.102173, 222.298843)));
  437. CHECK(r[0][1].is_equal_approx(Point2(20, 212)));
  438. CHECK(r[0][2].is_equal_approx(Point2(47.588089, 122.798492)));
  439. }
  440. SUBCASE("[Geometry2D] Polygon b completely overlaps polygon a") {
  441. b.push_back(Point2(250, 170));
  442. b.push_back(Point2(175, 270));
  443. b.push_back(Point2(10, 210));
  444. b.push_back(Point2(55, 80));
  445. r = Geometry2D::clip_polygons(a, b);
  446. CHECK_MESSAGE(r.is_empty(), "Polygon 'b' completely overlaps polygon 'a'. This should result in no clipped polygons.");
  447. }
  448. SUBCASE("[Geometry2D] Polygon a completely overlaps polygon b") {
  449. b.push_back(Point2(150, 200));
  450. b.push_back(Point2(65, 190));
  451. b.push_back(Point2(80, 140));
  452. r = Geometry2D::clip_polygons(a, b);
  453. REQUIRE_MESSAGE(r.size() == 2, "Polygon 'a' completely overlaps polygon 'b'. This should result in 2 clipped polygons.");
  454. REQUIRE_MESSAGE(r[0].size() == 4, "The resulting clipped polygon should have 4 vertices.");
  455. REQUIRE_MESSAGE(!Geometry2D::is_polygon_clockwise(r[0]), "The resulting clipped polygon (outline) should be counter-clockwise.");
  456. CHECK(r[0][0].is_equal_approx(a[0]));
  457. CHECK(r[0][1].is_equal_approx(a[1]));
  458. CHECK(r[0][2].is_equal_approx(a[2]));
  459. CHECK(r[0][3].is_equal_approx(a[3]));
  460. REQUIRE_MESSAGE(r[1].size() == 3, "The resulting clipped polygon should have 3 vertices.");
  461. REQUIRE_MESSAGE(Geometry2D::is_polygon_clockwise(r[1]), "The resulting clipped polygon (hole) should be clockwise.");
  462. CHECK(r[1][0].is_equal_approx(b[1]));
  463. CHECK(r[1][1].is_equal_approx(b[0]));
  464. CHECK(r[1][2].is_equal_approx(b[2]));
  465. }
  466. }
  467. TEST_CASE("[Geometry2D] Exclude polygons") {
  468. Vector<Point2> a;
  469. Vector<Point2> b;
  470. Vector<Vector<Point2>> r;
  471. a.push_back(Point2(225, 180));
  472. a.push_back(Point2(160, 230));
  473. a.push_back(Point2(20, 212));
  474. a.push_back(Point2(50, 115));
  475. SUBCASE("[Geometry2D] Both polygons are empty") {
  476. r = Geometry2D::exclude_polygons(Vector<Point2>(), Vector<Point2>());
  477. CHECK_MESSAGE(r.is_empty(), "Both polygons are empty. The excluded polygon should also be empty.");
  478. }
  479. SUBCASE("[Geometry2D] One polygon is empty") {
  480. r = Geometry2D::exclude_polygons(a, b);
  481. REQUIRE_MESSAGE(r.size() == 1, "One polygon is non-empty. There should be 1 resulting excluded polygon.");
  482. REQUIRE_MESSAGE(r[0].size() == 4, "The resulting excluded polygon should have 4 vertices.");
  483. CHECK(r[0][0].is_equal_approx(a[0]));
  484. CHECK(r[0][1].is_equal_approx(a[1]));
  485. CHECK(r[0][2].is_equal_approx(a[2]));
  486. CHECK(r[0][3].is_equal_approx(a[3]));
  487. }
  488. SUBCASE("[Geometry2D] Exclude with 2 resulting polygons (outline and hole)") {
  489. b.push_back(Point2(140, 160));
  490. b.push_back(Point2(150, 220));
  491. b.push_back(Point2(40, 200));
  492. b.push_back(Point2(60, 140));
  493. r = Geometry2D::exclude_polygons(a, b);
  494. REQUIRE_MESSAGE(r.size() == 2, "There should be 2 resulting excluded polygons (outline and hole).");
  495. REQUIRE_MESSAGE(r[0].size() == 4, "The resulting excluded polygon should have 4 vertices.");
  496. REQUIRE_MESSAGE(!Geometry2D::is_polygon_clockwise(r[0]), "The resulting excluded polygon (outline) should be counter-clockwise.");
  497. CHECK(r[0][0].is_equal_approx(a[0]));
  498. CHECK(r[0][1].is_equal_approx(a[1]));
  499. CHECK(r[0][2].is_equal_approx(a[2]));
  500. CHECK(r[0][3].is_equal_approx(a[3]));
  501. REQUIRE_MESSAGE(r[1].size() == 4, "The resulting excluded polygon should have 4 vertices.");
  502. REQUIRE_MESSAGE(Geometry2D::is_polygon_clockwise(r[1]), "The resulting excluded polygon (hole) should be clockwise.");
  503. CHECK(r[1][0].is_equal_approx(Point2(40, 200)));
  504. CHECK(r[1][1].is_equal_approx(Point2(150, 220)));
  505. CHECK(r[1][2].is_equal_approx(Point2(140, 160)));
  506. CHECK(r[1][3].is_equal_approx(Point2(60, 140)));
  507. }
  508. }
  509. TEST_CASE("[Geometry2D] Intersect polyline with polygon") {
  510. Vector<Vector2> l;
  511. Vector<Vector2> p;
  512. Vector<Vector<Point2>> r;
  513. l.push_back(Vector2(100, 90));
  514. l.push_back(Vector2(120, 250));
  515. p.push_back(Vector2(225, 180));
  516. p.push_back(Vector2(160, 230));
  517. p.push_back(Vector2(20, 212));
  518. p.push_back(Vector2(50, 115));
  519. SUBCASE("[Geometry2D] Both line and polygon are empty") {
  520. r = Geometry2D::intersect_polyline_with_polygon(Vector<Vector2>(), Vector<Vector2>());
  521. CHECK_MESSAGE(r.is_empty(), "Both line and polygon are empty. The intersection line should also be empty.");
  522. }
  523. SUBCASE("[Geometry2D] Line is non-empty and polygon is empty") {
  524. r = Geometry2D::intersect_polyline_with_polygon(l, Vector<Vector2>());
  525. CHECK_MESSAGE(r.is_empty(), "The polygon is empty while the line is non-empty. The intersection line should be empty.");
  526. }
  527. SUBCASE("[Geometry2D] Basic intersection with 1 resulting intersection line") {
  528. r = Geometry2D::intersect_polyline_with_polygon(l, p);
  529. REQUIRE_MESSAGE(r.size() == 1, "There should be 1 resulting intersection line.");
  530. REQUIRE_MESSAGE(r[0].size() == 2, "The resulting intersection line should have 2 vertices.");
  531. CHECK(r[0][0].is_equal_approx(Vector2(105.711609, 135.692886)));
  532. CHECK(r[0][1].is_equal_approx(Vector2(116.805809, 224.446457)));
  533. }
  534. SUBCASE("[Geometry2D] Complex intersection with 2 resulting intersection lines") {
  535. l.clear();
  536. l.push_back(Vector2(100, 90));
  537. l.push_back(Vector2(190, 255));
  538. l.push_back(Vector2(135, 260));
  539. l.push_back(Vector2(57, 200));
  540. l.push_back(Vector2(50, 170));
  541. l.push_back(Vector2(15, 155));
  542. r = Geometry2D::intersect_polyline_with_polygon(l, p);
  543. REQUIRE_MESSAGE(r.size() == 2, "There should be 2 resulting intersection lines.");
  544. REQUIRE_MESSAGE(r[0].size() == 2, "The resulting intersection line should have 2 vertices.");
  545. CHECK(r[0][0].is_equal_approx(Vector2(129.804565, 144.641693)));
  546. CHECK(r[0][1].is_equal_approx(Vector2(171.527084, 221.132996)));
  547. REQUIRE_MESSAGE(r[1].size() == 4, "The resulting intersection line should have 4 vertices.");
  548. CHECK(r[1][0].is_equal_approx(Vector2(83.15609, 220.120087)));
  549. CHECK(r[1][1].is_equal_approx(Vector2(57, 200)));
  550. CHECK(r[1][2].is_equal_approx(Vector2(50, 170)));
  551. CHECK(r[1][3].is_equal_approx(Vector2(34.980492, 163.563065)));
  552. }
  553. }
  554. TEST_CASE("[Geometry2D] Clip polyline with polygon") {
  555. Vector<Vector2> l;
  556. Vector<Vector2> p;
  557. Vector<Vector<Point2>> r;
  558. l.push_back(Vector2(70, 140));
  559. l.push_back(Vector2(160, 320));
  560. p.push_back(Vector2(225, 180));
  561. p.push_back(Vector2(160, 230));
  562. p.push_back(Vector2(20, 212));
  563. p.push_back(Vector2(50, 115));
  564. SUBCASE("[Geometry2D] Both line and polygon are empty") {
  565. r = Geometry2D::clip_polyline_with_polygon(Vector<Vector2>(), Vector<Vector2>());
  566. CHECK_MESSAGE(r.is_empty(), "Both line and polygon are empty. The clipped line should also be empty.");
  567. }
  568. SUBCASE("[Geometry2D] Polygon is empty and line is non-empty") {
  569. r = Geometry2D::clip_polyline_with_polygon(l, Vector<Vector2>());
  570. REQUIRE_MESSAGE(r.size() == 1, "There should be 1 resulting clipped line.");
  571. REQUIRE_MESSAGE(r[0].size() == 2, "The resulting clipped line should have 2 vertices.");
  572. CHECK(r[0][0].is_equal_approx(l[0]));
  573. CHECK(r[0][1].is_equal_approx(l[1]));
  574. }
  575. SUBCASE("[Geometry2D] Basic clip with 1 resulting clipped line") {
  576. r = Geometry2D::clip_polyline_with_polygon(l, p);
  577. REQUIRE_MESSAGE(r.size() == 1, "There should be 1 resulting clipped line.");
  578. REQUIRE_MESSAGE(r[0].size() == 2, "The resulting clipped line should have 2 vertices.");
  579. CHECK(r[0][0].is_equal_approx(Vector2(111.908401, 223.816803)));
  580. CHECK(r[0][1].is_equal_approx(Vector2(160, 320)));
  581. }
  582. SUBCASE("[Geometry2D] Complex clip with 2 resulting clipped lines") {
  583. l.clear();
  584. l.push_back(Vector2(55, 70));
  585. l.push_back(Vector2(50, 190));
  586. l.push_back(Vector2(120, 165));
  587. l.push_back(Vector2(122, 250));
  588. l.push_back(Vector2(160, 320));
  589. r = Geometry2D::clip_polyline_with_polygon(l, p);
  590. REQUIRE_MESSAGE(r.size() == 2, "There should be 2 resulting clipped lines.");
  591. REQUIRE_MESSAGE(r[0].size() == 3, "The resulting clipped line should have 3 vertices.");
  592. CHECK(r[0][0].is_equal_approx(Vector2(160, 320)));
  593. CHECK(r[0][1].is_equal_approx(Vector2(122, 250)));
  594. CHECK(r[0][2].is_equal_approx(Vector2(121.412682, 225.038757)));
  595. REQUIRE_MESSAGE(r[1].size() == 2, "The resulting clipped line should have 2 vertices.");
  596. CHECK(r[1][0].is_equal_approx(Vector2(53.07737, 116.143021)));
  597. CHECK(r[1][1].is_equal_approx(Vector2(55, 70)));
  598. }
  599. }
  600. TEST_CASE("[Geometry2D] Convex hull") {
  601. Vector<Point2> a;
  602. Vector<Point2> r;
  603. a.push_back(Point2(-4, -8));
  604. a.push_back(Point2(-10, -4));
  605. a.push_back(Point2(8, 2));
  606. a.push_back(Point2(-6, 10));
  607. a.push_back(Point2(-12, 4));
  608. a.push_back(Point2(10, -8));
  609. a.push_back(Point2(4, 8));
  610. SUBCASE("[Geometry2D] No points") {
  611. r = Geometry2D::convex_hull(Vector<Vector2>());
  612. CHECK_MESSAGE(r.is_empty(), "The convex hull should be empty if there are no input points.");
  613. }
  614. SUBCASE("[Geometry2D] Single point") {
  615. Vector<Point2> b;
  616. b.push_back(Point2(4, -3));
  617. r = Geometry2D::convex_hull(b);
  618. REQUIRE_MESSAGE(r.size() == 1, "Convex hull should contain 1 point.");
  619. CHECK(r[0].is_equal_approx(b[0]));
  620. }
  621. SUBCASE("[Geometry2D] All points form the convex hull") {
  622. r = Geometry2D::convex_hull(a);
  623. REQUIRE_MESSAGE(r.size() == 8, "Convex hull should contain 8 points.");
  624. CHECK(r[0].is_equal_approx(Point2(-12, 4)));
  625. CHECK(r[1].is_equal_approx(Point2(-10, -4)));
  626. CHECK(r[2].is_equal_approx(Point2(-4, -8)));
  627. CHECK(r[3].is_equal_approx(Point2(10, -8)));
  628. CHECK(r[4].is_equal_approx(Point2(8, 2)));
  629. CHECK(r[5].is_equal_approx(Point2(4, 8)));
  630. CHECK(r[6].is_equal_approx(Point2(-6, 10)));
  631. CHECK(r[7].is_equal_approx(Point2(-12, 4)));
  632. }
  633. SUBCASE("[Geometry2D] Add extra points inside original convex hull") {
  634. a.push_back(Point2(-4, -8));
  635. a.push_back(Point2(0, 0));
  636. a.push_back(Point2(0, 8));
  637. a.push_back(Point2(-10, -3));
  638. a.push_back(Point2(9, -4));
  639. a.push_back(Point2(6, 4));
  640. r = Geometry2D::convex_hull(a);
  641. REQUIRE_MESSAGE(r.size() == 8, "Convex hull should contain 8 points.");
  642. CHECK(r[0].is_equal_approx(Point2(-12, 4)));
  643. CHECK(r[1].is_equal_approx(Point2(-10, -4)));
  644. CHECK(r[2].is_equal_approx(Point2(-4, -8)));
  645. CHECK(r[3].is_equal_approx(Point2(10, -8)));
  646. CHECK(r[4].is_equal_approx(Point2(8, 2)));
  647. CHECK(r[5].is_equal_approx(Point2(4, 8)));
  648. CHECK(r[6].is_equal_approx(Point2(-6, 10)));
  649. CHECK(r[7].is_equal_approx(Point2(-12, 4)));
  650. }
  651. SUBCASE("[Geometry2D] Add extra points on border of original convex hull") {
  652. a.push_back(Point2(9, -3));
  653. a.push_back(Point2(-2, -8));
  654. r = Geometry2D::convex_hull(a);
  655. REQUIRE_MESSAGE(r.size() == 8, "Convex hull should contain 8 points.");
  656. CHECK(r[0].is_equal_approx(Point2(-12, 4)));
  657. CHECK(r[1].is_equal_approx(Point2(-10, -4)));
  658. CHECK(r[2].is_equal_approx(Point2(-4, -8)));
  659. CHECK(r[3].is_equal_approx(Point2(10, -8)));
  660. CHECK(r[4].is_equal_approx(Point2(8, 2)));
  661. CHECK(r[5].is_equal_approx(Point2(4, 8)));
  662. CHECK(r[6].is_equal_approx(Point2(-6, 10)));
  663. CHECK(r[7].is_equal_approx(Point2(-12, 4)));
  664. }
  665. SUBCASE("[Geometry2D] Add extra points outside border of original convex hull") {
  666. a.push_back(Point2(-11, -1));
  667. a.push_back(Point2(7, 6));
  668. r = Geometry2D::convex_hull(a);
  669. REQUIRE_MESSAGE(r.size() == 10, "Convex hull should contain 10 points.");
  670. CHECK(r[0].is_equal_approx(Point2(-12, 4)));
  671. CHECK(r[1].is_equal_approx(Point2(-11, -1)));
  672. CHECK(r[2].is_equal_approx(Point2(-10, -4)));
  673. CHECK(r[3].is_equal_approx(Point2(-4, -8)));
  674. CHECK(r[4].is_equal_approx(Point2(10, -8)));
  675. CHECK(r[5].is_equal_approx(Point2(8, 2)));
  676. CHECK(r[6].is_equal_approx(Point2(7, 6)));
  677. CHECK(r[7].is_equal_approx(Point2(4, 8)));
  678. CHECK(r[8].is_equal_approx(Point2(-6, 10)));
  679. CHECK(r[9].is_equal_approx(Point2(-12, 4)));
  680. }
  681. }
  682. TEST_CASE("[Geometry2D] Bresenham line") {
  683. Vector<Vector2i> r;
  684. SUBCASE("[Geometry2D] Single point") {
  685. r = Geometry2D::bresenham_line(Point2i(0, 0), Point2i(0, 0));
  686. REQUIRE_MESSAGE(r.size() == 1, "The Bresenham line should contain exactly one point.");
  687. CHECK(r[0] == Vector2i(0, 0));
  688. }
  689. SUBCASE("[Geometry2D] Line parallel to x-axis") {
  690. r = Geometry2D::bresenham_line(Point2i(1, 2), Point2i(5, 2));
  691. REQUIRE_MESSAGE(r.size() == 5, "The Bresenham line should contain exactly five points.");
  692. CHECK(r[0] == Vector2i(1, 2));
  693. CHECK(r[1] == Vector2i(2, 2));
  694. CHECK(r[2] == Vector2i(3, 2));
  695. CHECK(r[3] == Vector2i(4, 2));
  696. CHECK(r[4] == Vector2i(5, 2));
  697. }
  698. SUBCASE("[Geometry2D] 45 degree line from the origin") {
  699. r = Geometry2D::bresenham_line(Point2i(0, 0), Point2i(4, 4));
  700. REQUIRE_MESSAGE(r.size() == 5, "The Bresenham line should contain exactly five points.");
  701. CHECK(r[0] == Vector2i(0, 0));
  702. CHECK(r[1] == Vector2i(1, 1));
  703. CHECK(r[2] == Vector2i(2, 2));
  704. CHECK(r[3] == Vector2i(3, 3));
  705. CHECK(r[4] == Vector2i(4, 4));
  706. }
  707. SUBCASE("[Geometry2D] Sloped line going up one unit") {
  708. r = Geometry2D::bresenham_line(Point2i(0, 0), Point2i(4, 1));
  709. REQUIRE_MESSAGE(r.size() == 5, "The Bresenham line should contain exactly five points.");
  710. CHECK(r[0] == Vector2i(0, 0));
  711. CHECK(r[1] == Vector2i(1, 0));
  712. CHECK(r[2] == Vector2i(2, 0));
  713. CHECK(r[3] == Vector2i(3, 1));
  714. CHECK(r[4] == Vector2i(4, 1));
  715. }
  716. SUBCASE("[Geometry2D] Sloped line going up two units") {
  717. r = Geometry2D::bresenham_line(Point2i(0, 0), Point2i(4, 2));
  718. REQUIRE_MESSAGE(r.size() == 5, "The Bresenham line should contain exactly five points.");
  719. CHECK(r[0] == Vector2i(0, 0));
  720. CHECK(r[1] == Vector2i(1, 0));
  721. CHECK(r[2] == Vector2i(2, 1));
  722. CHECK(r[3] == Vector2i(3, 1));
  723. CHECK(r[4] == Vector2i(4, 2));
  724. }
  725. SUBCASE("[Geometry2D] Long sloped line") {
  726. r = Geometry2D::bresenham_line(Point2i(0, 0), Point2i(11, 5));
  727. REQUIRE_MESSAGE(r.size() == 12, "The Bresenham line should contain exactly twelve points.");
  728. CHECK(r[0] == Vector2i(0, 0));
  729. CHECK(r[1] == Vector2i(1, 0));
  730. CHECK(r[2] == Vector2i(2, 1));
  731. CHECK(r[3] == Vector2i(3, 1));
  732. CHECK(r[4] == Vector2i(4, 2));
  733. CHECK(r[5] == Vector2i(5, 2));
  734. CHECK(r[6] == Vector2i(6, 3));
  735. CHECK(r[7] == Vector2i(7, 3));
  736. CHECK(r[8] == Vector2i(8, 4));
  737. CHECK(r[9] == Vector2i(9, 4));
  738. CHECK(r[10] == Vector2i(10, 5));
  739. CHECK(r[11] == Vector2i(11, 5));
  740. }
  741. }
  742. } // namespace TestGeometry2D
  743. #endif // TEST_GEOMETRY_2D_H