# Bungalow: a single-storey house, 13.17 x 16.97 m outside and about 181 m² of rooms, drawn to scale
# from its owners' plan to answer their questions about it. The walls are cut at 1 m so every room
# shows; each door is swung the way the plan draws it; the furniture is the library's, at real sizes.
# Orange is what was proposed: a cross wall in the dressing room (so the bedroom door doesn't open
# onto the wardrobes), two cupboards, a shelf wall in the garage. The dimensions are the clearances
# that were checked. x east, y north; (0, 0) = the inner south-west corner of the entrance hall;
# the north wall's inner face is y = 16.3.
units("m")
WALL, CAB, NEW, WH, GREY = "#f1eee8", "#e8e4dc", "#f2a34b", "#f4f2ee", "#7d8590"
H = 1.0

def wall(x0, y0, x1, y1, h):
    return box(x1 - x0, y1 - y0, h, at=(x0, y0, 0), name="wall", color=WALL)

def door_in(x, y, turn, w, open, hinge, swing, color):
    # the frame's corner goes to (x, y): where it falls inside the whole door is read off a trial one
    t = door(w=w, h=210, turn=turn, open=open, hinge=hinge, swing=swing, at=(0, 0, 0))
    fx, fy = t.part("frame top").min[0], t.part("frame top").min[1]
    t.delete()
    return door(w=w, h=210, turn=turn, open=open, hinge=hinge, swing=swing, color=color, at=(x - fx, y - fy, 0))

# ---------- ground ----------
box(21.2, 18.5, 0.1, at=(-7.7, -1.0, -0.2), name="garden", color="#bcd6a0")
box(13.17, 11.97, 0.1, at=(-0.335, -0.335, -0.1), name="floor", color="oak planks")
box(10.17, 5.0, 0.1, at=(2.665, 11.635, -0.1), name="floor", color="oak planks")
box(6.685, 5.8, 0.1, at=(-7.02, -0.15, -0.1), name="garage floor", color="#c9c9c6")
box(3.0, 5.0, 0.1, at=(-0.335, 11.635, -0.1), name="terrace", color="#d6d2ca tile")
box(3.1, 6.8, 0.1, at=(-5.3, 8.1, -0.1), name="Pool", color="#7cc4e0")

# ---------- outer walls (33.5 cm), 1.4 m high under the high windows ----------
for x0, x1, h in ((2.665, 3.815, H), (3.815, 6.815, 1.4), (6.815, 9.035, H), (9.035, 12.035, 1.4), (12.035, 12.835, H)):
    wall(x0, 16.3, x1, 16.635, h)
for y0, y1, h in ((12.175, 16.3, H), (11.275, 12.175, 1.4), (0, 11.275, H)):
    wall(12.5, y0, 12.835, y1, h)
for x0, x1, h in ((-0.335, 0.39, H), (1.39, 7.745, H), (7.745, 8.645, 1.4), (8.645, 12.835, H)):
    wall(x0, -0.335, x1, 0, h)
wall(2.665, 16.0, 3.0, 16.3, H)
wall(2.665, 11.635, 3.0, 12.9, H)
wall(-0.335, 11.30, 0.05, 11.635, H)
wall(2.32, 11.30, 3.0, 11.635, H)
wall(-0.335, 10.6, 0, 11.30, H)
wall(-0.335, 3.10, 0, 7.5, H)
wall(-0.335, 0, 0, 2.20, H)

# ---------- inner walls (12.5 cm) ----------
for y0, y1 in ((10.60, 16.3), (6.55, 9.70), (4.72, 5.70)):
    wall(7.25, y0, 7.375, y1, H)                    # living room and kitchen | dressing room and hall
wall(7.375, 11.575, 7.60, 11.70, H)
wall(8.50, 11.575, 8.675, 11.70, H)                 # hall | dressing room, the bedroom door between
for y0, y1 in ((6.00, 12.30), (4.70, 5.10), (0, 3.80)):
    wall(8.675, y0, 8.80, y1, H)                    # hall | baths and children's rooms
wall(8.80, 12.175, 9.77, 12.30, H)
wall(10.67, 12.175, 12.5, 12.30, H)                 # bedroom | its bathroom
wall(8.80, 9.825, 12.5, 9.95, H)                    # bathroom | child 2
wall(8.80, 4.85, 12.5, 4.975, H)                    # child 2 | child 1
box(2.1, 0.2, 2.2, at=(4.075, 11.30, 0), name="wall piece", color=WALL)
wall(2.10, 6.625, 7.25, 6.75, H)                    # kitchen | pantry and laundry
wall(2.10, 6.25, 2.225, 6.625, H)
wall(2.10, 4.845, 2.225, 5.45, H)
wall(4.725, 4.845, 4.85, 6.625, H)
wall(2.10, 4.72, 7.25, 4.845, H)
for x0, x1 in ((0, 0.31), (1.31, 3.20), (4.10, 7.60), (8.50, 8.675)):
    wall(x0, 3.395, x1, 3.52, H)                    # hall | entrance, study, children's bathroom
wall(2.425, 0, 2.55, 3.395, H)
wall(6.25, 0, 6.375, 3.395, H)

# ---------- garage and plant room ----------
wall(-7.02, -0.15, -6.87, 5.65, H)
wall(-6.87, 5.50, -4.41, 5.65, H)
wall(-2.76, 5.50, -0.335, 5.65, H)
wall(-6.87, -0.15, -6.6, 0, H)
wall(-0.6, -0.15, -0.335, 0, H)
wall(-2.47, 3.16, -2.37, 4.44, H)
wall(-2.47, 5.34, -2.37, 5.50, H)
wall(-2.37, 3.16, -0.335, 3.26, H)
box(1.9, 4.8, 0.75, at=(-5.32, 0.17, 0), name="car", color="#9aa0a6")
box(1.7, 2.4, 0.55, at=(-5.22, 1.2, 0.75), name="car", color="#3a3f47")

# ---------- windows: sill 1.40 north and baths, 1.00 the rooms, floor-length to the terrace and by the dining table ----------
window(w=300, h=80, at=(3.815, 16.43, 1.4))
window(w=300, h=80, at=(9.035, 16.43, 1.4))
window(w=200, h=120, at=(12.63, 13.435, 1.0), turn=90)
window(w=90, h=80, at=(12.63, 11.275, 1.4), turn=90)
window(w=200, h=120, at=(12.63, 6.40, 1.0), turn=90)
window(w=200, h=120, at=(12.63, 1.425, 1.0), turn=90)
window(w=200, h=120, at=(4.01, -0.21, 1.0))
window(w=90, h=80, at=(7.745, -0.21, 1.4))
window(w=310, h=220, panes=2, at=(2.79, 12.9, 0), turn=90)       # living room -> terrace
window(w=227, h=220, panes=2, at=(0.05, 11.43, 0))               # dining -> terrace
window(w=310, h=220, panes=2, at=(-0.21, 7.5, 0), turn=90)       # by the dining table

# ---------- doors, each swung the way the plan draws it ----------
door_in(7.2625, 9.70, 270, 90, 90, "left", "in", WH)             # living room -> hall
door_in(7.60, 11.5875, 0, 90, 90, "right", "in", WH)             # hall -> bedroom (into the dressing room)
door_in(9.77, 12.1875, 0, 90, 90, "left", "out", WH)             # bedroom -> bathroom
door_in(8.6875, 5.10, 270, 90, 90, "left", "in", WH)             # child 2
door_in(8.6875, 3.80, 270, 90, 90, "right", "in", WH)            # child 1
door_in(7.60, 3.4075, 0, 90, 90, "right", "out", WH)             # children's bathroom
door_in(3.20, 3.4075, 0, 90, 90, "left", "out", WH)              # study
door_in(0.31, 3.4075, 0, 100, 90, "right", "in", WH)             # entrance -> living room
door_in(0.39, -0.22, 0, 100, 0, "right", "in", "#4a4440")        # front door, shut
door_in(-0.22, 2.20, 90, 90, 90, "left", "in", WH)               # entrance -> garage
door_in(2.1125, 5.45, 270, 80, 90, "left", "in", WH)             # pantry
door_in(7.2625, 5.70, 90, 85, 90, "right", "in", WH)             # laundry
door_in(-2.47, 4.44, 90, 90, 90, "left", "in", WH)               # plant room
door_in(-4.41, 5.525, 180, 165, 0, "left", "in", WH)             # garage -> garden

# ---------- living room ----------
sofa(w=260, d=95, arms="none", color=GREY, at=(3.73, 15.30, 0))
sofa(w=245, d=85, arms="none", color=GREY, at=(6.36, 13.80, 0), turn=270)
coffee_table(w=120, d=87, at=(4.565, 14.0, 0))
tv_unit(w=178, d=50, h=45, at=(4.2, 11.50, 0), turn=180)
box(1.4, 0.04, 0.8, at=(4.425, 11.50, 0.95), name="TV", color="#16171a")

# ---------- dining ----------
dining_table(w=200, d=90, at=(0.99, 8.315, 0), turn=90)
for y in (8.425, 9.09, 9.755):
    chair(at=(0.44, y, 0), turn=90)
    chair(at=(1.94, y, 0), turn=270)
chair(at=(1.215, 10.365, 0))
chair(at=(1.215, 7.765, 0), turn=180)

# ---------- kitchen: the run on the pantry wall, tall units on the east wall, the island ----------
fridge(w=88, d=72, h=200, at=(2.14, 6.75, 0), turn=180)
x = 3.02
for w, kind in ((115, "doors"), (60, "drawers"), (60, "hob_drawers"), (60, "drawers"), (60, "dishwasher"), (68, "doors")):
    base_unit(w=w, d=60, h=91, kind=kind, at=(x, 6.75, 0), turn=180)
    x += w / 100
for y, kind in ((7.40, "larder"), (8.00, "oven"), (8.60, "larder")):
    tall_unit(w=60, d=60, h=220, kind=kind, at=(6.65, y, 0), turn=270)
for i, kind in enumerate(("drawers", "sink", "pan_drawers")):
    base_unit(w=60, d=60, h=91, kind=kind, at=(3.86 + 0.6 * i, 8.23, 0))
    base_unit(w=60, d=60, h=91, kind="doors", at=(3.86 + 0.6 * i, 8.85, 0), turn=180)

# ---------- bedroom: wardrobes on the west wall, a free-standing block, the bed's head under the east window ----------
wardrobe(w=290, d=60, h=220, at=(7.375, 13.40, 0), turn=90)
wardrobe(w=180, d=60, h=220, at=(8.975, 13.60, 0), turn=270)
bed(w=200, l=200, at=(10.37, 13.40, 0), turn=270)
bedside_table(at=(12.09, 15.50, 0), turn=270)
bedside_table(at=(12.09, 12.93, 0), turn=270)
bathtub(w=170, d=75, at=(8.80, 9.96, 0), turn=90)
washbasin(w=76, at=(9.79, 9.95, 0), turn=180)
toilet(at=(10.75, 9.95, 0), turn=180)
shower(w=150, d=98, at=(11.52, 9.95, 0), turn=270)

# ---------- the children's rooms ----------
bed(w=140, l=200, at=(8.81, 7.70, 0))
wardrobe(w=150, d=50, at=(10.95, 9.325, 0))
desk(w=135, d=57, at=(11.92, 7.05, 0), turn=270)
chair(at=(11.37, 7.5, 0), turn=90)
wardrobe(w=185, d=52, at=(10.65, 4.975, 0), turn=180)
wardrobe(w=185, d=52, at=(10.65, 4.33, 0))
bed(w=140, l=200, at=(8.81, 0.01, 0), turn=180)
wardrobe(w=146, d=55, at=(10.95, 0, 0), turn=180)
desk(w=140, d=57, at=(11.92, 1.45, 0), turn=270)
chair(at=(11.37, 1.92, 0), turn=90)

# ---------- study, children's bathroom, entrance, pantry, laundry ----------
desk(w=140, d=90, at=(4.60, 1.99, 0), turn=90)
chair(at=(5.55, 2.47, 0), turn=270)
sofa(w=198, d=80, color=GREY, at=(3.755, 0, 0), turn=180)
shower(w=135, d=100, at=(6.375, 0, 0), turn=180)
toilet(at=(6.375, 1.25, 0), turn=90)
washbasin(w=70, at=(6.375, 2.0, 0), turn=90)
box(0.55, 0.52, 2.0, at=(6.375, 2.875, 0), name="cupboard", color=CAB)
wardrobe(w=169, d=60, at=(1.825, 0, 0), turn=270)
wardrobe(w=169, d=60, at=(1.825, 1.70, 0), turn=270)
bookcase(w=200, d=40, h=200, books=False, color=WH, at=(2.70, 6.225, 0))
bookcase(w=120, d=40, h=200, books=False, color=WH, at=(4.325, 4.85, 0), turn=270)
washing_machine(at=(4.87, 6.045, 0))
washing_machine(at=(5.49, 6.045, 0))

# ---------- terrace ----------
dining_table(w=300, d=90, color="#8a8f99", at=(0.83, 12.59, 0), turn=90)
for y in (12.9, 13.9, 14.9):
    chair(color="#8a8f99", at=(0.28, y, 0), turn=90)
    chair(color="#8a8f99", at=(1.78, y, 0), turn=270)

# ---------- proposals (orange) ----------
box(0.6, 2.0, 2.2, at=(0, 4.70, 0), name="proposed: cupboard 2.00 x 0.60", color=NEW)
box(2.1, 0.4, 2.2, at=(4.075, 10.90, 0), name="proposed: cupboard 2.10 x 0.40", color=NEW)
box(0.5, 5.2, 2.0, at=(-6.87, 0.3, 0), name="proposed: shelf wall 5.20 x 0.50", color=NEW)
box(2.2, 0.1, 2.2, at=(7.375, 13.30, 0), name="proposed: cross wall 2.20", color=NEW)

# ---------- what was measured ----------
dim((7.375, 8.9, 0), (8.675, 8.9, 0), offset=0, text="1.30")
dim((8.3, 3.52, 0), (8.3, 11.575, 0), offset=0, text="8.05")
dim((2.9, 3.52, 0), (2.9, 4.72, 0), offset=0, text="1.20")
dim((4.75, 7.35, 0.95), (4.75, 8.23, 0.95), offset=0, text="0.90")
dim((5.7, 9.45, 0), (5.7, 10.90, 0), offset=0, text="1.45")
dim((0, 8.98, 0), (0.99, 8.98, 0), offset=0, text="1.00")
dim((0.6, 5.9, 0), (2.10, 5.9, 0), offset=0, text="1.50")
dim((-6.37, 2.6, 0), (-5.32, 2.6, 0), offset=0, text="1.05")
dim((8.2, 11.70, 0), (8.2, 13.30, 0), offset=0, text="1.60")
dim((9.9, 12.30, 0), (9.9, 13.30, 0), offset=0, text="1.00")

# ---------- names ----------
for text, x, y in (("Hall 16.4 m²", 5.6, 4.1),
                   ("Dressing", 8.47, 15.2), ("Bath", 10.6, 11.3), ("Child 2", 10.4, 6.6),
                   ("Child 1", 10.3, 3.2), ("Bath", 7.6, 1.6), ("Study", 4.0, 1.4), ("Entrance", 0.9, 1.6),
                   ("Pantry", 3.5, 5.5), ("Laundry", 6.0, 5.4), ("Plant", -1.35, 4.5), ("Garage", -2.0, 1.5),
                   ("Pool", -3.75, 11.5)):
    label(text, at=(x, y, 0))
label("Living", at=(5.17, 14.4, 0.45))
label("Kitchen", at=(4.76, 8.85, 0.95))
label("Dining", at=(1.44, 9.8, 0.8))
label("Bedroom", at=(11.4, 14.4, 0.6))
label("Terrace", at=(1.28, 14.5, 0.8))
label("2 sliding doors", at=(1.75, 12.15, 0))
label("fixed glazing", at=(-1.45, 8.2, 0), to=(-0.2, 8.6, 0.3))
label("cupboard 2.00 × 0.60", at=(-2.4, 6.9, 0), to=(0.05, 5.7, 2.2))
label("cupboard 2.10 × 0.40", at=(5.1, 11.1, 2.3))
label("shelves 5.20 × 0.50", at=(-6.6, 2.8, 2.1))
label("cross wall 2.20", at=(8.47, 13.28, 1.2))
label("orange = proposed", at=(-4.6, 16.4, 0))
