c++sfmlrectangles

How to find the coordinates of a rectangle shape in SFML?


I would like to draw on the screen a consecutive line, using the rectangle shape. I have modified the thickness of the rectangle shape, so it seems a line, instead of a rectangle. Now, since I would like to draw different rectangle shapes, I need to know the coordinates of the shape so I can adjust them, but the functions that are given, the GetPoint gives me back only a vector that contains two values, when the rectangle has 4 points. Maybe it's a dumb question, but how can I get the coordinates of the shape, and if there have been rotations how can I do that? I read on sfml that the function GetPoint doesn't take in consideration the rotation and things. I attach the link of what I would like to do. So I was thinking to make a list of rectangle shapes, that look lines, generating a random angle for the rotation and then make a function that sets the position as in the picture (Even though, I don't know if my idea is doable, if there is a simpler way, feel free to suggest ideas).

https://imgur.com/a/KUwqn9M


Solution

  • You can get all the points of a shape using getPoint(index), with index in range 0 to Shape::getPointCount() (should be 4 in your case). You can transform these points further using your custom matrix to get the real world position.

    sf::Shape myShape = /* ... */;
    sf::Transform matrix = /* could be myShape.getTransform() */;
    for (int i = 0; i < myShape.getPointCount(); ++i) {
        const auto pointInModelSpace = matrix.transformPoint(myShape.getPoint(i));
    }