#includeusing namespace sf; int main() { RectangleShape rect(Vector2f(100, 50)); rect.setPosition(50, 100); rect.setFillColor(Color::Red); // Set the top edge of the rectangle to y=75 rect.move(0, 25); // Display the rectangle RenderWindow window(VideoMode(200, 200), "Rect top example"); while (window.isOpen()) { Event event; while (window.pollEvent(event)) { if (event.type == Event::Closed) { window.close(); } } window.clear(); window.draw(rect); window.display(); } return 0; }
#includeThis example creates a rectangle shape using the SFML graphics library, sets its position to (50, 100), and then uses the getGlobalBounds() method to retrieve a bounding rectangle that contains the shape. The top property of this bounding rectangle represents the top edge of the shape, so we save this value to a variable called rect_top and print it out using cout. This example also uses the SFML graphics package.#include using namespace sf; int main() { RectangleShape rect(Vector2f(100, 50)); rect.setPosition(50, 100); // Get the top edge of the rectangle float rect_top = rect.getGlobalBounds().top; std::cout << "Top position of rectangle: " << rect_top << std::endl; return 0; }