Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. std::vector<Point> jarvisHull(std::vector<Point> points) {
  2. Point lowestPoint = Point::getLowestPoint(points);
  3. Point highestPoint = Point::getHighestPoint(points);
  4. std::vector<Point> hull;
  5. hull.push_back(lowestPoint);
  6.  
  7. std::vector<Point> leftPoints = Point::getRightPoints(highestPoint, lowestPoint, points);
  8. if (leftPoints.size() > 0) {
  9. leftPoints.push_back(lowestPoint);
  10. Point curPoint = highestPoint;
  11. while (!curPoint.equals(lowestPoint)) {
  12. float minPolarAngle = 3 * M_PI;
  13. Point closestPoint = leftPoints[0];
  14. for (std::vector<Point>::iterator it = leftPoints.begin(); it != leftPoints.end(); ++it) {
  15. float polarAngle = it->getPolarAngle(curPoint);
  16. if (polarAngle < polarAngle && curPoint.getY() > it->getX()) {
  17. minPolarAngle = polarAngle;
  18. closestPoint = *it;
  19. }
  20. }
  21. if (minPolarAngle == 3 * M_PI)
  22. std::cout << "ERROR: NO CLOSE LEFT POINT FOUND" << std::endl;
  23. hull.push_back(closestPoint);
  24. curPoint = closestPoint;
  25. }
  26. }
  27.  
  28. std::vector<Point> rightPoints = Point::getRightPoints(lowestPoint, highestPoint, points);
  29. if (rightPoints.size() > 0) {
  30. Point curPoint = lowestPoint;
  31. while (!curPoint.equals(highestPoint)) {
  32. float minPolarAngle = 3 * M_PI;
  33. Point closestPoint = rightPoints[0];
  34. for (std::vector<Point>::iterator it = rightPoints.begin(); it != rightPoints.end(); ++it) {
  35. float polarAngle = it->getPolarAngle(curPoint);
  36. if (polarAngle < polarAngle && curPoint.getY() < it->getX()) {
  37. minPolarAngle = polarAngle;
  38. closestPoint = *it;
  39. }
  40. }
  41. if (minPolarAngle == 3 * M_PI)
  42. std::cout << "ERROR: NO CLOSE RIGHT POINT FOUND" << std::endl;
  43. hull.push_back(closestPoint);
  44. curPoint = closestPoint;
  45. }
  46. }
  47. else
  48. hull.push_back(highestPoint);
  49. return hull;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement