on Android, there's a Canvas class that represents a drawing surface. It has a clipping rect. Question - are the rect's right and bottom borders inclusive or exclusive? In other words - if the rect is (0, 0)-(10, 10), will the Canvas allow drawing in pixels at coordinates 10?
Asked
Active
Viewed 1,612 times
5
-
1Yes, it will allow drawing in pixels at coordinate 10. – Daisetsu Jun 17 '10 at 17:12
-
If so, then there's probably an off-by-1 bug in Android framework. My view is screen wide, I'm getting a Canvas with clipping region (0, 0)-(320,280). The pixel at x=320 is off the screen. – Seva Alekseyev Jun 17 '10 at 17:18
-
1For `Rect`, width = right - left [1] I don't believe that right is a valid x coordinate value; right - 1 should be the last one. [1] http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=graphics/java/android/graphics/Rect.java;h=98ffb8b4318ba2cb59a68499a6add006ca1b2c5d;hb=master#l172 – Roman Nurik Jun 19 '10 at 00:09
-
1By definition, a Rect don't contains the left/bottom edges. See [http://developer.android.com/reference/android/graphics/Rect.html#contains(int,%20int)](http://developer.android.com/reference/android/graphics/Rect.html#contains(int,%20int)). – aalmeida Dec 11 '13 at 19:59
-
@Daisetsu is incorrect. – Michael Scheper Feb 18 '14 at 06:37
-
@aalmeida: Thank you for finding that reference. It's unfortunate that this is buried in the detail of one method, instead of with the field variables themselves. I'm surprised Google let the API get out the door with those field variables undocumented! – Michael Scheper Feb 18 '14 at 06:39
1 Answers
3
According to another StackOverflow question, right
and bottom
are exclusive, but top
and left
are inclusive.
As I say in my answer there (which I suppose is really a comment), this is consistent with other Java API, and has other benefits.
So, no, you won't be able to draw at ordinate 10. But it does mean that your Rect
is a 10×10 pixel square.
Also, calculations are simpler, like:
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
Just for example, I know we have
.getWidth()
and.getHeight()
methods.

Top-Master
- 7,611
- 5
- 39
- 71

Michael Scheper
- 6,514
- 7
- 63
- 76