I am trying to draw a convex shape with points that the user types in. I am having an issue where the shape does not show up if there are two points, And if there are more than two points, the shape is offset and does not connect the points I want. The program starts by asking how many points they want, and it calls this number n. I then store the X and Y coordinates in a vector, which I call whenever I need the points. Here is the code I am trying to use to draw the shapeshape.setPointCount(n);
for (int a = 0; a <= n-1; ++a) {
shape.setPoint(a, Vector2f(xc[a], yc[a]));
}
Xc is the vector I use to store x coordinates, and yc is the vector I use to store Y coordinates. I later draw this shape. Why does this only work with more than two points, and why Is it offset when it does work?
Usually the minimum number of points for drawing a figure is three.
Furthermore the points must be entered in a certain order.
From the tutorial:
The order in which you define the points is very important. They must all be defined either in clockwise or counter-clockwise order. If you define them in an inconsistent order, the shape will be constructed incorrectly.
See the example:
// create an empty shape
sf::ConvexShape convex;
// resize it to 5 points
convex.setPointCount(5);
// define the points
convex.setPoint(0, sf::Vector2f(0.f, 0.f));
convex.setPoint(1, sf::Vector2f(150.f, 10.f));
convex.setPoint(2, sf::Vector2f(120.f, 90.f));
convex.setPoint(3, sf::Vector2f(30.f, 100.f));
convex.setPoint(4, sf::Vector2f(0.f, 50.f));
Link to the tutorial page: https://www.sfml-dev.org/tutorials/2.5/graphics-shape.php