Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
segment_segment_intersection_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
31
45#include <cassert>
46#include <iostream>
47#include <geom_algorithms.H>
48
49using namespace std;
50
52{
53 switch (kind)
54 {
55 case SegmentSegmentIntersection::Kind::NONE: return "NONE";
56 case SegmentSegmentIntersection::Kind::POINT: return "POINT";
57 case SegmentSegmentIntersection::Kind::OVERLAP: return "OVERLAP";
58 }
59 return "?";
60}
61
62static void print_case(const char * name,
63 const Segment & a,
64 const Segment & b,
66{
67 cout << "Case: " << name << '\n';
68 cout << " A = " << a.get_src_point().to_string()
69 << " -> " << a.get_tgt_point().to_string() << '\n';
70 cout << " B = " << b.get_src_point().to_string()
71 << " -> " << b.get_tgt_point().to_string() << '\n';
72 cout << " kind = " << kind_str(result.kind) << '\n';
73
74 if (result.kind == SegmentSegmentIntersection::Kind::POINT)
75 cout << " point = " << result.point.to_string() << '\n';
76 else if (result.kind == SegmentSegmentIntersection::Kind::OVERLAP)
77 cout << " overlap = "
78 << result.overlap.get_src_point().to_string()
79 << " -> "
80 << result.overlap.get_tgt_point().to_string() << '\n';
81
82 cout << '\n';
83}
84
85int main()
86{
87 cout << "[Aleph Geometry Example] Dedicated O(1) Segment-Segment Intersection\n";
88 cout << "====================================================================\n\n";
89
91
92 // 1) Proper crossing.
93 {
94 const Segment a(Point(0, 0), Point(4, 4));
95 const Segment b(Point(0, 4), Point(4, 0));
96 const auto result = pair_ix(a, b);
97 print_case("Proper cross", a, b, result);
98 assert(result.kind == SegmentSegmentIntersection::Kind::POINT);
99 assert(result.point == Point(2, 2));
100 }
101
102 // 2) Endpoint touch.
103 {
104 const Segment a(Point(0, 0), Point(4, 0));
105 const Segment b(Point(4, 0), Point(4, 3));
106 const auto result = pair_ix(a, b);
107 print_case("Endpoint touching", a, b, result);
108 assert(result.kind == SegmentSegmentIntersection::Kind::POINT);
109 assert(result.point == Point(4, 0));
110 }
111
112 // 3) Collinear overlap (interval result).
113 {
114 const Segment a(Point(0, 0), Point(6, 0));
115 const Segment b(Point(2, 0), Point(5, 0));
116 const auto result = pair_ix(a, b);
117 print_case("Collinear overlap", a, b, result);
118 assert(result.kind == SegmentSegmentIntersection::Kind::OVERLAP);
119 assert(result.overlap.get_src_point() == Point(2, 0));
120 assert(result.overlap.get_tgt_point() == Point(5, 0));
121 }
122
123 // 4) Disjoint segments.
124 {
125 const Segment a(Point(0, 0), Point(1, 1));
126 const Segment b(Point(3, 0), Point(4, 1));
127 const auto result = pair_ix(a, b);
128 print_case("Disjoint", a, b, result);
129 assert(result.kind == SegmentSegmentIntersection::Kind::NONE);
130 assert(not result.intersects());
131 }
132
133 // 5) Free-function convenience + exact rational point.
134 {
135 const Segment a(Point(0, 0), Point(3, 0));
136 const Segment b(Point(1, 1), Point(2, -1));
137 const auto result = segment_segment_intersection(a, b);
138 print_case("Free function exact rational", a, b, result);
139 assert(result.kind == SegmentSegmentIntersection::Kind::POINT);
140 assert(result.point.get_x() == Geom_Number(3, 2));
141 assert(result.point.get_y() == Geom_Number(0));
142 }
143
144 cout << "STATUS: OK\n";
145 return 0;
146}
Represents a point with rectangular coordinates in a 2D plane.
Definition point.H:229
std::string to_string() const
Returns a string representation of the point as "(x,y)".
Definition point.H:651
Dedicated exact intersection for a single pair of segments.
Kind
Types of intersection between two segments.
Represents a line segment between two points.
Definition point.H:827
const Point & get_tgt_point() const noexcept
Gets the target point of the segment.
Definition point.H:921
const Point & get_src_point() const noexcept
Gets the source point of the segment.
Definition point.H:915
Computational geometry algorithms.
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.
SegmentSegmentIntersection::Result segment_segment_intersection(const Segment &s1, const Segment &s2)
Convenience free-function wrapper for SegmentSegmentIntersection.
STL namespace.
static void print_case(const char *name, const Segment &a, const Segment &b, const SegmentSegmentIntersection::Result &result)
static const char * kind_str(SegmentSegmentIntersection::Kind kind)
Detailed result of a segment-segment intersection test.
Point point
The intersection point (if kind is POINT).
Segment overlap
The overlap segment (if kind is OVERLAP).