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

Author Topic: sf::Rect  (Read 9430 times)

0 Members and 1 Guest are viewing this topic.

achpile

  • Full Member
  • ***
  • Posts: 231
    • View Profile
    • Achpile's homepage
    • Email
sf::Rect
« on: September 10, 2018, 08:24:07 pm »
Hey guys! Just got an issue with rect class.... Can someone explain this please?
Commit:

commit 082a928555125e37cc52a80c11cf286f0b03dee5
Author: LaurentGom <LaurentGom@4e206d99-4929-0410-ac5d-dfc041789085>
Date:   Fri Apr 9 13:04:49 2010 +0000

    *important* sf::Rect now uses Width/Height instead of Right/Bottom
    Removed Offset, GetSize and GetCenter functions from sf::Rect
    Added a sf::Rect constructor taking two Vector2 parameters
    Updated the API documentation of the sf::Rect class
 

 template <typename T>
 bool Rect<T>::Contains(T x, T y) const
 {
-    return (x >= Left) && (x <= Right) && (y >= Top) && (y <= Bottom);
+    return (x >= Left) && (x < Left + Width) && (y >= Top) && (y < Top + Height);
 }
 

Why changed "<=" to "<"?

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: sf::Rect
« Reply #1 on: September 10, 2018, 08:51:01 pm »
Imagine Left=0 and Width=5. The 5 horizontal points in your rectangle would then be 0, 1, 2, 3, 4.

Now imagine x=5. (x < Left + Width) becomes (5 < 0 + 5), which will evaluate to false. This is correct because the horizontal point 5 is not in the rectangle. If it were "<=" then it would incorrectly evaluate to true.

achpile

  • Full Member
  • ***
  • Posts: 231
    • View Profile
    • Achpile's homepage
    • Email
Re: sf::Rect
« Reply #2 on: September 10, 2018, 09:10:49 pm »
Imagine Left=0 and Width=5. The 5 horizontal points in your rectangle would then be 0, 1, 2, 3, 4.

Now imagine x=5. (x < Left + Width) becomes (5 < 0 + 5), which will evaluate to false. This is correct because the horizontal point 5 is not in the rectangle. If it were "<=" then it would incorrectly evaluate to true.

left = 0; width = 5;
width 5 is 0-1, 1-2, 2-3, 3-4, 4-5

width of 0-4 is 4, not 5.

Dude, learn math please before posting here

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: sf::Rect
« Reply #3 on: September 10, 2018, 10:28:55 pm »
Quote
Dude, learn math please before posting here
Yikes, no need to be an ass to people trying to help you. It's possible I'm wrong as I'm not at a place where I can try things out, but let's take another stab at it. It's easy to end up with off-by-one errors.

Let's now imagine Left=0 and Width=1.Based on what you said width 1 would be 0-1, but what are you expecting to happen if you were to draw it to the screen in red? Would you expect both pixel 0 and 1 to be red? That would feel like a width of 2 to me.

Width of 1 doesn't include all of 0 and all of 1. In other words [0,1).

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Rect
« Reply #4 on: September 11, 2018, 07:39:41 am »
"right" was the last pixel included in the rectangle. But "width", added to left, yields to the first pixel not in the rectangle. And Arcade's math is 100% correct by the way.

If you can't understand that then think about arrays: if the size (width) of an array is 5, and its first index (left) is 0, then its last element (right) is index 4, not 5. 0-based indexing, you know.
« Last Edit: September 11, 2018, 07:41:19 am by Laurent »
Laurent Gomila - SFML developer

achpile

  • Full Member
  • ***
  • Posts: 231
    • View Profile
    • Achpile's homepage
    • Email
Re: sf::Rect
« Reply #5 on: September 11, 2018, 09:19:28 am »
Quote
If you can't understand that then think about arrays

Don't mix up the size of an array (amount of elements) and length of a line.
In geometry rectangle contains all 4 points. And all points on the edges.

PS: i'm not talking about which pixels are included and which are not. I was talking about FloatRect

achpile

  • Full Member
  • ***
  • Posts: 231
    • View Profile
    • Achpile's homepage
    • Email
Re: sf::Rect
« Reply #6 on: September 11, 2018, 09:30:52 am »
Just let's take a simple example....

I have rectangle. Based on 4 points. (0,0), (0,2), (2,2) and (2,0)

The length of each side is 2.
Does it contain point (0,0) ? Yes, it does.
Does it contain point (2,2) ? Yes, it does.

All you said is right only if you use sf::IntRect to store array dimentions, but not as a geometry object

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Rect
« Reply #7 on: September 11, 2018, 10:14:48 am »
This is usually a discussion for integer coordinates. For floating point, nobody cares that 4.99999 is inside the rect and 5.0 is not. And I can't imagine why it would be different, anyway.
Laurent Gomila - SFML developer

achpile

  • Full Member
  • ***
  • Posts: 231
    • View Profile
    • Achpile's homepage
    • Email
Re: sf::Rect
« Reply #8 on: September 11, 2018, 10:27:38 am »
Well, this is not for integer coordinates. This is for pixels.
As i said in example - let's take a look at 2x2 rectangle.
And what is pixel? It's 1x1 square. So 2x2 square will contain 4 pixels (0,0), (1,0), (0,1) and (1,1)
But pixel is not a point - it's a square.

Will 2x2 rect contain pixel (2,2)? No
Will 2x2 rect contain point (2,2)? Yes

So this is not clear in Rect class - if you work with points or pixels. In function call it called a point, but actually it isn't.

And, well, if nobody cares about that - i won't too.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Rect
« Reply #9 on: September 11, 2018, 10:41:52 am »
Quote
Will 2x2 rect contain point (2,2)? Yes
It doesn't, because by convention, point (2, 2) belongs to pixel (2, 2).

There's no strong mathematical or logical argument to decide whether an edge is inside or outside a geometrical shape, it's just about conventions. We can say it is in, we can say it is out. And in this particular case, we say it's out.
Laurent Gomila - SFML developer

achpile

  • Full Member
  • ***
  • Posts: 231
    • View Profile
    • Achpile's homepage
    • Email
Re: sf::Rect
« Reply #10 on: September 11, 2018, 10:45:11 am »
Quote
It doesn't, because by convention, point (2, 2) belongs to pixel (2, 2).

But it also belongs to pixels (1,1), (1,2) and (2,1)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Rect
« Reply #11 on: September 11, 2018, 10:58:25 am »
Quote
But it also belongs to pixels (1,1), (1,2) and (2,1)
Because you say it does doesn't mean that it's the absolute truth. The OpenGL rasterizer (for obvious reasons) says that a coordinate cannot belong to multiple pixels. There's no truth, only conventions.
Laurent Gomila - SFML developer

achpile

  • Full Member
  • ***
  • Posts: 231
    • View Profile
    • Achpile's homepage
    • Email
Re: sf::Rect
« Reply #12 on: September 11, 2018, 11:00:39 am »
Quote
There's no truth, only conventions.

Sad but true....
So I guess topic can be closed