Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
convex_polygon_distance_gjk_example.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
42# include <geom_algorithms.H>
43
44# include <cassert>
45# include <iostream>
46
47using namespace Aleph;
48using namespace std;
49
50static void print_banner(const char * title)
51{
52 cout << "[Aleph Geometry Example] " << title << "\n";
53 cout << "============================================================\n";
54}
55
56int main()
57{
58 print_banner("Convex Polygon Distance (GJK)");
59
60 Polygon a;
61 a.add_vertex(Point(0, 0));
62 a.add_vertex(Point(1, 0));
63 a.add_vertex(Point(1, 1));
64 a.add_vertex(Point(0, 1));
65 a.close();
66
67 Polygon b;
68 b.add_vertex(Point(2, 0));
69 b.add_vertex(Point(3, 0));
70 b.add_vertex(Point(3, 1));
71 b.add_vertex(Point(2, 1));
72 b.close();
73
75 const auto sep = gjk(a, b);
76
77 cout << "Separated case:\n";
78 cout << " intersects = " << (sep.intersects ? "true" : "false") << "\n";
79 cout << " distance^2 = " << sep.distance_squared << "\n";
80 cout << " distance = " << sep.distance << "\n";
81 cout << " closest A = (" << sep.closest_on_first.get_x() << ", "
82 << sep.closest_on_first.get_y() << ")\n";
83 cout << " closest B = (" << sep.closest_on_second.get_x() << ", "
84 << sep.closest_on_second.get_y() << ")\n";
85 assert(not sep.intersects);
86 assert(sep.distance_squared == 1);
87
88 Polygon c;
89 c.add_vertex(Point(0, 0));
90 c.add_vertex(Point(3, 0));
91 c.add_vertex(Point(3, 3));
92 c.add_vertex(Point(0, 3));
93 c.close();
94
95 Polygon d;
96 d.add_vertex(Point(2, 2));
97 d.add_vertex(Point(4, 2));
98 d.add_vertex(Point(4, 4));
99 d.add_vertex(Point(2, 4));
100 d.close();
101
102 const auto ov = gjk(c, d);
103 cout << "\nOverlapping case:\n";
104 cout << " intersects = " << (ov.intersects ? "true" : "false") << "\n";
105 cout << " distance^2 = " << ov.distance_squared << "\n";
106 assert(ov.intersects);
107 assert(ov.distance_squared == 0);
108
109 cout << "STATUS: OK\n";
110 return 0;
111}
112
Distance between two closed convex polygons using GJK.
Represents a point with rectangular coordinates in a 2D plane.
Definition point.H:229
A general (irregular) 2D polygon defined by a sequence of vertices.
Definition polygon.H:246
void add_vertex(const Point &point)
Add a vertex to the polygon.
Definition polygon.H:677
void close()
Close the polygon.
Definition polygon.H:840
static void print_banner(const char *title)
Computational geometry algorithms.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
STL namespace.