Try now in the
Open Cloud »
(no sign-in required)

Random Cityscapes

Create a random arrangement of skyscrapers.

Run the code to place 30 random squares in a size 10 region. Try numbers other than 30:

SHOW/HIDE DETAILS

This draws a default rectangle, which is a square:

In[1]:=
X
Out[1]=

Add PlotRange{{0,10},{0,10}} to make the drawing area extend from 0 to 10 in both directions, and AxesTrue to turn on axes so you can see the coordinate values:

In[2]:=
X
Out[2]=

You can draw a square in a random position using RandomReal.

This gives a random number between 0 and 10. Each time you run the code, you get a different number:

In[3]:=
X
Out[3]=

This gives a pair of random numbers between 0 and 10. Each time you run the code, you get a different pair:

In[4]:=
X
Out[4]=

Use RandomReal to draw a square in a random position. Each time you run the code, the square is in a different place:

In[5]:=
X
Out[5]=

Table makes lists of things. This uses Table to make a list of 5 random numbers:

In[6]:=
X
Out[6]=

This uses Table to draw 30 squares in random positions:

In[7]:=
X
Out[7]=

Turn off the axes by removing AxesTrue, and remove PlotRange{{0,10},{0,10}} too. Without an explicit PlotRange value, the plot range is calculated automatically to be just large enough to contain the contents of the graphic:

In[8]:=
X
Out[8]=

HIDE DETAILS
In[1]:=
X
Out[1]=

Use 3D cuboids instead of 2D squares. Try numbers other than 30:

SHOW/HIDE DETAILS

This draws a square at {0,0}:

In[1]:=
X
Out[1]=

This draws a cube at {0,0,0}. The expression for drawing a cube is like the one for drawing a square, but Graphics is replaced by Graphics3D, Rectangle by Cuboid, and the two-dimensional coordinate {0,0} by the three-dimensional coordinate {0,0,0}:

In[2]:=
X
Out[2]=

You can draw cubes in random positions similar to the way you drew squares in random positions. In addition to replacing Graphics with Graphics3D and Rectangle with Cuboid, you need to replace RandomReal[10,2] with RandomReal[10,3] so that the coordinate values are three-dimensional:

In[3]:=
X
Out[3]=

To draw cubes in random positions but with the same vertical coordinate, make a two-dimensional coordinate with RandomReal[10,2] and append zero to it:

In[4]:=
X
Out[4]=

Use that code to make random cubes that all lie in the same vertical position:

In[5]:=
X
Out[5]=

HIDE DETAILS
In[1]:=
X
Out[1]=

Pick random heights between 2 and 8. Try ranges of heights other than {2,8}for example, {1,2}:

SHOW/HIDE DETAILS

This draws a box with corners at coordinates {5,5,0} and {6,6,7}:

In[1]:=
X
Out[1]=

Another way to write the same thing is to separate the position of the box{5,5,0}from the size of the box{1,1,7}. The first corner coordinate is given by the position, and the second by the position + size:

In[2]:=
X
Out[2]=

A better way to write the same thing is to use With so that if you want to change the boxs position, you only have to change the code in one place:

In[3]:=
X
Out[3]=

Now its easy to change the code to draw a height 7 box at a random position. Just replace pos={5,5,0} with pos=Append[RandomReal[10,2],0]. Run this code repeatedly to see the box move around:

In[4]:=
X
Out[4]=

Make the height vary between 2 and 8 by replacing 7 with RandomReal[{2,8}]:

In[5]:=
X
Out[5]=

Use Table to draw 30 random boxes and remove PlotRange{{0,11},{0,11},{0,11}} so that the output is just big enough to contain the boxes:

In[6]:=
X
Out[6]=

HIDE DETAILS
In[1]:=
X
Out[1]=

Use neutral lighting (and remove the box):

SHOW/HIDE DETAILS

Use BoxedFalse to draw the graphic without a box around it:

In[1]:=
X
Out[1]=

Add Lighting Neutral to get white lighting:

In[2]:=
X
Out[2]=

HIDE DETAILS
In[1]:=
X
Out[1]=

Use random gray levels between .3 and .7 for the skyscrapers. Try ranges of gray levels other than {.3,.7}for example, {0,1}:

SHOW/HIDE DETAILS

This draws a white box:

In[1]:=
X
Out[1]=

This draws a 50% gray box:

In[2]:=
X
Out[2]=

This draws a box with a random gray color between 30% and 70%. Each time you run the code, you get a different gray:

In[3]:=
X
Out[3]=

Add GrayLevel[RandomReal[{.3,.7}]] to the skyscraper code to get randomly colored buildings:

In[4]:=
X
Out[4]=

HIDE DETAILS
In[1]:=
X
Out[1]=

Add ground for the skyscrapers to stand on. Try colors other than Brownfor example, Pink or Darker[Green]:

SHOW/HIDE DETAILS

Add another Cuboid to represent the ground. The skyscrapers are positioned between 0 and 10, and they are 1 unit wide and deep, so their footprints will be anywhere between 0 to 11 in the x and y directions. Add a 1-unit margin around the edges for a range of -1 to 12.

The bottoms of the skyscrapers are at z coordinate 0. Make the ground cuboid .5 units thick by making it extend downward to -.5. That gives Cuboid[{{-1,-1,-.5},{12,12,0}}] for the ground:

In[1]:=
X
Out[1]=

Color the ground Brown:

In[2]:=
X
Out[2]=

HIDE DETAILS
In[1]:=
X
Out[1]=

Interactively fill in skyscrapers. Drag the slider to fill in the landscape:

SHOW/HIDE DETAILS

This is the code to make a landscape with 30 skyscrapers:

In[1]:=
X
Out[1]=

You can make the landscape interactive with Manipulate.

Manipulate attaches interactive controls to things like this expression for 3 squared:

In[2]:=
X
Out[2]=

Make that expression interactive by wrapping it with Manipulate[...], replacing the 3 with n, and specifying that n goes from 0 to 30 in steps of 1. Drag the slider to see the squares of various numbers:

In[3]:=
X
Out[3]=

You can do the same thing with the skyscraper expression to make it interactive. Wrap it with Manipulate[...], replace the 30 with n, and specify that n goes from 0 to 30 in steps of 1. Drag the slider to see the number of buildings change:

In[4]:=
X
Out[4]=

The buildings jump around because RandomReal gives different numbers each time it is used. You can make them stay put by adding SeedRandom[12345] to the code:

In[5]:=
X
Out[5]=

Heres why SeedRandom[12345] keeps the buildings from jumping around.

RandomReal gives a different number each time it is used, so each time a new landscape is drawn, the buildings are in different positions.

SeedRandom[12345] resets the random number generator so that subsequent uses of RandomReal give the same sequence of random numbers.

This list of random numbers changes each time you run the code:

In[6]:=
X
Out[6]=

This list of random numbers is always the same because the random number generator is reset with SeedRandom:

In[7]:=
X
Out[7]=

You can give different numbers to SeedRandom to reset the random number generator to different places.

HIDE DETAILS
In[1]:=
X
Out[1]=

Share ItMake an interactive cityscapes website:

SHOW/HIDE DETAILS

Deploy the Manipulate to the Wolfram Cloud where anyone with a browser can use it:

In[1]:=
X
Out[1]=

Click the link in the output to visit the site.

Share the link by right-clicking it and choosing Copy Address. Paste the link into an email, tweet, or other message.

HIDE DETAILS
In[1]:=
X
Out[1]=