giving up... just sorting calculated hullpoints clockwise before connecting with lines

This commit is contained in:
incredibleLeitman 2020-11-29 23:48:18 +01:00
parent a4acfdee3c
commit abcce8cbd4

View File

@ -334,11 +334,45 @@ void Display::update ()
if (curStep != 5 || m_points.size() > 0) m_step++; 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) void Display::render (sf::RenderWindow &window)
{ {
window.clear(sf::Color::White); window.clear(sf::Color::White);
// draw already calculated hull points // draw already calculated hull points
std::sort(m_hullPoints.begin(), m_hullPoints.end(), less); // sort clockwise
size_t elements = m_hullPoints.size(); size_t elements = m_hullPoints.size();
for (size_t i = 0; i < elements; ++i) for (size_t i = 0; i < elements; ++i)
{ {