Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
point_utils.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
57# ifndef POINT_UTILS_H
58# define POINT_UTILS_H
59
74inline const Geom_Number area_of_parallelogram(const Point & a,
75 const Point & b,
76 const Point & c)
77{
78 return ((b.get_x() - a.get_x()) * (c.get_y() - a.get_y()) -
79 (c.get_x() - a.get_x()) * (b.get_y() - a.get_y()));
80}
81
90inline const Geom_Number area_of_triangle(const Point & a,
91 const Point & b,
92 const Point & c)
93{
94 return mpq_abs(area_of_parallelogram(a, b, c))/2;
95}
96
107inline const bool points_are_colinear(const Point & a,
108 const Point & b,
109 const Point & c)
110{
111 return area_of_parallelogram(a, b, c) == 0;
112}
113
122inline const bool c_is_to_left_(const Point & a, const Point & b, const Point & c)
123{
124 return parallelogram_area(a, b, c) > 0;
125}
126
135inline const bool is_clockwise(const Point & a,
136 const Point & b,
137 const Point & c)
138{
139 return not left(a, b, c);
140}
141
150inline const bool left_on(const Point & a, const Point & b, const Point & c)
151{
152 return parallelogram_area(a, b, c) >= 0;
153}
154
167const bool proper_intersection(const Point & a, const Point & b,
168 const Point & c, const Point & d)
169{
170 if (colinear_points(a, b, c) or
171 colinear_points(a, b, d) or
172 colinear_points(c, d, a) or
173 colinear_points(c, d, b))
174 return false;
175
176 return ((left(a, b, c) xor left(a, b, d)) and
177 (left(c, d, a) xor left(c, d, b)));
178}
179
190const bool between(const Point & a, const Point & b, const Point & c)
191{
192 if (not colinear_points(a, b, c))
193 return false;
194
195 // If ab is not vertical, compare x coordinates
196 if (a.get_x() != b.get_x())
197 return (((a.get_x() <= c.get_x()) and (c.get_x() <= b.get_x())) or
198 ((a.get_x() >= c.get_x()) and (c.get_x() >= b.get_x())));
199
200 // Vertical segment: compare y coordinates
201 return (((a.get_y() <= c.get_y()) and (c.get_y() <= b.get_y())) or
202 ((a.get_y() >= c.get_y()) and (c.get_y() >= b.get_y())));
203}
204
217const bool intersect_improp(const Point & a, const Point & b,
218 const Point & c, const Point & d)
219{
220 if (between(a, b, c) or
221 between(a, b, d) or
222 between(c, d, a) or
223 between(c, d, b))
224 return true;
225
226 return false;
227}
228
240const bool intersectp(const Point & a, const Point & b,
241 const Point & c, const Point & d)
242{
243 if (intersect_prop(a, b, c, d))
244 return true;
245
246 if (intersect_improp(a, b, c, d))
247 return true;
248
249 return false;
250}
251
260int read_pixels(ifstream &in){
261 int c=0, i=0;
262 char buffer[]={'\0','\0','\0','\0','\0'};
263 while(!in.eof() && isdigit(c=in.get())) buffer[i++] = c;
264 int num = atoi(buffer);
265 return num;
266
267}
268
269
270# endif // POINT_UTILS_H
Rectangular point in the plane.
Definition point.H:156
const Geom_Number & get_y() const
Returns y value.
Definition point.H:226
const Geom_Number & get_x() const
Returns x value.
Definition point.H:221
const bool c_is_to_left_(const Point &a, const Point &b, const Point &c)
Test if point c is strictly left of line a→b.
const bool is_clockwise(const Point &a, const Point &b, const Point &c)
Test if points are in clockwise order.
const bool between(const Point &a, const Point &b, const Point &c)
Test if point c is between points a and b.
const Geom_Number area_of_triangle(const Point &a, const Point &b, const Point &c)
Compute area of triangle formed by three points.
Definition point_utils.H:90
const Geom_Number area_of_parallelogram(const Point &a, const Point &b, const Point &c)
Compute signed area of parallelogram formed by three points.
Definition point_utils.H:74
const bool intersect_improp(const Point &a, const Point &b, const Point &c, const Point &d)
Test for improper (endpoint) segment intersection.
int read_pixels(ifstream &in)
Read integer from input stream (pixel coordinates).
const bool proper_intersection(const Point &a, const Point &b, const Point &c, const Point &d)
Test for proper segment intersection.
const bool left_on(const Point &a, const Point &b, const Point &c)
Test if point c is left of or on line a→b.
const bool intersectp(const Point &a, const Point &b, const Point &c, const Point &d)
Test if two segments intersect (proper or improper).
const bool points_are_colinear(const Point &a, const Point &b, const Point &c)
Test if three points are collinear.