TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
jarvis_algorithm.cpp
Go to the documentation of this file.
1
28
29
#include <vector>
30
#include <cassert>
31
#include <iostream>
32
37
namespace
geometry
{
42
namespace
jarvis
{
47
struct
Point
{
48
int
x, y;
49
};
50
55
class
Convexhull
{
56
std::vector<Point> points;
57
int
size;
58
59
public
:
66
explicit
Convexhull
(
const
std::vector<Point> &pointList) {
67
points = pointList;
68
size = points.size();
69
}
70
78
std::vector<Point>
getConvexHull
()
const
{
79
// Initialize Result
80
std::vector<Point> hull;
81
82
// Find the leftmost point
83
int
leftmost_point = 0;
84
for
(
int
i = 1; i < size; i++) {
85
if
(points[i].x < points[leftmost_point].x) {
86
leftmost_point = i;
87
}
88
}
89
// Start from leftmost point, keep moving counterclockwise
90
// until reach the start point again. This loop runs O(h)
91
// times where h is number of points in result or output.
92
int
p = leftmost_point, q = 0;
93
do
{
94
// Add current point to result
95
hull.push_back(points[p]);
96
97
// Search for a point 'q' such that orientation(p, x, q)
98
// is counterclockwise for all points 'x'. The idea
99
// is to keep track of last visited most counter clock-
100
// wise point in q. If any point 'i' is more counter clock-
101
// wise than q, then update q.
102
q = (p + 1) % size;
103
for
(
int
i = 0; i < size; i++) {
104
// If i is more counterclockwise than current q, then
105
// update q
106
if
(
orientation
(points[p], points[i], points[q]) == 2) {
107
q = i;
108
}
109
}
110
111
// Now q is the most counterclockwise with respect to p
112
// Set p as q for next iteration, so that q is added to
113
// result 'hull'
114
p = q;
115
116
}
while
(p != leftmost_point);
// While we don't come to first point
117
118
return
hull;
119
}
120
133
static
int
orientation
(
const
Point
&p,
const
Point
&q,
const
Point
&r) {
134
int
val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
135
136
if
(val == 0) {
137
return
0;
138
}
139
return
(val > 0) ? 1 : 2;
140
}
141
142
};
143
144
}
// namespace jarvis
145
}
// namespace geometry
146
151
static
void
test
() {
152
std::vector<geometry::jarvis::Point> points = {{0, 3},
153
{2, 2},
154
{1, 1},
155
{2, 1},
156
{3, 0},
157
{0, 0},
158
{3, 3}
159
};
160
geometry::jarvis::Convexhull
hull(points);
161
std::vector<geometry::jarvis::Point> actualPoint;
162
actualPoint = hull.
getConvexHull
();
163
164
std::vector<geometry::jarvis::Point> expectedPoint = {{0, 3},
165
{0, 0},
166
{3, 0},
167
{3, 3}};
168
for
(
int
i = 0; i < expectedPoint.size(); i++) {
169
assert(actualPoint[i].x == expectedPoint[i].x);
170
assert(actualPoint[i].y == expectedPoint[i].y);
171
}
172
std::cout <<
"Test implementations passed!\n"
;
173
}
174
176
int
main
() {
177
test
();
178
return
0;
179
}
test
void test()
Definition
caesar_cipher.cpp:100
geometry::jarvis::Convexhull
Definition
jarvis_algorithm.cpp:55
geometry::jarvis::Convexhull::orientation
static int orientation(const Point &p, const Point &q, const Point &r)
Definition
jarvis_algorithm.cpp:133
geometry::jarvis::Convexhull::Convexhull
Convexhull(const std::vector< Point > &pointList)
Definition
jarvis_algorithm.cpp:66
geometry::jarvis::Convexhull::getConvexHull
std::vector< Point > getConvexHull() const
Definition
jarvis_algorithm.cpp:78
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
geometry
for std::swap
Definition
graham_scan_functions.hpp:52
jarvis
Functions for Jarvis’s algorithm.
geometry::jarvis::Point
Definition
jarvis_algorithm.cpp:47
geometry
jarvis_algorithm.cpp
Generated by
1.18.0