Bug? Is the new Rect.Width & Rect.Height working correctly?

Widget

Well-Known Member
Licensed User
Longtime User
In B4A v6.30 we got Rect.Width and Rect.Height properties which I use a lot.
But is it working correctly? This may or may not be a bug. I don't know.

In the past I always measure width as
Width = Right-Left+1
Height = Bottom-Top+1

This of course assumes the origin (0,0) is at the top left corner as it is with View positions.

So if a point is located at (0,0) and has a width=1 and height=1, shouldn't Right=0 and Bottom=0?
This means it starts and finishes on that 1 pixel located at (0,0). The Rectangle would be (0,0,0,0) and represents a single point.

a) If you agree with that, then Rect.Width and Rect.Height are not working correctly. Here is a code example.

B4X:
    Private locRect As Rect
   
    'Example #1
    locRect.Initialize(0,0,0,0)
    Log(locRect& " Width="&locRect.Width&" Height="&locRect.Height)
    'Log Output: (Rect)(0, 0, 0, 0) Width=0 Height=0
    'Why isn't Width=1 and Height=1?
 
   'Example #2  
    locRect.Width  = 1
    locRect.Height = 1
    Log(locRect& " Width="&locRect.Width&" Height="&locRect.Height)
    'Log Output: (Rect)(0, 0, 1, 1) Width=1 Height=1
    'Why isn't Rect=(0,0,0,0)
   
    'Example #3
    locRect.Left=1
    locRect.Right=2
    locRect.Top=1
    locRect.Bottom=2
    Log(locRect& " Width="&locRect.Width&" Height="&locRect.Height)
    'Log Output: (Rect)(1, 1, 2, 2) Width=1 Height=1
    'Why isn't Width=2 and Height=2?

b) If my statement in a) is incorrect, then it has to be calculated as:
Width = Right-Left
Height = Bottom-Top
But this seems strange to me because Rect.Right and Rect.Bottom are one more than necessary.

Can someone clarify?
TIA
 

JordiCP

Expert
Licensed User
Longtime User
I think that android Rect implementation is b), at least as stated here http://stackoverflow.com/a/38098594/3296955

Also, a) would sound more logic to me if I draw the rectangle on a grid with visible pixels... but thinking more about it, b) makes sense since bounds are dimensionless

For instance, with time: if I am here from 12 to 12, in fact I am not here for 1 hour, but 0 hours
If I walk from km 17 to km 18 I will have walked 1 Km, not 2km
 
Top