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

Author Topic: Boundaries of X, Y coordinates in window  (Read 5304 times)

0 Members and 1 Guest are viewing this topic.

cppxor2arr

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Boundaries of X, Y coordinates in window
« on: August 15, 2017, 03:36:01 pm »
In this code:

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window{sf::VideoMode{300, 300}, "Window Title"};

    sf::VertexArray points{sf::Points, 4};

    // which two of these are the top left and bottom right corner

    points[0].position = sf::Vector2f{0, 0};
    points[1].position = sf::Vector2f{1, 1};
    points[2].position = sf::Vector2f{299,  299};
    points[3].position = sf::Vector2f{300, 300};

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
            if (event.type == sf::Event::Closed)
                window.close();
        window.clear(); window.draw(points); window.display();
    }
}

What are the boundaries? 0-299 or 1-300 (width or height)? Also, the question in the comment.
« Last Edit: August 15, 2017, 03:39:24 pm by cppxor2arr »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Boundaries of X, Y coordinates in window
« Reply #1 on: August 15, 2017, 03:50:26 pm »
(0, 0) is top left
(width - 1, height -1) is bottom right

However, when you use sf::Points primitive, you have to consider that a pixel is still an area, while it can't be resolved any further by your screen, OpenGL still handles it as float, so the top left pixel starts at position (0, 0) and spawns up to the point (1, 1). So if you want to render a point on that pixel, you need to center the point, setting its position to (0.5f, 0.5f).

Illustration



Red = Point at (0, 0)
Blue = Point at (0.5f, 0.5f)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

cppxor2arr

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Boundaries of X, Y coordinates in window
« Reply #2 on: August 15, 2017, 04:15:09 pm »
Thanks a lot!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Boundaries of X, Y coordinates in window
« Reply #3 on: August 25, 2017, 03:11:04 pm »
It is a side effect of OpenGL rounding rules. The correct position is (0.5, 0.5).
Laurent Gomila - SFML developer