Class Rectangle
Rectangle represents a located area in a two dimensional space. Its members are position and size. Position means the upper left corner.
Rectangle is an auxiliar class to be used with drawing routines for instance.
CONSTRUCTION
COMPARISION
OTHER METHODS
main
// Create a new rectangle, which upper left corner is at (x,y) = (5,5) and the size 10 * 50
rectangle r1 = 5, 5 size 10, 50
// Create a new rectangle, which left corner is at origo and the size spans the whole screen size
rectangle r2 = 0, 0 size screen.get size
position p = 1, 10
size s = 20, 50
// Create a new rectangle using existing position & size classes
rectangle r3 = p size s
Creates a new rectangle that is an exact copy of sourceRectangle.
Parameters
- sourceRectangle - The rectangle that will be copied
main
rectangle r1 = 0, 0 size 100, 100
rectangle r2 = r1.copy // Rectangle sized 100x100, positioned at top left corner.
Creates a new rectangle with the given position (that is, the upper left corner) and the size.
Parameters
- p - Upper left corner of the rectangle
- s - Size of the rectangle
Exceptions
- InvalidArgumentException - If the size is not a valid size
main
rectangle r = 0, 0 size 100, 100 // Rectangle sized 100x100, positioned at top left corner.
Comparision
Parameters
- r1 - Operand
- r2 - Operand
Return value
- true, if operands are equal, otherwise false.
main
rectangle r1 = 0, 0 size 100, 100 // Rectangle sized 100x100, positioned at top left corner.
rectangle r2 = 10,10 size 90, 90 // Rectangle sized 90x90, positioned at 10, 10.
bool result = r1 == r2 // result == false
Comparision
Parameters
- r1 - Operand
- r2 - Operand
Return value
- true, if operands are not equal, otherwise false.
main
rectangle r1 = 0, 0 size 100, 100 // Rectangle sized 100x100, positioned at top left corner.
rectangle r2 = 10,10 size 90, 90 // Rectangle sized 90x90, positioned at 10, 10.
bool result = r1 != r2 // result == true
Get the position.
Return value
main
rectangle r = 10,10 size 90, 90 // Rectangle sized 90x90, positioned at 10, 10.
position p = r.get position // position == 10, 10
(size) get size
Get the size.
Return value
main
rectangle r = 10,10 size 90, 90 // Rectangle sized 90x90, positioned at 10, 10.
size s = r.get size // size == 90, 90
(int) get x
Get the x-coordinate of the position.
Return value
main
rectangle r = 10,20 size 90, 100 // Rectangle sized 90x100, positioned at 10, 20.
int x = r.get x // x == 10
(int) get y
Get the y-coordinate of the position.
Return value
main
rectangle r = 10,20 size 90, 100 // Rectangle sized 90x100, positioned at 10, 20.
int y = r.get y // x == 20
(int) get bottom
Get the y-coordinate of a bottom side of the rectangle. This is the same as y-coordinate + height.
Return value
main
rectangle r = 10,20 size 90, 100 // Rectangle sized 90x100, positioned at 10, 20.
int bottom = r.get bottom // bottom == 120
(int) get right
Get the x-coordinate of the right side of the rectangle. This is the same as x-coordinate + width.
Return value
main
rectangle r = 10,20 size 90, 100 // Rectangle sized 90x100, positioned at 10, 20.
int right = r.get right // right == 100
(int) get width
Get the width of the rectangle.
Return value
main
rectangle r = 10,20 size 90, 100 // Rectangle sized 90x100, positioned at 10, 20.
int width = r.get width // width == 100
(int) get height
Get the height of the rectangle.
Return value
main
rectangle r = 10,20 size 90, 100 // Rectangle sized 90x100, positioned at 10, 20.
int height = r.get height // height == 90
set position to <position newPosition>
Assing a new value for the position.
Parameters
main
rectangle r = 10,20 size 90, 100 // Rectangle sized 90x100, positioned at 10, 20.
r.set position to 0,0 // Rectangle sized 90x100, positioned at top left corner.
set size to <size newSize>
Assing a new value for the size.
Parameters
Exceptions
- InvalidArgumentException - If the new value is not a valid size
main
rectangle r = 10,20 size 90, 100 // Rectangle sized 90x100, positioned at 10, 20.
r.set size to 240,100 // Rectangle sized 240x100, positioned at 10, 20.
set x to <int newX>
Assing a new value for the x-coordinate of the position.
Parameters
main
rectangle r = 10,20 size 90, 100 // Rectangle sized 90x100, positioned at 10, 20.
r.set x to 20 // Rectangle sized 90x100, positioned at 20, 20.
set y to <int newY>
Assing a new value for the y-coordinate of the position.
Parameters
main
rectangle r = 10,20 size 90, 100 // Rectangle sized 90x100, positioned at 10, 20.
r.set y to 10 // Rectangle sized 90x100, positioned at 10, 10.
set width to <int newWidth>
Assing a new value for the width of the rectangle.
Parameters
Exceptions
- InvalidArgumentException - If the new value is negative
main
rectangle r = 10,20 size 90, 100 // Rectangle sized 90x100, positioned at 10, 20.
r.set width to 100 // Rectangle sized 100x100, positioned at 10, 20.
set height to <int newHeight>
Assing a new value for the height of the rectangle.
Parameters
Exceptions
- InvalidArgumentException - If the new value is negative
main
rectangle r = 10,20 size 90, 100 // Rectangle sized 90x100, positioned at 10, 20.
r.set height to 90 // Rectangle sized 90x90, positioned at 10, 20.
set right to <int right>
Assing a new value for the right of the rectangle.
Parameters
Exceptions
- InvalidArgumentException - If the new value is negative
main
rectangle r = 10,20 size 90, 100 // Rectangle sized 90x100, positioned at 10, 20.
r.set right to 360 // Rectangle sized 350x100, positioned at 10, 20.
set bottom to <int bottom>
Assing a new value for the bottom of the rectangle.
Parameters
Exceptions
- InvalidArgumentException - If the new value is negative
main
rectangle r = 10,20 size 90, 100 // Rectangle sized 90x100, positioned at 10, 20.
r.set bottom to 360 // Rectangle sized 90x340, positioned at 10, 20.