Question
The O in I/O: writing data to a file in ppm format. Program requirements and description:: We will create an image in ppm format using
The O in I/O: writing data to a file in ppm format. Program requirements and description:: We will create an image in ppm format using the data computed from Assignment 3. The ppm file format (Figure 2) is a simple image format that contains data about a pixel color using three colors (RGB). We will restrict each color to be a value from 0 to 255. For example, we represent the colors red as 255 0 0, blue as 0 255 0, green as 0 0 255, and grey as 127 127 127. A pie chart will be created by setting pixel colors to represents the percentage of energy consumption per state. To do the assignment the following items will be required:
1. A file called A04.cpp with a main (int argc, char** argv) function.
2. Comments and formatting that follow our CS52 Programming Guide.
3. The menu-driven driven functionality from Assignment 3. If your assignment 3 is not working then hard code the seven double values to work on assignment 4 and place C++ comments in the A04.cpp stating that A3 is not working.
4. A 3D array will be used to store three color values at each cell in the array. We will call the 3D array a pixel buffer. The pixel buffer will store three unsigned char values that represent a 24-bit (3 byte) RGB color. The dimensions of the array are HEIGHT x WIDTH x DEPTH (R,G & B). There is no requirement for the array format but the suggested format is a 1D linear array. We can create arrays in a variety of ways including a stack allocated 3D array (declared using [][][]), a 1D array of length W*H*D, or a 3D dynamic array allocated with new. If a 1D array is used, a linear index will be needed to traverse the array as a multidimensional array in a nested loop. See Listing 1 for examples of declaring different types of 3D arrays.
5. To center the circle we can use the same odd width and height. The odd number allows for the center to be in the middle of the matrix. For example a 5 x 5 matrix will have the center at index location center with array indices = (2, 2)
Use a two-element int array to store the circles center. int center[2] = { WIDTH / 2, HEIGHT / 2 }; Where the center is located is not critical. We just want the circle to be inside our image dimensions (width or height). We can test if a pixels x,y coordinate pair is either in the circle or out of the circle by comparing a pixels distance from the center to the chosen pie chart radius. A function get_distance_from_center(...) is provided to compute the distance of a pixel to the center of the circle from index values x,y. If the distance is greater than the radius, set the color to backgound color (e.g. grey, white, etc.).
6. Three helper functions are provided that compute an angle and distance using the center of the image and from the x and y index values of a nested for loop: double theta = get_theta (x , y , center ); double hyp = get_distance_from_center (x , y , center , theta ); double deg = quadrant_converter (x , y , center , theta );
7. To create the pie chart write a function called void CreatePieChart(unsigned char *pixels,const int dims[3],double percentages[7], double radius); ,where the percentage for each of the seven groups is the fraction of energy computed from part 1 and dims holds width, height, and depth (3). Your pixels and other formal parameters may differ from the ones shown.
8. The size of each of the seven pie slices is controlled by the sweep angle computed from a line drawn from the center of a circle to the pixel located at the cell x,y in the 2D array and the horizontal midpoint line of the circle. 9. The function get theta will return the angle given the pixels index location (e.g. x,y, i,j, or row,col, etc.). The angle returned can be used to determine which of the seven groups the pixel is located. For example, if coal usage is 50%, then all pixels with an angle between 0 and 180 (.50 360 = 180 ) will be drawn with coals color.
10. The start of the next pie slice is a cummulative angle. For example, if coal is 50% and Natural Gas is 20% (.2 360 = 72 ) then the pixels for natural gas are in the range of angles strictly greater than 180 and less than or equal to 252 (180 + 72 ) .
11. The order of each energy groups pie slice is fixed as the order from A3. In A4, we need to first pick a color for each pie slice (coal is red, solar is green, etc.) and then assign that color to each pixel (array cell) using the angle returned from get theta. The angle theta will allow us to choose the pie slice for each pixel in the pixels array.
12. The function void WriteImage(std::ofstream &out, unsigned char *pixels, const int dims[3]); will write the pixel color data to a ppm file. The ppm format is described on the last page. The output will be in text (ASCII) format. A type conversion to an unsigned int number format may be helpful to write out an unsigned char. A useful typedef will be typedef unsigned char uchar;
PPM Image Format:
Tip: The image is stored in row-major order. This means that when viewing the image, the top left coordinates (row,col) are (0,0) and the bottom right coordinates are (height,width). For example, the 4 x 4 image below has 16 colors. Each color is represented as three numbers. The upper left at cell 0,0 is red and is the number 255 0 0 (the first three entries in the ppm file below). The last three values in the file, 255 255 0, encode yellow and are at coordinate (4,4). We are using ASCII (text) encoding identified by the P3 in the file header (P6 is binary). # are comments, Width Height, then maximum color value. Write 70 characters or less for each line in the ppm file.
Please someone help to write this programm in c++
AND THIS IS MY CODE FOR MY ASSIGNMENTS THREE
#include
#include
#include
#include
#include
#include
using namespace std;
struct Entry
{
string state;
double Coal;
double NaturalGas;
double Petroleum;
double Nuclear;
double Renewable;
double Total; double wind , solar;
};
void readData(ifstream &file, vector
int main()
{
std::ifstream file("Data.csv");
vector
readData(file,DataList,51);
string input;
while(true)
{
cout << "Enter a statename or \'q\' to exit: ";
cin >> input;
if(input == "q") break;
for(int i=0;i { Entry entry = DataList[i]; if(entry.state == input) { cout << "Coal : " << entry.Coal/ entry.Total*100.0 << "% "; cout << "NaturalGas : " << entry.NaturalGas *100.0/entry.Total << "% "; cout << "Petroleum : " << entry.Petroleum *100.0/entry.Total << "% "; cout << "Nuclear : " << entry.Nuclear *100.0/entry.Total << "% "; cout << "Renewable : " << entry.Renewable *100.0/entry.Total << "% " ; cout<<" solar :" < cout << "wind : " << entry.wind *100.0/entry.Total << "% " ; break; } } } cout << "GoodBye"; return 0; } void readData(ifstream &file, vector { std::string line = ""; int lineNum =0; while (getline(file, line)) { lineNum++; if(lineNum == 1) continue; if(lineNum-1 > NumEntry ) break; std::vector for (char *p = strtok((char*)line.c_str(), ", "); p != NULL; p = strtok(NULL, ", ")) { vec.push_back( string(p) ); } Entry entry; entry.state = vec[0]; entry.Coal = atof(vec[1].c_str()); entry.NaturalGas = atof(vec[2].c_str()); entry.Petroleum = atof(vec[3].c_str())+atof(vec[4].c_str())+atof(vec[5].c_str())+atof(vec[6].c_str())+atof(vec[7].c_str()); entry.Nuclear = atof(vec[8].c_str()); entry.Renewable = atof(vec[9].c_str())+atof(vec[10].c_str())+atof(vec[11].c_str())+atof(vec[12].c_str()); entry.solar=atof(vec[13].c_str()); entry.wind=atof(vec[14].c_str()); entry.Total = entry.Coal + entry.NaturalGas +entry.Petroleum +entry.Nuclear + entry.Renewable+entry.solar+entry.wind ; DataList.push_back(entry); } file.close(); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started