 |
CLIP Score: 24.62
from AIDL import *
def city_skyline(
num_buildings: int = 7,
base_height: float = 2*inches,
height_variance: float = 3*inches,
building_width: float = 1*inches,
width_variance: float = 0.5*inches
):
"""
Generates a simple city skyline with a variety of building types.
Buildings will be randomly varied in height and width, and positioned side-by-side.
"""
from random import uniform, choice
skyline = Assembly()
# Define different building shapes
def create_rectangular_building(height, width):
building = Solid()
building.body = Rectangle((0,0), width, height)
return building
def create_triangular_building(height, width):
building = Solid()
building.body = Triangle((0,0), width, height)
return building
def create_circular_building(height, width):
radius = width / 2
building = Solid()
building.body = Circle((0, height - radius), radius)
return building
# Generate the buildings and position them side-by-side
current_x = 0
building_types = [create_rectangular_building, create_triangular_building, create_circular_building]
for i in range(num_buildings):
height = base_height + uniform(0, height_variance)
width = building_width + uniform(-width_variance, width_variance)
building_type = choice(building_types)
building_structure = building_type(height, width)
# Set the shift to position buildings side by side
building_structure.shift = (current_x, 0)
current_x += width
# Link the building to the skyline structure
setattr(skyline, f'building_{i}', building_structure)
return skyline
with AIDL_2D:
model = city_skyline()
print(model.Solve())
model.Render()
|
Please write an AIDL program to generate a city skyline
Creating a city skyline model with various buildings of different shapes and sizes can be a fun and visually interesting project. Let's outline the steps for generating this model using AIDL.
### Steps
1. **Create Structure for the skyline**
2. **Generate a few unique building Solid structures**: Rectangular and perhaps a triangular or circular building.
3. **Create instances of these buildings with different dimensions and arrange them in the skyline**
4. **Add holes for windows and doors**
5. **Position the buildings relative to each other in an aesthetically pleasing way**
Let's dive into coding this:
```python
from AIDL import *
def city_skyline(
num_buildings: int = 7,
base_height: float = 2*inches,
height_variance: float = 3*inches,
building_width: float = 1*inches,
width_variance: float = 0.5*inches
):
"""
Generates a simple city skyline with a variety of building types.
Buildings will be randomly varied in height and width, and positioned side-by-side.
"""
from random import uniform, choice
skyline = Assembly()
# Define different building shapes
def create_rectangular_building(height, width):
building = Solid()
building.body = Rectangle((0,0), width, height)
return building
def create_triangular_building(height, width):
building = Solid()
building.body = Triangle((0,0), width, height)
return building
def create_circular_building(height, width):
radius = width / 2
building = Solid()
building.body = Circle((0, height - radius), radius)
return building
# Generate the buildings and position them side-by-side
current_x = 0
building_types = [create_rectangular_building, create_triangular_building, create_circular_building]
for i in range(num_buildings):
height = base_height + uniform(0, height_variance)
width = building_width + uniform(-width_variance, width_variance)
building_type = choice(building_types)
building_structure = building_type(height, width)
skyline[f'building_{i}'] = building_structure
building_structure.shift = (current_x, 0)
current_x += width
return skyline
with AIDL_2D:
model = city_skyline()
print(model.Solve())
model.Render()
```
### Explanation:
1. **Imports and Function Definition**:
- Start by importing the AIDL library.
- Define the `city_skyline` function with parameters for the number of buildings, their base height, height variance, their width, and width variance.
2. **Building Geometry and Types**:
- Define three types of buildings: rectangular, triangular, and circular, each with their own function to create the geometries as Solid structures.
3. **Generate and Position Buildings**:
- Use a loop to create the specified number of buildings. Heights and widths are varied using the `uniform` function for randomness within specified ranges.
- Randomly select a building shape for each building.
- Position each building next to the previous one by updating `current_x`.
4. **Assembly Structure**:
- `skyline` is an Assembly structure that holds all the buildings positioned side-by-side.
- Attach each building to the `skyline` assembly.
5. **Render the Model**:
- Define the AIDL_2D context, create the model, solve, and render it using the provided snippet.
Feel free to customize parameters and add more details (e.g., windows, doors) as needed. This model gives a simple but effective skyline representation. Let me know if you need any changes or additional features!
Traceback (most recent call last):
File "/net/projects/ranalab/jz/aidl/AIDL/generation/backend.py", line 21, in dumbrun
exec(code, env)
File "", line 51, in
File "", line 44, in city_skyline
TypeError: 'Assembly' object does not support item assignment
It looks like I made an error in assigning buildings to the `skyline` Assembly. Assemblies do not support item assignment as I tried to use. Instead, buildings should be added as children of the Assembly by setting their `shift` property directly.
Let's correct that.
### Updated Code:
```python
from AIDL import *
def city_skyline(
num_buildings: int = 7,
base_height: float = 2*inches,
height_variance: float = 3*inches,
building_width: float = 1*inches,
width_variance: float = 0.5*inches
):
"""
Generates a simple city skyline with a variety of building types.
Buildings will be randomly varied in height and width, and positioned side-by-side.
"""
from random import uniform, choice
skyline = Assembly()
# Define different building shapes
def create_rectangular_building(height, width):
building = Solid()
building.body = Rectangle((0,0), width, height)
return building
def create_triangular_building(height, width):
building = Solid()
building.body = Triangle((0,0), width, height)
return building
def create_circular_building(height, width):
radius = width / 2
building = Solid()
building.body = Circle((0, height - radius), radius)
return building
# Generate the buildings and position them side-by-side
current_x = 0
building_types = [create_rectangular_building, create_triangular_building, create_circular_building]
for i in range(num_buildings):
height = base_height + uniform(0, height_variance)
width = building_width + uniform(-width_variance, width_variance)
building_type = choice(building_types)
building_structure = building_type(height, width)
# Set the shift to position buildings side by side
building_structure.shift = (current_x, 0)
current_x += width
# Link the building to the skyline structure
setattr(skyline, f'building_{i}', building_structure)
return skyline
with AIDL_2D:
model = city_skyline()
print(model.Solve())
model.Render()
```
In this updated code:
1. **Fix the Assignment**:
- Replace the `skyline[f'building_{i}'] = building_structure` line with `setattr(skyline, f'building_{i}', building_structure)`, which correctly assigns the buildings as children of the `skyline` Assembly.
2. **Set Shifts Properly**:
- Each building's `shift` property is set directly to position them side-by-side in the skyline.
Now this code should correctly generate the city skyline with a variety of building shapes. Let me know if there are any other features or modifications you would like to add!
|