2023-04-19 16:41:11 +00:00
|
|
|
-- Copyright (C) 2019-2023 Igara Studio S.A.
|
2018-08-22 17:56:07 +00:00
|
|
|
-- Copyright (C) 2018 David Capello
|
|
|
|
--
|
|
|
|
-- This file is released under the terms of the MIT license.
|
|
|
|
-- Read LICENSE.txt for more information.
|
|
|
|
|
|
|
|
local rc = Rectangle()
|
|
|
|
assert(rc.x == 0)
|
|
|
|
assert(rc.y == 0)
|
|
|
|
assert(rc.width == 0)
|
|
|
|
assert(rc.height == 0)
|
2018-08-31 18:07:51 +00:00
|
|
|
assert(rc.isEmpty)
|
2018-08-22 17:56:07 +00:00
|
|
|
|
|
|
|
rc = Rectangle(1, 2, 3, 4)
|
|
|
|
assert(rc.x == 1)
|
|
|
|
assert(rc.y == 2)
|
2023-04-19 16:41:11 +00:00
|
|
|
assert(rc.w == 3) -- Short w/h form
|
|
|
|
assert(rc.h == 4)
|
|
|
|
assert(rc.width == 3) -- Long width/height form
|
2018-08-22 17:56:07 +00:00
|
|
|
assert(rc.height == 4)
|
2022-12-20 21:04:51 +00:00
|
|
|
assert(rc.origin == Point(1, 2))
|
|
|
|
assert(rc.size == Size(3, 4))
|
2018-08-31 18:07:51 +00:00
|
|
|
assert(not rc.isEmpty)
|
2019-03-29 18:54:50 +00:00
|
|
|
assert("Rectangle{ x=1, y=2, width=3, height=4 }" == tostring(rc))
|
2018-08-22 17:56:07 +00:00
|
|
|
|
|
|
|
local rc2 = Rectangle(rc)
|
|
|
|
assert(rc2.x == 1)
|
|
|
|
assert(rc2.y == 2)
|
|
|
|
assert(rc2.width == 3)
|
|
|
|
assert(rc2.height == 4)
|
|
|
|
|
2022-12-20 20:52:16 +00:00
|
|
|
rc.x = 5
|
|
|
|
rc.y = 6
|
|
|
|
rc.width = 7
|
|
|
|
rc.height = 8
|
2018-08-22 17:56:07 +00:00
|
|
|
assert(rc.x == 5)
|
|
|
|
assert(rc.y == 6)
|
|
|
|
assert(rc.width == 7)
|
|
|
|
assert(rc.height == 8)
|
|
|
|
|
2022-12-20 21:04:51 +00:00
|
|
|
rc.origin = Point(10, 12)
|
|
|
|
assert(rc.x == 10)
|
|
|
|
assert(rc.y == 12)
|
|
|
|
assert(rc.width == 7)
|
|
|
|
assert(rc.height == 8)
|
|
|
|
|
|
|
|
rc.size = Size(32, 64)
|
|
|
|
assert(rc.x == 10)
|
|
|
|
assert(rc.y == 12)
|
|
|
|
assert(rc.width == 32)
|
|
|
|
assert(rc.height == 64)
|
|
|
|
|
2018-08-22 17:56:07 +00:00
|
|
|
rc = Rectangle{x=2, y=3, width=4, height=5}
|
|
|
|
assert(rc.x == 2)
|
|
|
|
assert(rc.y == 3)
|
|
|
|
assert(rc.width == 4)
|
|
|
|
assert(rc.height == 5)
|
2018-11-14 19:53:24 +00:00
|
|
|
|
2023-04-19 16:41:11 +00:00
|
|
|
rc = Rectangle{x=0, y=1, w=2, h=3}
|
|
|
|
assert(rc.x == 0)
|
|
|
|
assert(rc.y == 1)
|
|
|
|
assert(rc.w == 2)
|
|
|
|
assert(rc.h == 3)
|
|
|
|
|
2019-07-19 21:39:32 +00:00
|
|
|
rc = Rectangle{6, 7, 8, 9}
|
|
|
|
assert(rc.x == 6)
|
|
|
|
assert(rc.y == 7)
|
2023-04-19 16:41:11 +00:00
|
|
|
assert(rc.w == 8)
|
|
|
|
assert(rc.h == 9)
|
2019-07-19 21:39:32 +00:00
|
|
|
|
2022-12-21 16:01:03 +00:00
|
|
|
rc = Rectangle(Point(2, 3), Size(4, 5))
|
|
|
|
assert(rc.x == 2)
|
|
|
|
assert(rc.y == 3)
|
|
|
|
assert(rc.width == 4)
|
|
|
|
assert(rc.height == 5)
|
|
|
|
|
2018-11-14 19:53:24 +00:00
|
|
|
-- Rectangle:contains
|
|
|
|
|
|
|
|
local a = Rectangle{x=2, y=3, width=4, height=5}
|
|
|
|
local b = Rectangle{x=3, y=4, width=1, height=1}
|
|
|
|
assert(a:contains(b))
|
|
|
|
assert(not b:contains(a))
|
|
|
|
|
|
|
|
-- Rectangle:intersect
|
|
|
|
|
|
|
|
assert(a:intersects(b))
|
|
|
|
assert(b == a:intersect(b))
|
|
|
|
|
|
|
|
a = Rectangle{x=2, y=3, width=4, height=5}
|
|
|
|
b = Rectangle{x=3, y=4, width=4, height=5}
|
|
|
|
c = Rectangle{x=3, y=4, width=3, height=4}
|
|
|
|
assert(c == a:intersect(b))
|
|
|
|
assert(c == b:intersect(a))
|
2019-09-23 13:22:33 +00:00
|
|
|
assert((a & b) == c)
|
2018-11-14 19:53:24 +00:00
|
|
|
|
|
|
|
-- Rectangle:union
|
|
|
|
|
2019-09-23 13:22:33 +00:00
|
|
|
c = Rectangle{x=2, y=3, width=5, height=6}
|
|
|
|
assert(c == a:union(b))
|
|
|
|
assert(c == (a | b))
|