1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
local test = require('minunit').test
local geom = require 'geometry'
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--
-- geom.point tests
--
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test(
'create point object',
function()
local pt = geom.point(1, 5.5)
assert(pt.x == 1)
assert(pt.y == 5.5)
local mt = getmetatable(pt)
assert(mt.__index == geom.point)
end
)
test(
'point distances',
function()
local a = geom.point(1, 3)
local b = geom.point(4, 7)
assert(a:distance_to(b) == 5)
assert(a:distance_to(a) == 0)
end
)
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--
-- geom.square tests
--
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test(
'create square object',
function()
local s = geom.square(geom.point(2, 5), 1)
assert(s.center.x == 2)
assert(s.center.y == 5)
assert(s.span == 1)
assert(s.x.min == 1.5)
assert(s.x.max == 2.5)
assert(s.y.min == 4.5)
assert(s.y.max == 5.5)
end
)
test(
'check if point is in square',
function()
local center = geom.point(1, 1)
local square = geom.square(center, 2)
local interior_points = {
geom.point(1, 1),
geom.point(2, 2),
geom.point(2, 0.1),
geom.point(0.1, 0.1),
}
for _, point in ipairs(interior_points) do
assert(
square:contains(point),
string.format(
'point (%0.2f, %0.2f) was incorrectly outside!',
point.x, point.y
)
)
end
local exterior_points = {
geom.point(0, 0),
geom.point(10, 10),
geom.point(0, 1),
geom.point(1, 0),
geom.point(-2, 1),
geom.point(1, -2),
}
for _, point in ipairs(exterior_points) do
assert(
not square:contains(point),
string.format(
'point (%0.2f, %0.2f) was incorrectly inside!',
point.x, point.y
)
)
end
end
)
test(
'subdivide a square',
function()
local center = geom.point(2, 2)
local square = geom.square(center, 4)
local children = square:divide()
assert(
#children == 4,
string.format(
'incorrect number of children: %d',
#children
)
)
local aa = children[1]
assert(aa.center.x == 1)
assert(aa.center.y == 1)
assert(aa.span == 2)
local ba = children[2]
assert(ba.center.x == 3)
assert(ba.center.y == 1)
assert(ba.span == 2)
local ab = children[3]
assert(ab.center.x == 1)
assert(ab.center.y == 3)
assert(ab.span == 2)
local bb = children[4]
assert(bb.center.x == 3)
assert(bb.center.y == 3)
assert(bb.span == 2)
end
)
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--
-- geom.qt_node
--
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test(
'create qt_node',
function()
local center = geom.point(2, 2)
local node = geom.qt_node(center, 4)
assert(node.square.center.x == 2)
assert(node.square.center.y == 2)
assert(node.square.span == 4)
assert(not node:is_leaf())
assert(not node.point)
end
)
|