Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <string> #include <iostream> #include <fstream> #include <cmath> using

ID: 2247501 • Letter: #

Question

#include <string>
#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;


struct point {
int x, y;

// constructor
point(int _x = 0, int _y = 0){
x = _x;
y = _y;
}
};

// struct for rgb-pixel, holds red, green and blue values
struct rgb {
int r, g, b;

// constructor
rgb(int _r = 0, int _g = 0, int _b = 0){
r = _r;
g = _g;
b = _b;
}
};

class PPMImage {
public:

PPMImage(int width, int height){
w = width;
h = height;
// create pointer to pointers
pixmap = new rgb*[h];
// initiliaze each pointer
for (int i = 0; i < h; ++i)
pixmap[i] = new rgb[w];
}

// copy constructor something I learn, copies data from another PPMImage object
// arguments:
// other - another image
PPMImage(PPMImage &other) {
copy(other);
}

// assignment operator, clears image and copies data from another one
// arguemnts:
// other another image
const PPMImage& operator=(PPMImage &other) {
clear();
copy(other);

// return reference to current image something I learned
return *this;
}

// destructor, clears data
~PPMImage() {
clear();
}

// fiils image with one colour
// arguments:
// col - fill colour
void fill(rgb col) {
// loop and assign put col in each pixel
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
pixmap[i][j] = col;
}
}
}

// draws circle
// arguments:
// c - center point
// rx - x radius
// ry - y radius
// col - line colour
void draw_circle(point c, int r, rgb col) {
// current `x` and `y`
int x = 0, y = -r;
// previous `x` and `y`
int _x = x, _y = y;
// compute r^2 only once
int rr = r * r;

while (_y < r) {
// compute next `x`
y = _y + 1, x = round(sqrt(rr-y*y));

// move `x` and `y` by center coordinates
x += c.x;
y += c.y;
_x += c.x;
_y += c.y;

// draw compute `x`
pixmap[y][x] = col;

// draw reflected `x`
pixmap[y][2 * c.x - x] = col;

// connect previous points with current ones
draw_line(point(_x, _y), point(x, y), col);
draw_line(point(2 * c.x - x, y), point(2 * c.x - _x, _y), col);

// move previous
_x = x - c.x, _y = y - c.y;
}
}

// draws line between two points
// e.g. line between (1,1) and (8,4)
// arguments:
// p1 - first point
// p2 - second point
// col - line color
void draw_line(point p1, point p2, rgb col) {
// swap points so p1.x if lesser than p2.x
if (p1.x > p2.x) {
point temp = p1;
p1 = p2;
p2 = temp;
}

// loop increment value
int dy = p1.y < p2.y ? 1 : -1, **********************PLEASE EXPLAIN P2.Y ? 1: -1 WHAT IS THIS DOING
// length of the each subline
len = (p2.x - p1.x + 1) / (abs(p1.y - p2.y) + 1);

// draw each subline
while (p1.y != p2.y) {
for (int i = 0; i < len; ++i) {
pixmap[p1.y][p1.x++] = col;
}

p1.y += dy;
}

// fill remaining pixels
while (p1.x < p2.x) {
pixmap[p2.y][p1.x++] = col;
}
}

// prints image to file
bool print_to_file(string filename) {
ofstream out(filename.c_str());
// couldn't open file
if (!out){
return false;
}


out << "P3" << endl;
out << w << " " << h << endl;
out << 255 << endl;

// fill each pixel
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
rgb &col = pixmap[i][j];
out << col.r << " " << col.g << " " << col.b << endl;
}
}

return true;
}
// image width and height
int w, h;
// image pixels // I know you said not use double pointers
rgb **pixmap;

// clears image content
void clear() {
for (int i = 0; i < h; ++i)
delete[] pixmap[i];

w = 0, h = 0;
delete[] pixmap;
}

// copies data from another image
void copy(PPMImage &other) {
w = other.w, h = other.h;
pixmap = new rgb*[h];
for (int i = 0; i < h; ++i) {
pixmap[i] = new rgb[w];
for (int j = 0; j < w; ++j)
pixmap[i][j] = other.pixmap[i][j];

}
}
};

// some constants
int WIDTH = 320,
HEIGHT = 240,
R = 100;

// circle center point
point CENTER = point(160, 120);
// colors for background and line
rgb LIGHT_GRAY = rgb(245, 245, 245),
BLACK = rgb(0, 0, 255);

int main() {
// create image
PPMImage img(WIDTH, HEIGHT);
// fill background
img.fill(LIGHT_GRAY);
// draw a black circle
img.draw_circle(CENTER, R, BLACK);
// print image
img.print_to_file("img.ppm");

return 0;
}

**** PLEASE EXPLAIN LINE 133 MARKED ABOVE THANKS C++

Explanation / Answer

Hi,

Below is the explanation of marked line-

int dy = p1.y < p2.y ? 1 : -1,

This statement is an example of C++ Shorthands. It is equivalent to if..else statement but only thing is that its structure is different.

In the above statement, the condition p1.y<p2.y is evaluated. If the condition is success, then the code which is written after "?" gets executed. Otherwise the code which is written after the ":" colon.

In this case if p1.y <p2.y, the output 1 will get generated and gets stored into dy else -1 gets generated and stored.

Regards

Vinay Singh