Na szybko pisany kod
#include <SFML/Graphics.hpp>
#include <vector>
using namespace std;
using namespace sf;
int main()
{
RenderWindow mainWindow( VideoMode( 800, 600 ), "shapes", Style::Close );
Event event;
Mouse mouse;
Keyboard keyboard;
CircleShape point( 6 );
point.setFillColor( Color( 255, 0, 0, 200 ) );
vector<int>points;
bool leftButtonPressed = false;
ConvexShape shape;
shape.setFillColor( Color( 0, 255, 0, 100 ) );
while( mainWindow.isOpen() )
{
mainWindow.clear();
while( mainWindow.pollEvent( event ) )
{
if( event.type == Event::Closed )
{
mainWindow.close();
}
if( leftButtonPressed && !mouse.isButtonPressed( Mouse::Left ) )
{
leftButtonPressed = false;
points.push_back( mouse.getPosition( mainWindow ).x );
points.push_back( mouse.getPosition( mainWindow ).y );
}
}
if( mouse.isButtonPressed( Mouse::Left ) )
{
leftButtonPressed = true;
point.setPosition( mouse.getPosition( mainWindow ).x + point.getRadius() - point.getGlobalBounds().width, mouse.getPosition( mainWindow).y + point.getRadius() - point.getGlobalBounds().height );
mainWindow.draw( point );
}
shape.setPointCount( leftButtonPressed?points.size() / 2 + 1:points.size() / 2 );
for( int i = 0; i < points.size() / 2; ++i )
{
shape.setPoint( i, Vector2f( points[ i * 2 ], points[ i * 2 + 1 ] ) );
point.setPosition( points[ i * 2 ] - point.getRadius(), points[ i * 2 + 1 ] - point.getRadius() );
mainWindow.draw( point );
}
if( leftButtonPressed )
shape.setPoint( shape.getPointCount() - 1, Vector2f( mouse.getPosition( mainWindow ).x, mouse.getPosition( mainWindow ).y ) );
mainWindow.draw( shape );
mainWindow.display();
}
return 0;
}
Otóż program działa ładnie
Dodając kolejny wierzchołek też jest ładnie
Próbując wielokątów wklęsłych (czyli tam gdzie podejrzewałem że coś pójdzie nie tak) wszystko jest pozornie dobrze
Problem zaczyna się gdy ten kąt jest troszeczkę większy
(ten jaśniejszy zielony jest dlatego że dwa kolory RGB(0, 255, 0) z kanałem alpha na siebie nachodzą)
Co można z tym zrobić żeby ta figura faktycznie składała się z połączonych punktów? I dlaczego ta figura na obrazku wyżej jest narysowana w ten sposób?