Added Jarvi algo to find convex hull

This commit is contained in:
rishabh-997 2020-08-17 02:46:38 +05:30
parent d21f56e836
commit bf610fb91d

View File

@ -26,7 +26,6 @@
* @author [Rishabh Agarwal](https://github.com/rishabh-997) * @author [Rishabh Agarwal](https://github.com/rishabh-997)
*/ */
#include <utility>
#include <vector> #include <vector>
#include <cassert> #include <cassert>
#include <iostream> #include <iostream>
@ -54,19 +53,19 @@ namespace geometry {
* throughout the code * throughout the code
*/ */
class Convexhull { class Convexhull {
public:
std::vector<Point> points; std::vector<Point> points;
int size; int size;
public:
/** /**
* Constructor of given class * Constructor of given class
* *
* @param pointList list of all points in the space * @param pointList list of all points in the space
* @param n number of points in space * @param n number of points in space
*/ */
Convexhull(std::vector<Point> pointList, int n) { explicit Convexhull(const std::vector<Point> &pointList) {
points = std::move(pointList); points = pointList;
size = n; size = points.size();
} }
/** /**
@ -120,16 +119,18 @@ namespace geometry {
} }
/** /**
* * This function returns the geometric orientation for the three points
* in a space, ie, whether they are linear ir clockwise or
* anti-clockwise
* @param p first point selected * @param p first point selected
* @param q adjacent point for q * @param q adjacent point for q
* @param r adjacent point for q * @param r adjacent point for q
* @returns the geometric orientation for the three points *
* 0 -> Linear * @returns 0 -> Linear
* 1 -> Clock Wise * @returns 1 -> Clock Wise
* 2 -> Anti Clock Wise * @returns 2 -> Anti Clock Wise
*/ */
static int orientation(Point p, Point q, Point r) { static int orientation(const Point &p, const Point &q, const Point &r) {
int val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y); int val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
if (val == 0) { if (val == 0) {
@ -149,22 +150,21 @@ namespace geometry {
*/ */
static void test() { static void test() {
std::vector<geometry::jarvis::Point> points = {{0, 3}, std::vector<geometry::jarvis::Point> points = {{0, 3},
{2, 2}, {2, 2},
{1, 1}, {1, 1},
{2, 1}, {2, 1},
{3, 0}, {3, 0},
{0, 0}, {0, 0},
{3, 3} {3, 3}
}; };
int n = points.size(); geometry::jarvis::Convexhull hull(points);
geometry::jarvis::Convexhull hull(points, n);
std::vector<geometry::jarvis::Point> actualPoint; std::vector<geometry::jarvis::Point> actualPoint;
actualPoint = hull.getConvexHull(); actualPoint = hull.getConvexHull();
std::vector<geometry::jarvis::Point> expectedPoint = {{0, 3}, std::vector<geometry::jarvis::Point> expectedPoint = {{0, 3},
{0, 0}, {0, 0},
{3, 0}, {3, 0},
{3, 3}}; {3, 3}};
for (int i = 0; i < expectedPoint.size(); i++) { for (int i = 0; i < expectedPoint.size(); i++) {
assert(actualPoint[i].x == expectedPoint[i].x); assert(actualPoint[i].x == expectedPoint[i].x);
assert(actualPoint[i].y == expectedPoint[i].y); assert(actualPoint[i].y == expectedPoint[i].y);