-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex17.2.lua
63 lines (61 loc) · 1.28 KB
/
ex17.2.lua
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
local function disk (cx, cy, r)
return function (x, y)
return (x - cx)^2 + (y - cy)^2 <= r^2
end
end
local function rect (left, right, bottom, up)
return function (x, y)
return left <= x and x <= right and
bottom <= y and y <= up
end
end
local function complement (r)
return function (x, y)
return not r(x, y)
end
end
local function union (r1, r2)
return function (x, y)
return r1(x, y) or r2(x, y)
end
end
local function intersection (r1, r2)
return function (x, y)
return r1(x, y) and r2(x, y)
end
end
local function difference (r1, r2)
return function (x, y)
return r1(x, y) and not r2(x, y)
end
end
local function translate (r, dx, dy)
return function (x, y)
return r(x - dx, y - dy)
end
end
local function plot (r, M, N)
io.write("P1\n", M, " ", N, "\n")
-- header
for i = 1, N do
-- for each line
local y = (N - i*2)/N
for j = 1, M do
-- for each column
local x = (j*2 - M)/M
io.write(r(x, y) and "1" or "0")
end
io.write("\n")
end
end
local M = {
disk = disk,
rect = rect,
complement = complement,
union = union,
intersection = intersection,
difference = difference,
translate = translate,
plot = plot
}
return M