diff --git a/Display.cpp b/Display.cpp index 7cf02e7..f0f9bbe 100644 --- a/Display.cpp +++ b/Display.cpp @@ -334,11 +334,45 @@ void Display::update () if (curStep != 5 || m_points.size() > 0) m_step++; } +bool less (sf::CircleShape a, sf::CircleShape b) +{ + float centerx = WIDTH / 2; + float centery = HEIGHT / 2; + float ax = a.getPosition().x; + float ay = a.getPosition().y; + float bx = b.getPosition().x; + float by = b.getPosition().y; + if (ax - centerx >= 0 && bx - centerx < 0) + return true; + if (ax - centerx < 0 && bx - centerx >= 0) + return false; + if (ax - centerx == 0 && bx - centerx == 0) { + if (ay - centery >= 0 || by - centery >= 0) + return ay > by; + return by > ay; + } + + // compute the cross product of vectors (center -> a) x (center -> b) + int det = (ax - centerx) * (by - centery) - (bx - centerx) * (ay - centery); + if (det < 0) + return true; + if (det > 0) + return false; + + // points a and b are on the same line from the center + // check which point is closer to the center + int d1 = (ax - centerx) * (ax - centerx) + (ay - centery) * (ay - centery); + int d2 = (bx - centerx) * (bx - centerx) + (by - centery) * (by - centery); + return d1 > d2; +} + void Display::render (sf::RenderWindow &window) { window.clear(sf::Color::White); // draw already calculated hull points + std::sort(m_hullPoints.begin(), m_hullPoints.end(), less); // sort clockwise + size_t elements = m_hullPoints.size(); for (size_t i = 0; i < elements; ++i) {