Create Python code to generate a 2D floor plan for a modern single-story house. Use the Cairo graphics library for drawing. The plan should include the following specifications:
Total Rooms: 7 (including Living Room, Kitchen, Dining Room, 2 Bedrooms, 2 Bathrooms, Entry, Hallway, Storage)
Room Specifications:
Living Room: Include a large window, seating area, and a coffee table.
Kitchen: Include L-shaped countertops, a sink, refrigerator, and stove. Add an island with barstools.
Dining Room: Include a rectangular table with chairs.
Bedrooms: Two bedrooms with beds and nightstands. One should have a walk-in closet.
Bathrooms: Two bathrooms, each with essential fixtures (bathtub or shower, toilet, sink).
Entry: Include a front door and space for a coat rack.
Hallway: Connecting all rooms.
Storage Room: Include shelving units.
Additional Requirements:
Windows: Include windows in every room for natural light.
Lighting: Consider different lighting types for each room (ceiling fixtures, lamps, sconces).
Exterior Features: Include a small porch at the front entry and basic landscaping.
Color Palette: Use neutral tones with accents in decor.
Code Structure:
Use the following libraries:
cairo: For drawing the plan
numpy: To handle any calculations, if necessary
The code should create a PNG file of the house plan.
Expected Output: Provide the complete Python code that, when executed, generates a 2D floor plan as described.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Creating a detailed 2D floor plan programmatically involves defining the layout, dimensions, and features of each room. Below is a Python script using the Cairo graphics library to generate a simple yet comprehensive floor plan based on your specifications. This script outlines the structure, rooms, and key features using basic shapes and annotations.
Overview of the Approach:
Setup:
Initialize a Cairo surface with appropriate dimensions.
Define a scaling factor to convert real-world measurements (meters) to pixels.
Room Layout:
Define positions and sizes for each room on a grid.
Draw walls for each room.
Features and Furniture:
Add doors and windows to appropriate walls.
Insert simple representations of furniture like tables, beds, and appliances.
Exterior Features:
Draw the porch and basic landscaping elements.
Styling:
Use a neutral color palette with accent colors for different features.
Differentiate rooms using light fill colors.
Finalization:
Save the drawing as a PNG file.
Complete Python Code:
python
import cairo
import numpy as np
# Define constants
WIDTH, HEIGHT = 1200, 800 # Image size in pixels
SCALE = 50 # 1 meter = 50 pixels
# Colors
NEUTRAL = (0.9, 0.9, 0.9)
WALL = (0, 0, 0)
WINDOW = (0.529, 0.808, 0.980)
DOOR_COLOR = (0.545, 0.271, 0.075)
FURNITURE = (0.7, 0.7, 0.7)
PORCH_COLOR = (0.82, 0.41, 0.12)
LANDSCAPE = (0.196, 0.804, 0.196)
TEXT_COLOR = (0, 0, 0)
def draw_rectangle(ctx, x, y, width, height, fill=True, stroke=True, color=(0,0,0)):
ctx.save()
ctx.rectangle(x, y, width, height)
if fill:
ctx.set_source_rgb(*color)
ctx.fill_preserve()
if stroke:
ctx.set_source_rgb(*WALL)
ctx.set_line_width(2)
ctx.stroke()
ctx.restore()
def draw_text(ctx, x, y, text, size=12):
ctx.save()
ctx.select_font_face("Sans", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
ctx.set_font_size(size)
ctx.set_source_rgb(*TEXT_COLOR)
ctx.move_to(x, y)
ctx.show_text(text)
ctx.restore()
def main():
# Create an image surface
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
ctx = cairo.Context(surface)
# Fill background
ctx.set_source_rgb(*LANDSCAPE)
ctx.rectangle(0, 0, WIDTH, HEIGHT)
ctx.fill()
# Define house parameters
house_x, house_y = 200, 100
house_width, house_height = 800, 600
# Draw porch
porch_width, porch_depth = 2, 1.5 # in meters
porch_px_width = porch_width * SCALE
porch_px_depth = porch_depth * SCALE
ctx.set_source_rgb(*PORCH_COLOR)
ctx.rectangle(house_x - porch_px_depth, house_y + house_height/2 - porch_px_width/2, porch_px_depth, porch_px_width)
ctx.fill()
# Draw house foundation
ctx.set_source_rgb(*NEUTRAL)
ctx.rectangle(house_x, house_y, house_width, house_height)
ctx.fill_preserve()
ctx.set_source_rgb(*WALL)
ctx.set_line_width(3)
ctx.stroke()
# Define rooms with (x, y, width, height) in meters
rooms = {
"Entry": (0, 0, 3, 2),
"Hallway": (3, 0, 1, 8),
"Living Room": (4, 0, 5, 5),
"Kitchen": (4, 5, 4, 3),
"Dining Room": (8,5,3,3),
"Bedroom 1": (4,8,4,4),
"Bedroom 2": (8,8,4,4),
"Bathroom 1": (12,5,2,2),
"Bathroom 2": (12,8,2,2),
"Storage": (10,0,2,5)
}
# Convert and store room positions in pixels
room_pixels = {}
for name, (x, y, w, h) in rooms.items():
room_pixels[name] = (house_x + x * SCALE, house_y + y * SCALE, w * SCALE, h * SCALE)
# Draw each room
for name, (x, y, w, h) in room_pixels.items():
# Fill room
ctx.set_source_rgb(*NEUTRAL)
ctx.rectangle(x, y, w, h)
ctx.fill_preserve()
# Draw walls
ctx.set_source_rgb(*WALL)
ctx.set_line_width(2)
ctx.rectangle(x, y, w, h)
ctx.stroke()
# Add room name
draw_text(ctx, x + 10, y + 20, name, size=14)
# Draw doors (simple representation as lines)
doors = {
"Entry": ("south", room_pixels["Entry"]),
"Hallway_Living": ("east", room_pixels["Entry"], room_pixels["Hallway"]),
"Hallway_Kitchen": ("south", room_pixels["Hallway"], room_pixels["Kitchen"]),
"Kitchen_Dining": ("east", room_pixels["Kitchen"], room_pixels["Dining Room"]),
"Hallway_Bed1": ("south", room_pixels["Hallway"], room_pixels["Bedroom 1"]),
"Hallway_Bed2": ("south", room_pixels["Hallway"], room_pixels["Bedroom 2"]),
"Bedroom1_Bath1": ("east", room_pixels["Bedroom 1"], room_pixels["Bathroom 1"]),
"Bedroom2_Bath2": ("east", room_pixels["Bedroom 2"], room_pixels["Bathroom 2"]),
"Kitchen_Storage": ("north", room_pixels["Kitchen"], room_pixels["Storage"])
}
for door, params in doors.items():
if door == "Entry":
dir, room = params
if dir == "south":
x1, y1, w, h = room
ctx.set_source_rgb(*DOOR_COLOR)
ctx.set_line_width(4)
ctx.move_to(x1 + w/2 - SCALE/2, y1 + h)
ctx.line_to(x1 + w/2 + SCALE/2, y1 + h)
ctx.stroke()
else:
dir = params[0]
room1 = params[1]
room2 = params[2]
x1, y1, w1, h1 = room1
x2, y2, w2, h2 = room2
if dir == "east":
ctx.set_source_rgb(*DOOR_COLOR)
ctx.set_line_width(4)
ctx.move_to(x1 + w1, y1 + h1/2 - SCALE/2)
ctx.line_to(x1 + w1, y1 + h1/2 + SCALE/2)
ctx.stroke()
elif dir == "south":
ctx.set_source_rgb(*DOOR_COLOR)
ctx.set_line_width(4)
ctx.move_to(x1 + w1/2 - SCALE/2, y1 + h1)
ctx.line_to(x1 + w1/2 + SCALE/2, y1 + h1)
ctx.stroke()
# Draw windows (simple rectangles on walls)
windows = {
"Living Room": ("north", room_pixels["Living Room"], 3),
"Kitchen": ("east", room_pixels["Kitchen"], 2),
"Dining Room": ("north", room_pixels["Dining Room"], 2),
"Bedroom 1": ("west", room_pixels["Bedroom 1"], 1),
"Bedroom 2": ("west", room_pixels["Bedroom 2"], 1),
"Bathroom 1": ("north", room_pixels["Bathroom 1"], 1),
"Bathroom 2": ("north", room_pixels["Bathroom 2"], 1),
"Entry": ("north", room_pixels["Entry"],1),
"Storage": ("south", room_pixels["Storage"],1)
}
for name, (side, room, count) in windows.items():
x, y, w, h = room
ctx.set_source_rgb(*WINDOW)
for i in range(count):
if side == "north":
wx = x + (i +1) * w / (count +1) - SCALE/4
wy = y - SCALE/4
ctx.rectangle(wx, wy, SCALE/2, SCALE/4)
elif side == "south":
wx = x + (i +1) * w / (count +1) - SCALE/4
wy = y + h
ctx.rectangle(wx, wy, SCALE/2, SCALE/4)
elif side == "east":
wx = x + w
wy = y + (i +1) * h / (count +1) - SCALE/4
ctx.rectangle(wx, wy, SCALE/4, SCALE/2)
elif side == "west":
wx = x - SCALE/4
wy = y + (i +1) * h / (count +1) - SCALE/4
ctx.rectangle(wx, wy, SCALE/4, SCALE/2)
ctx.fill()
# Add furniture (simplified as rectangles)
def add_furniture(room, furniture_list):
x, y, w, h = room
for item, (fx, fy, fw, fh) in furniture_list.items():
ctx.set_source_rgb(*FURNITURE)
ctx.rectangle(x + fx * SCALE, y + fy * SCALE, fw * SCALE, fh * SCALE)
ctx.fill_preserve()
ctx.set_source_rgb(*WALL)
ctx.set_line_width(1)
ctx.rectangle(x + fx * SCALE, y + fy * SCALE, fw * SCALE, fh * SCALE)
ctx.stroke()
# Optionally, add labels
draw_text(ctx, x + fx * SCALE + 2, y + fy * SCALE + fh * SCALE / 2, item, size=10)
# Living Room Furniture
living_room = room_pixels["Living Room"]
living_furniture = {
"Window": (1, 0.1, 3, 1),
"Seating": (1, 2, 3, 2),
"Coffee Table": (2, 3, 1, 1)
}
add_furniture(living_room, living_furniture)
# Kitchen Furniture
kitchen = room_pixels["Kitchen"]
kitchen_furniture = {
"Sink": (0.5, 0.5, 1, 0.5),
"Refrigerator": (3.5, 0.5, 1, 1),
"Stove": (1.5, 0.5, 1, 0.5),
"Island": (1.5, 2, 2, 1),
"Barstool": (2, 3, 0.5, 0.5)
}
add_furniture(kitchen, kitchen_furniture)
# Dining Room Furniture
dining = room_pixels["Dining Room"]
dining_furniture = {
"Table": (1,1,2,1),
"Chair": (0.5,1,0.5,0.5),
"Chair": (3,1,0.5,0.5)
}
add_furniture(dining, dining_furniture)
# Bedrooms Furniture
bedroom1 = room_pixels["Bedroom 1"]
bedroom1_furniture = {
"Bed": (0.5,0.5,2,1.5),
"Nightstand": (0.5,2,0.5,0.5),
"Closet": (1.5,2,1,1.5)
}
add_furniture(bedroom1, bedroom1_furniture)
bedroom2 = room_pixels["Bedroom 2"]
bedroom2_furniture = {
"Bed": (0.5,0.5,2,1.5),
"Nightstand": (0.5,2,0.5,0.5)
}
add_furniture(bedroom2, bedroom2_furniture)
# Bathrooms Furniture
bathroom1 = room_pixels["Bathroom 1"]
bathroom1_furniture = {
"Sink": (0.5,0.5,1,0.5),
"Toilet": (1.5,0.5,0.5,0.5),
"Shower": (0.5,1.5,1,1)
}
add_furniture(bathroom1, bathroom1_furniture)
bathroom2 = room_pixels["Bathroom 2"]
bathroom2_furniture = {
"Sink": (0.5,0.5,1,0.5),
"Toilet": (1.5,0.5,0.5,0.5),
"Bathtub": (0.5,1.5,1,1)
}
add_furniture(bathroom2, bathroom2_furniture)
# Storage Furniture
storage = room_pixels["Storage"]
storage_furniture = {
"Shelves": (0.5,0.5,1,3)
}
add_furniture(storage, storage_furniture)
# Add lighting indicators (simple circles)
def add_lighting(room, lighting_list):
x, y, w, h = room
for light, (lx, ly) in lighting_list.items():
ctx.set_source_rgb(1, 1, 0) # Yellow for lights
ctx.arc(x + lx * SCALE, y + ly * SCALE, 5, 0, 2 * np.pi)
ctx.fill()
# Define lighting for rooms
lighting = {
"Living Room": [("Ceiling", (w/2, h/2)) for w,h in [room_pixels["Living Room"][2:]]],
"Kitchen": [("Ceiling", (2,1.5))],
"Dining Room": [("Ceiling", (1.5,1.5))],
"Bedroom 1": [("Ceiling", (2,2))],
"Bedroom 2": [("Ceiling", (2,2))],
"Bathroom 1": [("Ceiling", (1,1))],
"Bathroom 2": [("Ceiling", (1,1))],
"Entry": [("Ceiling", (1.5,1))]
}
for room, lights in lighting.items():
room_coords = room_pixels.get(room)
if room_coords:
add_lighting(room_coords, lights)
# Add front door
entry = room_pixels["Entry"]
ctx.set_source_rgb(*DOOR_COLOR)
ctx.set_line_width(4)
ctx.move_to(entry[0] + entry[2]/2 - SCALE/2, entry[1] + entry[3])
ctx.line_to(entry[0] + entry[2]/2 + SCALE/2, entry[1] + entry[3])
ctx.stroke()
# Save the image
surface.write_to_png("floor_plan.png")
print("Floor plan generated and saved as floor_plan.png")
if __name__ == "__main__":
main()
Explanation of Key Components:
Scaling and Dimensions:
The SCALE constant defines the conversion rate from meters to pixels. Adjusting this value will scale the entire floor plan accordingly.
Room Definitions:
Each room is defined with its position (x, y) and size (width, height) in meters.
Positions are converted to pixels based on the scaling factor.
Drawing Rooms:
Rooms are drawn as rectangles filled with a neutral color and outlined with walls.
Room names are annotated within each room.
Doors and Windows:
Doors are represented as thicker lines in brown color indicating entry points between rooms.
Windows are drawn as lighter blue rectangles on specified walls to indicate natural light sources.
Furniture:
Basic furniture items are represented by smaller rectangles within each room.
Each piece of furniture is labeled for clarity.
Lighting:
Simple yellow circles represent lighting fixtures in each room.
Exterior Features:
A porch is drawn at the front with a different color to distinguish it from the main house.
Basic landscaping is added as the background color.
Saving the Image:
The final floor plan is saved as floor_plan.png in the working directory.
Running the Code:
Prerequisites:
Ensure you have Python installed on your system.
Install the pycairo library if you haven't already:
bash
pip install pycairo
Execute the Script:
Save the provided code to a file, say floor_plan.py.
Run the script using the command:
bash
python floor_plan.py
After execution, floor_plan.png will be created in the same directory, depicting the 2D floor plan.
Notes:
This script provides a simplified representation of a floor plan. For more detailed and accurate architectural drawings, more sophisticated tools and libraries may be required.
Adjust the room sizes, positions, and furniture placements as needed to better fit your specific requirements.
Enhancements such as different lighting types, more detailed furniture, or advanced styling can be incorporated with additional Cairo drawing commands.