Visual Computing Library
Loading...
Searching...
No Matches
core2.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_ALGORITHMS_CORE_POLYGON_CORE2_H
24#define VCL_ALGORITHMS_CORE_POLYGON_CORE2_H
25
26#include <vclib/space/core/polygon.h>
27
28namespace vcl {
29
52template<Point2Concept PointType>
54 const PointType& p0,
55 const PointType& p1,
56 const PointType& p2)
57{
58 return (
59 (p1.x() - p0.x()) * (p2.y() - p0.y()) -
60 (p2.x() - p0.x()) * (p1.y() - p0.y()));
61}
62
74template<Point2Concept PointType>
76 const PointType& p0,
77 const PointType& p1,
78 const PointType& p2)
79{
80 return collinearityTest(p0, p1, p2) > 0;
81}
82
95template<Point2IteratorConcept Iterator>
96bool isCounterClockWise(Iterator begin, Iterator end)
97{
98 using PointType = std::decay_t<decltype(*begin)>;
99 using ScalarType = PointType::ScalarType;
100
101 if (begin == end)
102 return false;
103
104 auto latest = begin;
105 auto it = begin;
106 ScalarType sum = 0;
107 for (++it; it != end; ++it) {
108 sum += (it->x() - latest->x()) * (it->y() + latest->y());
109 latest = it;
110 }
111
112 sum += (latest->x() - begin->x()) * (latest->y() + begin->y());
113
114 return sum < 0;
115}
116
134template<Point2IteratorConcept Iterator>
135void sortConvexPolygonVertices(Iterator begin, Iterator end)
136{
137 using PointType = std::decay_t<decltype(*begin)>;
138
139 // Find the point with the lowest x-coordinate
140 PointType minPoint = *std::min_element(
141 begin, end, [](const PointType& a, const PointType& b) {
142 return a.y() < b.y();
143 });
144
145 // Sort the points based on the angle with respect to minPoint
146 std::sort(begin, end, [minPoint](const PointType& a, const PointType& b) {
147 double angleA = std::atan2(a.y() - minPoint.y(), a.x() - minPoint.x());
148 double angleB = std::atan2(b.y() - minPoint.y(), b.x() - minPoint.x());
149 return angleA < angleB;
150 });
151}
152
153} // namespace vcl
154
155#endif // VCL_ALGORITHMS_CORE_POLYGON_CORE2_H
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
auto collinearityTest(const PointType &p0, const PointType &p1, const PointType &p2)
Computes the collinearity test between three points. The test returns a positive value if the points ...
Definition core2.h:53
bool areCounterClockwise(const PointType &p0, const PointType &p1, const PointType &p2)
Checks if the three points are counter-clockwise.
Definition core2.h:75
void sortConvexPolygonVertices(Iterator begin, Iterator end)
Sorts the vertices of a convex polygon in counter-clockwise order.
Definition core2.h:135
bool isCounterClockWise(Iterator begin, Iterator end)
Checks if a set of points that form a polygon are in counter-clockwise order.
Definition core2.h:96