• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

OTClient Rect Class

danilopucci

Well-Known Member
Joined
Nov 22, 2019
Messages
114
Solutions
4
Reaction score
95
GitHub
danilopucci
Hello guys,

I am currently experimenting some codes on OTClient and I have a conceptual question about width/height in Rect class

There is a bunch of "+1" and "-1" corrections on its values, which does not look right. I am blowing my mind trying to understand that and why and how it works as it is used all over the code.

As an example:
C++:
TRect(T x, T y, T width, T height) : x1(x), y1(y), x2(x+width-1), y2(y+height-1) { }
T width() const { return  x2 - x1 + 1; }
T height() const { return  y2 - y1 + 1; }

The width of a rectangle it is given only of x2 - x1 and not x2 -x1 + 1

Does anybody has an explanation about this approach? I have searched on openCV and other math/graphical libraries and the width is like I said above (as the math says).

On I first saw it on default constructor x2 and y2 is set as -1, I though that the bunch of "+1" was some offset compensation, but it is not.


Thank you
 
Hello guys,

I am currently experimenting some codes on OTClient and I have a conceptual question about width/height in Rect class

There is a bunch of "+1" and "-1" corrections on its values, which does not look right. I am blowing my mind trying to understand that and why and how it works as it is used all over the code.

As an example:
C++:
TRect(T x, T y, T width, T height) : x1(x), y1(y), x2(x+width-1), y2(y+height-1) { }
T width() const { return  x2 - x1 + 1; }
T height() const { return  y2 - y1 + 1; }

The width of a rectangle it is given only of x2 - x1 and not x2 -x1 + 1

Does anybody has an explanation about this approach? I have searched on openCV and other math/graphical libraries and the width is like I said above (as the math says).

On I first saw it on default constructor x2 and y2 is set as -1, I though that the bunch of "+1" was some offset compensation, but it is not.


Thank you

Hi!

Let's say a widget is at position x10 y20 and it's size are width 2 and height 4:

The pixel positions are:
[x10, y20] [x11, y20]
[x10, y21] [x11, y21]
[x10, y22] [x11, y22]
[x10, y23] [x11, y23]

We know that our widget has 2 of width and 4 of height, right?
Let's prove it:

Width = X2 - X1 + 1 = 11 - 10 + 1 = 2
Height = Y2 - Y1 + 1 = 23 - 20 + 1 = 4

-

For you to understand, we will use, as an example, a widget that has 3 of width and 1 of height at same position (x10 y20) of previous example. So our pixel positions are:
[x10, y20] [x11, y20] [x12, y20]

To simplify, we will ignore the height and focus only at the width:
[x10] [x11] [x12]

In math, there is no pixels.
As math, it would make sense to say that x10 + 3 of width would result the last right position at x13, right?
But that didn't happen with our example. Why?
In math distance formula, we calculate the distance.
In pixels, we calculate the pixels count that fits in an area.

In math distance formula, we consider position at x10 as 0 (this means we ARE NOT including it), so 1 is position at x11, 2 is position at x12, and that is not what we want.
So, starting at x10 up to x12, we have a distance of 2.

In pixels, our first pixel x10 is 1 instead (this means we ARE including it), so pixel 2 is x11, pixel 3 is x12, and that is exactly our pixel positions.
So, starting at x10 up to x12, we have 3 pixels.

-

Now, we will calculate the width of previous example:
12 - 10 + 1 = 3

But why +1?
Because once we remove the value 10, we are not including (counting) the first pixel x10.
And that would be the distance formula 12 - 10, which the result would be 2, and this means our pixel positions would be:
[x11] [x12]
That's incorrect! We are missing our first pixel x10. So we add +1!
As said, in pixels, we DO include the first pixel, in distance we DO NOT.
So distance and pixels count are different!

Adding +1, it would result:
[x10] [x11] [x12]

So, between positions x10 and x12, we have 2 of distance (which is the math distance context, because we do not included the first pixel x10) and 3 pixels (which is the pixels width, because we included the first pixel x10)!
 
Oh, now I got it! It is the width/height in terms of pixelcount/pixel origin

Thank you so much for your attention and clear illustration!
 

Similar threads

Back
Top