Saint Louis University |
Computer Science 145
|
Dept. of Math & Computer Science |
Topic: Flood Levels
Related Reading: notes about finding connected components in an array
Due:
Monday, 27 April 2009, 11:59pm
For this assignment, you are allowed to work with one other student if you wish. If any student wishes to have a partner but has not been able to locate one, please let the instructor know so that we can match up partners. One member of the partnership should submit the final script, making sure that all members' names are included as part of comments at the beginning of the file.
Please make sure you adhere to the policies on academic integrity in this regard.
Our goal for this assignment is to write a program that simulates the rising and falling water levels over a fixed terrain. We will represent the terrain as a two-dimensional array of numbers, representing the height of the terrain at each point. As a simple example, the array
7 2 5 3 4 1represents the terrain shown in the following figure.
We then consider what happens when a terrain is surrounded by water with a given sea level. If the water level gets strictly greater than the terrain level at the boundary, the water will flood that portion of the terrain, as well as any neighboring terrain locations that are strictly below that water level. For example, if the above terrain were surrounded by a water level of 5, we would find associated water depths of
0 3 0 2 1 4as diagrammed in the following figure.
However, it is not simply that all smaller terrain values get covered by water. As an example, consider the terrain
3 5 7 8 9 2 6 4 1 6 7 3If we give this an exterior water level of 5, there will not be any water on top of the terrain with height 2. That location is buffered both horizontally and vertically by taller structures that block out the water. In this case, the external water level of 5 leads to water levels of
2 0 0 0 0 0 0 1 4 0 0 2
The same terrain, with an external water level of 8 would produce internal water levels of
5 3 1 0 0 6 2 4 7 2 1 5
If we take that water level, but then drop the exterior water level to 2, we get internal water levels of
0 0 0 0 0 3 0 0 1 0 0 0The interesting thing to note is that the terrain spot with height 2 is covered by 3 units of water. Even though the external water has dropped, those three units of water remain as a "lake" because it is surrounded by higher ground that keeps the water from draining (note that there are only 3 units of water, because a fourth and further unit of water will drain away because it would have combined altitude of 6 with the terrain, and thus would drain past the neighboring terrain of height 5).
function water = recomputeWater(terrain, outsideLevel, water) % Recalculate water level based on new exterior level. % Terrain is a fixed array with heights of land % outsideLevel is the new height for the surrounding water % water is a previous map of water depths above the terrain. % (if water is not specified, it is assumed to be 0 everywhere) % % Result of the call is a recomputed water array that reflects % any necessary changes to the system.
function plotTerrain(terrain, water) % Creates a figure with the given terrain (and water). % % water is optional. If given, it should have same % dimensions as terrain.
function water = simulateFlood(terrain) % Computes a series of arrays designating water level % as it rises and falls accross a terrain. % % The water level will begin at height 1, and rise one % level at a time until the entire terrain is submerged, % after which it will drop one level at a time until the % exterior level goes back to zero. % % Result will be three-dimensional array where % water(:,:, k) depicts the water level after the % kth step of the process.
function movie = animateFlood(terrain, waterLevels, animationDelay, filename) % Creates an animation of varying water levels. % % terrain is a fixed two-dimensional array portraying ground height % waterLevel is a three-dimensional array of water levels per time step % (such as the result of simluateFlood function) % animationDelay is time between frames % filename, if given, will be used for writing result to AVI file
6 6 10 5 10 7 0 9 7 6 1 3 5 5 6 2 6 5 7 5 2 8 8 7 6 5 5 4 3 7 2 6 10 6 7 7 5 6 8 2 7 2 7 4 2 9 2 4 3 6 4 3 8 3Shown below is a movie produced using our animateFlood function.
We also provide more detailed frame-by-frame information for this example.