import rhinoscriptsyntax as rs import random as ran # Delete all objects and start fresh allobjs = rs.AllObjects() rs.DeleteObjects(allobjs) ############################################################## # Domino Variables # ############################################################## A = 5 # A = Module size (distance between columns) B = A / 3 # B = Distance from columns to edge of plate thick = 0.18 # Thickness of all slabs hgt = 5.0 max_levels = 30 max_buildings = 18 f_height = 0.5 # Foundation height f_size = 0.8 # Foundation edge size min_gap = 5.0 # Minimum gap between buildings to avoid overlap ############################################################## # Derived Values # ############################################################## center_pt = [A * (2 - 1) / 2, A * (3 - 1) / 2, f_height] # Insertion point of floor plate p_width = A * (2 - 1) + 2 * B # Width of floor plate (x) p_length = A * (3 - 1) + f_size # Length of floor plate (y) ############################################################## # Functions to create components # ############################################################## def make_box(insertion=[0, 0, 0], xsize=10, ysize=10, zsize=10): # Create a box corners = [[0, 0, 0], [xsize, 0, 0], [xsize, ysize, 0], [0, ysize, 0], [0, 0, zsize], [xsize, 0, zsize], [xsize, ysize, zsize], [0, ysize, zsize]] box = rs.AddBox(corners) rs.MoveObject(box, (-xsize / 2, -ysize / 2, 0)) rs.MoveObject(box, insertion) return box def make_foundations(A=5.0, f_size=0.8, f_height=0.5, xcol=2, ycol=3): # Creates a grid of foundation blocks fns = [] for i in range(xcol): for j in range(ycol): fns.append(make_box([i * A, j * A, 0], f_size, f_size, f_height)) return fns def make_columns(A=5.0, level=0.7, thick=0.2, hgt=7.0, xcol=2, ycol=3): # Creates a grid of columns cls = [] for i in range(xcol): for j in range(ycol): cls.append(make_box([i * A, j * A, level], thick, thick, hgt)) return cls def make_checkerboard_facade(xcol=2, ycol=3, A=5.0, hgt=7.0, thick=0.18, levels=5): # Creates a colorful checkerboard facade pattern facade_panels = [] colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255), (0, 255, 255)] panel_size = A / 4 gap = 0.2 for i in range(levels): # Position of the facade at the current level, aligned with the top of each floor plate z = i * (hgt + thick) + thick # Adjusted z-position to match plate height for j in range(int(ycol * A / panel_size)): color_index = (i + j) % len(colors) panel1 = make_box([-A / 2, j * (panel_size + gap), z], thick, panel_size, hgt) rs.ObjectColor(panel1, colors[color_index]) panel2 = make_box([A * (xcol - 1) + A / 2, j * (panel_size + gap), z], thick, panel_size, hgt) rs.ObjectColor(panel2, colors[(color_index + 1) % len(colors)]) facade_panels.extend([panel1, panel2]) for k in range(int(xcol * A / panel_size)): color_index = (i + k) % len(colors) panel3 = make_box([k * (panel_size + gap), -A / 2, z], panel_size, thick, hgt) rs.ObjectColor(panel3, colors[color_index]) panel4 = make_box([k * (panel_size + gap), A * (ycol - 1) + A / 2, z], panel_size, thick, hgt) rs.ObjectColor(panel4, colors[(color_index + 2) % len(colors)]) facade_panels.extend([panel3, panel4]) return facade_panels def make_domino(A=A, B=B, thick=thick, hgt=hgt, levels=None, xcol=None, ycol=None): # Builds a randomized Domino structure with a colorful facade levels = levels if levels else ran.randint(10, max_levels) # Increased minimum height (was 5) xcol = xcol if xcol else ran.randint(2, 5) ycol = ycol if ycol else ran.randint(2, 5) # Derived values for plate and insertion points center_pt = [A * (xcol - 1) / 2, A * (ycol - 1) / 2, f_height] p_width = A * (xcol - 1) + 2 * B p_length = A * (ycol - 1) + f_size # Building components f_list = make_foundations(A, f_size, f_height, xcol, ycol) c_list, p_list, stair_l = [], [], [] for i in range(levels): center_pt[2] = f_height + i * (thick + hgt) level = f_height + thick + (i - 1) * (hgt + thick) c_list.extend(make_columns(A, level, thick, hgt, xcol, ycol)) p_list.append(make_box(center_pt, p_width, p_length, thick)) # Create checkerboard facade facada_panels=[] facade_panels = make_checkerboard_facade(xcol, ycol, A, hgt, thick, levels) return f_list, c_list, p_list, facade_panels make_domino() def check_collision(new_bbox, existing_bboxes): # Optimized collision check for bounding boxes for bbox in existing_bboxes: if (new_bbox[0][0] < bbox[1][0] + min_gap and new_bbox[1][0] > bbox[0][0] - min_gap and new_bbox[0][1] < bbox[1][1] + min_gap and new_bbox[1][1] > bbox[0][1] - min_gap): return True return False def create_city(num_buildings=max_buildings): # Generates multiple randomized Domino buildings with collision checks all_buildings = [] existing_bboxes = [] # Track bounding boxes of placed buildings placed_buildings = 0 for _ in range(num_buildings): levels = ran.randint(10, max_levels) f_list, c_list, p_list, facade_panels = make_domino(levels=levels) building_components = f_list + c_list + p_list + facade_panels # Calculate bounding box dimensions bbox = rs.BoundingBox(building_components) if not bbox: continue bbox_size = [(bbox[0][0], bbox[0][1]), (bbox[6][0], bbox[6][1])] # 2D min/max points # Attempt to place without overlap (max 20 attempts) placed = False attempts = 0 while attempts < 20 and not placed: x_offset = ran.randint(-200, 200) # Reduce range for faster placement y_offset = ran.randint(-200, 200) # Move bounding box to new position new_bbox = [(pt[0] + x_offset, pt[1] + y_offset) for pt in bbox_size] if not check_collision(new_bbox, existing_bboxes): # No collision detected, place the building rs.MoveObjects(building_components, [x_offset, y_offset, 0]) rotation_angle = ran.uniform(0, 360) rs.RotateObjects(building_components, [x_offset, y_offset, 0], rotation_angle) # Add the bounding box to the list existing_bboxes.append(new_bbox) all_buildings.extend(building_components) placed = True placed_buildings += 1 attempts += 1 # If the building couldn't be placed, print a message if not placed: print("Building {} could not be placed after 20 attempts.".format(placed_buildings + 1)) return all_buildings # Create the city with random buildings #create_city()