Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
point_in_polygon_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
44# include <geom_algorithms.H>
45
46# include <cassert>
47# include <iostream>
48
49using namespace Aleph;
50using namespace std;
51
52static void print_banner(const char * title)
53{
54 cout << "[Aleph Geometry Example] " << title << "\n";
55 cout << "============================================================\n";
56}
57
58static const char * to_cstr(const PointInPolygonWinding::Location loc)
59{
60 switch (loc)
61 {
62 case PointInPolygonWinding::Location::Inside: return "Inside";
63 case PointInPolygonWinding::Location::Boundary: return "Boundary";
64 case PointInPolygonWinding::Location::Outside: return "Outside";
65 }
66 return "Unknown";
67}
68
70{
71 Polygon poly;
72 poly.add_vertex(Point(0, 0));
73 poly.add_vertex(Point(8, 0));
74 poly.add_vertex(Point(8, 3));
75 poly.add_vertex(Point(5, 3));
76 poly.add_vertex(Point(5, 6));
77 poly.add_vertex(Point(8, 6));
78 poly.add_vertex(Point(8, 9));
79 poly.add_vertex(Point(0, 9));
80 poly.close();
81 return poly;
82}
83
84int main()
85{
86 print_banner("Point-in-Polygon");
87
90
92 queries.append(Point(1, 1)); // inside
93 queries.append(Point(6, 4)); // outside (inside the concavity notch)
94 queries.append(Point(5, 4)); // boundary
95 queries.append(Point(-1, 2)); // outside
96 queries.append(Point(2, 8)); // inside
97
99 labels.append("q0");
100 labels.append("q1");
101 labels.append("q2");
102 labels.append("q3");
103 labels.append("q4");
104
105 for (size_t i = 0; i < queries.size(); ++i)
106 {
107 const Point & p = queries(i);
108 const auto loc = pip.locate(poly, p);
109 cout << labels(i) << " = (" << geom_number_to_double(p.get_x()) << ", "
110 << geom_number_to_double(p.get_y()) << ") -> " << to_cstr(loc) << endl;
111 }
112
113 assert(pip.locate(poly, queries(0)) == PointInPolygonWinding::Location::Inside);
114 assert(pip.locate(poly, queries(1)) == PointInPolygonWinding::Location::Outside);
115 assert(pip.locate(poly, queries(2)) == PointInPolygonWinding::Location::Boundary);
116 assert(pip.locate(poly, queries(3)) == PointInPolygonWinding::Location::Outside);
117 assert(pip.locate(poly, queries(4)) == PointInPolygonWinding::Location::Inside);
118
119 cout << "\nValidation OK: inside / boundary / outside classifications match." << endl;
120 cout << "STATUS: OK" << endl;
121 return 0;
122}
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:139
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:245
Exact point-in-polygon classification via winding number.
Represents a point with rectangular coordinates in a 2D plane.
Definition point.H:229
const Geom_Number & get_x() const noexcept
Gets the x-coordinate value.
Definition point.H:457
const Geom_Number & get_y() const noexcept
Gets the y-coordinate value.
Definition point.H:466
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
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.
double geom_number_to_double(const Geom_Number &n)
Converts a Geom_Number to its double precision representation.
Definition point.H:122
STL namespace.
static Polygon build_concave_polygon()
static const char * to_cstr(const PointInPolygonWinding::Location loc)
static void print_banner(const char *title)