ANONYMOUS wrote:
Hi, I having trouble wrapping my head around the given answer for this question on the lms. Specifically, the part about checking if a point is within a rectangle as this is the Boolean used:
return x >= this.x && x <= this.x+width && y >= this.y && y <= this.y+height;
In this case above I tried running the example (-1,1) within a rectangle with dimensions:
x = -2, y =2, height = 2 and width = 2.
-1 >= -2 is True
-1 <= -2+2 is True
1 >= 2 FALSE **********************(This is the part I'm having an issue with)
1 <= 2+2 True
The point (-1,1) should be within a rectangle with these parameters correct?
Or have I misinterpreted this question?
This is a quirk of programming convention that was unfortunately not specified in the question:
In programming, we often do geometry in "screen coordinates" such that the origin is the top left corner of the screen, with x
increasing to the right and y
increasing downwards.
This reverses the y-axis compared to classic Cartesian coordinates.
This is why the question chose the upper-left corner as the "anchor" of the rectangle.
In classic Cartesian coordinates, you are correct, (-1, 1) is within the rectangle you specified, but not in screen coordinates.
If such a question were run in a test, any reasonable coordinate system would be accepted as a valid answer, so long as you are consistent in its use.