Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

using namespace std; #include #include #include struct Color { unsigned char r, g, b; // Default constructor. Pixel is black. Color() { r = 0;

using namespace std; #include #include #include struct Color { unsigned char r, g, b;

// Default constructor. Pixel is black. Color() { r = 0; g = 0; b = 0; }

// Parameter constructor. Creates color from R, G, B values. Color(unsigned char r, unsigned char g, unsigned char b) { this->r = r; this->g = g; this->b = b; } };

class PpmImage { public: // Constructor. Creates image with passed size. PpmImage(size_t width, size_t height); // Destructor. Frees object memory. virtual ~PpmImage();

// Fills entire image with given color. void fill(const Color &color); // Draws line of given color from (x1, y1) to (x2, y2). void drawLine(int x1, int y1, int x2, int y2, const Color &color);

// Needed for writing private data to stream. friend std::ostream& operator<<(std::ostream &os, const PpmImage &image); protected: private: // Image size size_t m_width; size_t m_height;

// Image pixels matrix Color **m_image; }; PpmImage::PpmImage(size_t width, size_t height) { // remember sizes m_width = width; m_height = height;

// initialize matrix m_image = new Color*[m_height];

// iterate through every row for (size_t i = 0; i < m_height; i++) { // initialize it m_image[i] = new Color[m_width]; } }

PpmImage::~PpmImage() { // iterate through every row for (size_t i = 0; i < m_height; i++) { // free its memory delete[] m_image[i]; }

// free matrix memory delete[] m_image; }

void PpmImage::fill(const Color &color) { // iterate through every pixel for (size_t i = 0; i < m_height; i++) { for (size_t j = 0; j < m_width; j++) { // set its color m_image[i][j] = color; } } }

void PpmImage::drawLine(int x1, int y1, int x2, int y2, const Color &color) { // find the x and y length int dx = abs(x1 - x2); int dy = abs(y1 - y2);

// if the x length is longer if (dx > dy) { // iterate through all x values for (int x = std::min(x1, x2); x <= std::max(x1, x2); x++) { // calculate y for this x int y = (y2 - y1)*(x - x1)/(x2 - x1) + y1; // got from (y - y1)/(y2 - y1) = (x - x1)(x2 - x1)

// set pixel's color m_image[y][x] = color; } } else { // iterate through all y values for (int y = std::min(y1, y2); y <= std::max(y1, y2); y++) { // calculate x for this y int x = (x2 - x1)*(y - y1)/(y2 - y1) + x1; // got from (y - y1)/(y2 - y1) = (x - x1)(x2 - x1) m_image[y][x] = color; // set pixel's color } } }

std::ostream& operator<<(std::ostream &os, const Color &color) { // print every component aligned os << std::setw(4) << color.r << std::setw(4) << color.g << std::setw(4) << color.b;

// return stream for chaining return os; }

std::ostream& operator<<(std::ostream &os, const PpmImage &image) { // write PPM magic number os << "P6" << std::endl; // write image size; os << image.m_width << " " << image.m_height << std::endl;

// find maximal color value unsigned char maxColorValue = 0; // initialize with lowest possible for (size_t i = 0; i < image.m_height; i++) // iterate rows { for (size_t j = 0; j < image.m_width; j++) // iterate columns { // update if any pixel's component is bigger than current maxColorValue = std::max(maxColorValue, image.m_image[i][j].r); maxColorValue = std::max(maxColorValue, image.m_image[i][j].g); maxColorValue = std::max(maxColorValue, image.m_image[i][j].b); } }

// write it os << (int)maxColorValue;

// image should be printed from top to bottom, so iterate rows in reverse order for (size_t i = image.m_height; i > 0; i--) { // write row separator os << std::endl;

// iterate columns for (size_t j = 0; j < image.m_width; j++) { // if not first column we need to write columns separator if (j) { os << " "; }

// write pixel color os << image.m_image[i - 1][j]; } }

// return stream for chaining return os; } #include #include #include

using namespace std;

int main() { // Create 320x240 image PpmImage lineImage(320, 240); // Set background pixels to light gray lineImage.fill(Color(245, 245, 245)); // Draw first line lineImage.drawLine(60, 120, 160, 120, Color(255, 0, 0)); // Draw second line lineImage.drawLine(160, 120, 160, 220, Color(0, 255, 0));

// variable to store save file name string fileName; // print prompt cout << "Enter filename to save image:" << endl; // read answer getline(cin, fileName);

// try open save file ofstream outputFile(fileName.c_str()); // if succeed if (outputFile.is_open()) { // write image to file outputFile << lineImage; // print message cout << "Image saved!" << endl; } else { // print error cout << "Can't save image to the file!" << endl; }

return 0; }

************** PROGRAM SHOULD HAVE BACKROUND 245, 245,245

LINES ABOVE 255,0,0 FROM ptT 60, 20 TO 160, 120 AND 0,255,0 LINE FROM PT 160,120 TO 160,220 BELOWthe first line - I am not getting either line - what am i doing wrong

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

Database Driven Web Sites

Authors: Mike Morrison, Joline Morrison

1st Edition

061901556X, 978-0619015565

More Books

Students also viewed these Databases questions

Question

Comment as you serve the customer.

Answered: 1 week ago

Question

What is job rotation ?

Answered: 1 week ago