Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Mountain Paths (Part 1) C++ code Objectives 2d vectors Store Use Nested Loops Parallel data structures (i.e. parallel vectors) Transform data Read from files Write

Mountain Paths (Part 1) C++ code

Objectives

2d vectors

Store

Use

Nested Loops

Parallel data structures (i.e. parallel vectors)

Transform data

Read from files

Write to files

Program

The minimum requirements for this homework are that you write code to read elevation data and produce a file representing an image that, when visualized, displays each position on the map with a color that represents the positions elevation.

Program Flow

Read the data into a 2D vector

Find min and max elevation to correspond to darkest and brightest color, respectively

Compute the shade of gray for each cell in the map

Produce the output file in the specified format (PPM)

Use an online free tool to convert your PPM file into a JPG file

Step 1 - Read the data into a 2D vector

Your first goal is to read the values into a 2D vector (i.e., a matrix) of ints from a data file. Note: You should use vector_name.at(index) to access the elements of the vector to benefit from the additional runtime checks.

This data file contains, in plain text, data representing the average elevations of patches of land (roughly 700x700 meters) in the US. The user of your program will provide as input three values:

The number of rows in the map; (The height of the image to be produced.)

The number of columns in the map; (The width of the image to be produced.)

The name of the file containing the map data.

After reading these data items from the user, you should read the elevation data from the specified file. We provide several sample data files containing elevation data for most of the state of Colorado (mountains!). Other input files can be obtained from the National Oceanic and Atmospheric Administration (http://maps.ngdc.noaa.gov/viewers/wcs-client/).

A data file comes as one large, space-separated list of integers. A file representing a 480-row by 844-col grid contains 405,120 integers (480*844). Each integer is the average elevation in meters of each cell in the grid. The data is listed in row-major order, i.e., first the 844 numbers for row 0, then the 844 numbers for row 1, etc.

Hints/Warnings:

The data is given as a continuous stream of numbers - there may be no line breaks in the file.

You should validate the input from the user (number of rows and columns), and you should also validate that the input file exists and that it contains all the data for the expected numbers of rows and columns.

It is ok to exit the program execution with an error message if there is an input error, i.e., you do not need to give the user a chance to enter corrected data.

Start all error messages with Error: (note the colon).

You may want to make sure that you are reading the data correctly before you move to the next step of this project. Implement a function to print your map data and try out your code with a file containing a small map first (you can create an input file corresponding to 4 rows and 4 columns, for example.) Then check what happens when you read the sample map data files: (100 by 100, 480 by 480, 844 by 480).

Step 2 - Find the min and max values

In order to draw the map with a gray scale that indicates the elevation, you will need to determine which scale to use, i.e., which will be shown as a dark area (with low elevation) and which ones will be shown as bright areas (high elevation). This means you need to find the min and max values in the map first, so that you know which values to associate with the brightest and darkest colors. You can compute the min and max elevation values by writing code that scans the entire map data and keeps track of the smallest and largest values found so far.

Test this functionality to make sure youre getting the correct values for min and max, before you proceed to implement the rest of the program. You may want to check with a friend or colleagues in the Piazza forum to see if other students are getting the same min and max values for the provided input files.

Step 3 - Compute the color for each part of the map and store

The input data file contains the elevation value for each cell in the map. Now you need to compute the color (in a gray scale between white and black) to use to represent these evaluation values. The shade of gray should be scaled to the elevation of the map.

Traditionally, images are represented and displayed in electronic systems (such as TVs and computers) through the RGB color model, an additive color model in which red, green, and blue light are added together in various ways to reproduce a broad array of colors. In this model, colors are represented through three integers (R, G, and B) values between 0 and 255. For example, (0, 0, 255) represents blue and (255, 255, 0) represents yellow. In RGB color, if each of the three RGB values are the same, we get a shade of gray. Thus, there are 256 possible shades of gray from black (0,0,0) to middle gray (128,128,128), to white (255,255,255).

To make the shade of gray, you should use the min and max values in the 2D vector to scale each integer (elevation data) to a value between 0 and 255 inclusive. This can be done with the following equation:

color =(elevation - min elevation)(max elevation - min elevation) * 255

Check your math to ensure that you are scaling correctly. Check your code to make sure that your arithmetic operations are working as you want. Recall that if a and b are variables declared as integers, the expression a/b will be 0 if a==128 and b==256.

As you compute the shade of grey, store that value in three parallel vectors for R, G and B. Putting the same value for R, G and B will result in grey. The structure of the vector should mirror the vector with the elevation data. This structure is required in the next part of the homework. If you use an alternative structure, then you will have to redo that part in the next homework. See section 11.5 in the zyBook for more information about parallel vectors. Unfortunately, the zyBook refers to parallel vectors with a more ambiguous term, multiple vectors.

Step 4 - Produce the output file in the PPM format

PPM (portable pixel map) format is a specification for representing images using the RGB color model. PPM is not used widely because it is very inefficient (for example, it does not apply any data compression to reduce the space required to represent an image.) But PPM is very simple, and there are programs available for Windows, Mac, and Linux that can be used to view ppm images. Even more conveniently, you can use an online tool with your browser to convert a PPM file into a widely used format such as JPG. We will be using Plain PPM, which has the information in text format (readable as decimal numbers instead of binary.)

In Step 3, you computed the RGB values for the shades of gray. All you will need to do to get a PPM image for these RGB values is to write a preamble before writing the RGB numbers into a file. We will follow the convention that if the input file is named input.dat, then your program will generate a file namedinput.dat.ppm

A PPM file has the following format:

First line: string P3

Second line: width (number of columns) and height (number of rows)

Third line: max color value (for us, 255)

Rest of the file: list of RGB values for the image, expressed as a raster of rows, from top to bottom. Each row contains the RGB values (i.e., three values) for each column.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

JDBC Database Programming With J2ee

Authors: Art Taylor

1st Edition

0130453234, 978-0130453235

More Books

Students also viewed these Databases questions

Question

Organize and support your main points

Answered: 1 week ago

Question

Move smoothly from point to point

Answered: 1 week ago

Question

Outlining Your Speech?

Answered: 1 week ago