Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Issue with drawing convex shape  (Read 756 times)

0 Members and 1 Guest are viewing this topic.

TheArmoredPanda

  • Newbie
  • *
  • Posts: 5
    • View Profile
Issue with drawing convex shape
« on: October 30, 2022, 04:50:36 pm »
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 shape
shape.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?

euscar

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Issue with drawing convex shape
« Reply #1 on: October 31, 2022, 08:09:30 am »
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:
Quote
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

Hapax

  • Hero Member
  • *****
  • Posts: 3346
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Issue with drawing convex shape
« Reply #2 on: November 02, 2022, 06:04:28 pm »
I am having an issue where the shape does not show up if there are two points
[...]
Why does this only work with more than two points[...]?
Usually the minimum number of points for drawing a figure is three.

A simple answer for this could be that shapes are made of triangles.

A slightly more complex answer is that the '"center of gravity" for 2 points would be located perfectly on the line between the two points, creating an infinitely thin line, which is not much of a convex shape...
Note that all shapes should fill areas and they are not for creating points or infinitely thin lines; there are more dedicated ways to do that sort of thing.
It's worth noting that ConvexShape uses an sf::TriangleFan type and decides its centre point automatically.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*