-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_factory.py
101 lines (83 loc) · 2.35 KB
/
table_factory.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import random
from typing import Tuple, Any, Iterable
from shapely.geometry import MultiPoint, Polygon
from table import Table, TableTemplate
from util import xy_combinations, with_dimensions
def _create_rect(
*,
width: float,
height: float,
chairs: Tuple[Tuple[float, float], ...],
):
return TableTemplate(
exterior=Polygon(xy_combinations(
x=width / 2,
y=height / 2,
)),
chairs=MultiPoint(chairs),
width=width,
height=height
)
def fixed(
*,
length: float,
number: int,
) -> Iterable[float]:
if number == 0:
return ()
inc = length / (2 * number)
initial = - length / 2
return (initial + inc * i for i in range(1, 2 * number, 2))
def x_fixed(
*,
x,
height: float,
number: int,
) -> Iterable[Tuple[float, float]]:
return ((x, y) for y in fixed(length=height, number=number))
def y_fixed(
*,
y,
width: float,
number: int,
) -> Iterable[Tuple[float, float]]:
return ((x, y) for x in fixed(length=width, number=number))
def _create_ltrb(
*,
width: float,
height: float,
ltrb: str,
chair_offset: float = 30,
):
l, t, r, b = map(int, ltrb)
return _create_rect(
width=width,
height=height,
chairs=(
*x_fixed(x=- width / 2 - chair_offset, height=height, number=l),
*y_fixed(y=height / 2 + chair_offset, width=width, number=t),
*reversed(tuple(x_fixed(x=width / 2 + chair_offset, height=height, number=r))),
*reversed(tuple(y_fixed(y=- height / 2 - chair_offset, width=width, number=b))),
)
)
@with_dimensions
def create_template(name: str, **kwargs: Any) -> TableTemplate:
return globals()['_create_' + name](**kwargs)
def create_table(
name: str,
room_dims: Tuple[float, float],
**kwargs,
):
room_x, room_y = room_dims
return Table(
template=create_template(name, **kwargs),
offset_x=random.randrange(-room_x//2, room_x//2)*100,
offset_y=random.randrange(-room_y//2, room_y//2)*100,
)
def create_multiple(
n: int,
name: str,
room_dims: Tuple[float, float],
**kwargs,
):
return [create_table(name, room_dims, **kwargs) for _ in range(n)]