bp74/StageXL — pull request #65 by wilkerlucio (2014-02-06 03:37:45+00:00)
Title: added tests for rectangle class, fixed #contains and refactorings, problem with rectangle 'right' calculation

Hi, first I was just going to refactor the class, then I noticed it wasn't tested, so I wrote tests for all methods on the Rectangle class. There were getters and setters for the properties, I just replaced them with regular variables (which is the same), that makes the code smaller and more clear.

During the test writing I found a bug into the `contains` and the `containsPoint` implementations, and here is an example of why it was wrong:

First, let's get the old algorithm for testing `contains` (`containsPoint` has same implementation):

```dart
bool contains(num px, num py) {
return _x <= px && _y <= py && _x + _width >= px && _y + _height >= py
}
```

Now let's imagine that we have a `Rectangle(1, 1, 2, 2)` and wanna test if the position `3, 1` is contained on it.

Let's do the math:

```
_x = 1
_y = 1
_width = 2
_height = 2
px = 3
py = 1

_x <= px # 1 <= 3 == true
_y <= py # 1 <= 1 == true
_x + _width >= px # 1 + 2 >= 3 == 3 >= 3 == true
_y + height >= py # 1 + 2 >= 1 == 3 >= 1 == true

true && true && true && true == TRUE
```

So, according to the algorithm, the point and the rectangle are colliding, but, let's draw this time:

```
Rectangle: *
Point: -

01234
0
1 **-
2 **
3
```

As we can see, a Rectangle in X(1) with WIDTH(2) has the most right point at X(2), which made me realize that the current implementation for `right` which is `x + width` is wrong... Because in fact it should be `x + width - 1`...

I tried to just change the `right` implementation, but that breaks tests all over the system, for now I just fixed the `contains` implementation, leaving it at:

```dart
bool contains(num px, num py) {
return x <= px && y <= py && right > px && bottom > py;
}
```

Which works correctly using current `right` and `bottom` implementations.

I would like your thoughts on that, I think the `right` and `bottom` should be correct, but I'm not sure how much it's gonna affect current users.

Thanks.