Пример #1
0
ConvexHull findConvexHull(PointSet *pointSet) {

  int pointCount = pointSet->getSize();

  pointSet->sortPointsByAngle();

  if (pointCount <= 3) { // The points are already a convex hull
    ConvexHull hull;
    for (int i = 0; i < pointCount; ++i) {
      hull.addPoint(*pointSet->getPoint(i));
    }
    if (pointCount > 0) {
      hull.addPoint(*pointSet->getPoint(0));
    }
    return hull;
  }

  std::stack<const Point *> candiates;

  const Point *prev;
  const Point *now;

  candiates.push(pointSet->getLastPoint());
  candiates.push(pointSet->getReferencePoint()); // Is always the first (idx 0)
  // element in the point set

  for (int i = 1; i < pointCount;) {
    const Point *point = pointSet->getPoint(i);

    now = candiates.top();
    candiates.pop();
    prev = candiates.top();
    candiates.push(now);

    if (isCCW(prev->pos(), now->pos(), point->pos())) {
      if (point->pos() == now->pos() && USE_CUSTOM_ALGO_FIX) {
        std::cout << "I saved your algorithm" << std::endl;
      } else {
        candiates.push(point);
      }
      ++i;
    } else {
      candiates.pop();
    }
  }

  ConvexHull hull(candiates);
  return hull;
}
Пример #2
0
HullState findConvexHullStep(PointSet *pointSet, int simulateUntilStep) {

  int step = 0;

  int pointCount = pointSet->getSize();

  pointSet->sortPointsByAngle();

  //++step;
  if (step++ == simulateUntilStep) // sort done -> update numbers
  {
    return HullState::SortDone(step);
  }

  if (pointCount <= 3) { // The points are already a convex hull
    ConvexHull hull;
    for (int i = 0; i < pointCount; ++i) {
      hull.addPoint(*pointSet->getPoint(i));
    }
    if (pointCount > 0) {
      hull.addPoint(*pointSet->getPoint(0));
    }
    return HullState::HullFound(hull);
  }

  std::stack<const Point *> candiates;

  const Point *prev;
  const Point *now;

  candiates.push(pointSet->getLastPoint());
  candiates.push(pointSet->getReferencePoint()); // Is always the first (idx 0)
                                                 // element in the point set

  //++step;
  if (step++ == simulateUntilStep) // break print candidates
  {
    return HullState::CandidateAdded(candiates, step);
  }

  for (int i = 1; i < pointCount;) {
    const Point *point = pointSet->getPoint(i);

    now = candiates.top();
    candiates.pop();
    prev = candiates.top();
    candiates.push(now);

    if (isCCW(prev->pos(), now->pos(), point->pos())) {
      candiates.push(point);
      // std::cout << "adding " << i << std::endl;
      ++i;

      //++step;
      if (step++ == simulateUntilStep) // break print candidates
      {
        return HullState::CandidateAdded(candiates, step);
      }
    } else {
      //++step;
      if (step++ == simulateUntilStep) // break print candidates
      {
        return HullState::CandidatePoped(candiates, point, step);
      }
      // std::cout << "pop" << std::endl;
      candiates.pop();
    }
  }
  ConvexHull hull(candiates);
  return HullState::HullFound(hull);
}