Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- std::vector<Point> jarvisHull(std::vector<Point> points) {
- Point lowestPoint = Point::getLowestPoint(points);
- Point highestPoint = Point::getHighestPoint(points);
- std::vector<Point> hull;
- hull.push_back(lowestPoint);
- std::vector<Point> leftPoints = Point::getRightPoints(highestPoint, lowestPoint, points);
- if (leftPoints.size() > 0) {
- leftPoints.push_back(lowestPoint);
- Point curPoint = highestPoint;
- while (!curPoint.equals(lowestPoint)) {
- float minPolarAngle = 3 * M_PI;
- Point closestPoint = leftPoints[0];
- for (std::vector<Point>::iterator it = leftPoints.begin(); it != leftPoints.end(); ++it) {
- float polarAngle = it->getPolarAngle(curPoint);
- if (polarAngle < polarAngle && curPoint.getY() > it->getX()) {
- minPolarAngle = polarAngle;
- closestPoint = *it;
- }
- }
- if (minPolarAngle == 3 * M_PI)
- std::cout << "ERROR: NO CLOSE LEFT POINT FOUND" << std::endl;
- hull.push_back(closestPoint);
- curPoint = closestPoint;
- }
- }
- std::vector<Point> rightPoints = Point::getRightPoints(lowestPoint, highestPoint, points);
- if (rightPoints.size() > 0) {
- Point curPoint = lowestPoint;
- while (!curPoint.equals(highestPoint)) {
- float minPolarAngle = 3 * M_PI;
- Point closestPoint = rightPoints[0];
- for (std::vector<Point>::iterator it = rightPoints.begin(); it != rightPoints.end(); ++it) {
- float polarAngle = it->getPolarAngle(curPoint);
- if (polarAngle < polarAngle && curPoint.getY() < it->getX()) {
- minPolarAngle = polarAngle;
- closestPoint = *it;
- }
- }
- if (minPolarAngle == 3 * M_PI)
- std::cout << "ERROR: NO CLOSE RIGHT POINT FOUND" << std::endl;
- hull.push_back(closestPoint);
- curPoint = closestPoint;
- }
- }
- else
- hull.push_back(highestPoint);
- return hull;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement