-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_chessboard.py
85 lines (75 loc) · 2.24 KB
/
make_chessboard.py
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
from collections import namedtuple
import json
import copy
class Point(object):
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def __repr__(self):
return f"Point({self.x}, {self.y}, {self.z})"
def make_rectangle(A: Point, W: Point, H: Point, r, g, b) -> dict:
return {
"type": "rectangle",
"a": {"x": A.x, "y": A.y, "z": A.z},
"w": {"x": W.x, "y": W.y, "z": W.z},
"h": {"x": H.x, "y": H.y, "z": H.z},
"mat": {
"type": "metal",
"albedo": {"x": r / 255, "y": g / 255, "z": b / 255},
"fuzz": 0,
},
}
def make_chessboard(starting: Point, w: int, l: int) -> str:
config = {"random": False}
spheres = [
{
"type": "sphere",
"center": {"x": 0, "y": 0.5, "z": -1},
"radius": 0.5,
"mat": {"type": "lambertian", "albedo": {"x": 0.1, "y": 0.2, "z": 0.5}},
},
{
"type": "sphere",
"center": {"x": 1, "y": 0.5, "z": -1},
"radius": 0.5,
"mat": {
"type": "metal",
"albedo": {"x": 0.8, "y": 0.6, "z": 0.2},
"fuzz": 0,
},
},
{
"type": "sphere",
"center": {"x": -1, "y": 0.5, "z": -1},
"radius": 0.5,
"mat": {"type": "dielectric", "refindex": 1.5},
},
]
cur = copy.copy(starting)
square_dim = 0.5
board = []
row_start_color = 0xFF
while cur.z < (starting.z + l):
cur.x = starting.x
color = row_start_color
while cur.x < (starting.x + w):
board.append(
make_rectangle(
cur,
Point(cur.x + square_dim, cur.y, cur.z),
Point(0, 0, square_dim),
color,
color,
color,
)
)
color ^= 0xFF
cur.x += square_dim
cur.z += square_dim
row_start_color ^= 0xFF
config["static"] = board + spheres
return json.dumps(config, indent=4)
if __name__ == "__main__":
chessboard = make_chessboard(Point(-8, 0, -8), 16, 16)
print(chessboard)