Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#ifndef CANVAS_H #define CANVAS_H /************************** DO NOT MODIFY THIS FILE **************************/ class Canvas { // Represents a fixed size 2D grid of pixels which can

#ifndef CANVAS_H

#define CANVAS_H

/************************** DO NOT MODIFY THIS FILE **************************/

class Canvas {

// Represents a fixed size 2D grid of "pixels" which can be set to either

// "on" or "off" and can print itself to the terminal for viewing.

// NOTE: The origin (0,0) for the Canvas is at the bottom left.

public:

static const int WIDTH = 30;

static const int HEIGHT = 30;

static const char PIXEL_ON = '#';

static const char PIXEL_OFF = ' ';

//EFFECTS: creates a new Canvas with size WIDTH x HEIGHT

Canvas();

//REQUIRES: the pixel is on the canvas (0 <= x < WIDTH and 0 <= y < HEIGHT)

//MODIFIES: grid

//EFFECTS: the pixel at (x,y) is set to ON if value, and OFF otherwise.

void set_pixel(int x, int y, bool value);

//EFFECTS: prints this canvas to cout

void print() const;

private:

// Stores whether each pixel is ON or OFF

bool grid[HEIGHT][WIDTH];

};

#endif /* CANVAS_H */

#include "Canvas.h"

#include

#include

using namespace std;

/************************** DO NOT MODIFY THIS FILE **************************/

//EFFECTS: creates a new Canvas with size WIDTH x HEIGHT

Canvas::Canvas(){

for (int r = 0; r < HEIGHT; ++r) {

for (int c = 0; c < WIDTH; ++c) {

grid[r][c] = false;

}

}

}

//REQUIRES: the pixel is on the canvas (0 <= x < WIDTH and 0 <= y < HEIGHT)

//MODIFIES: grid

//EFFECTS: the pixel at (x,y) is set to ON if value, and OFF otherwise.

void Canvas::set_pixel(int x, int y, bool value){

assert(0 <= x && x < WIDTH);

assert(0 <= y && y < HEIGHT);

// y: row, x: column

grid[y][x] = value;

}

//EFFECTS: prints this canvas to cout

void Canvas::print() const {

// Start from top and move down

for (int r = HEIGHT - 1; r >= 0; --r) {

for (int c = 0; c < WIDTH; ++c) {

cout << (grid[r][c] ? PIXEL_ON : PIXEL_OFF) << " ";

}

cout << endl;

}

}

#ifndef SHAPES_H

#define SHAPES_H

#include "Canvas.h"

////////////////////////// vvvvv DO NOT CHANGE vvvvv //////////////////////////

/*********************************** SHAPE ***********************************/

class Shape {

public:

//EFFECTS: creates a Shape with initial position (0,0)

Shape() : x_pos(0), y_pos(0) { }

//EFFECTS: returns the area of this Shape

virtual double area() const = 0;

//MODIFIES: canvas

//EFFECTS: draws this shape onto canvas at its current position

virtual void draw(Canvas *canvas) const = 0;

//MODIFIES: x_pos, y_pos

//EFFECTS: sets the position of this shape

void set_position(double x_pos_in, double y_pos_in) {

x_pos = x_pos_in;

y_pos = y_pos_in;

}

double get_x_pos() const { return x_pos; }

double get_y_pos() const { return y_pos; }

private:

// Represents the position of this shape

double x_pos, y_pos;

};

/********************************** ELLIPSE **********************************/

class Ellipse : public Shape {

public:

//REQUIRES: x_rad_in, y_rad_in are non-negative

//EFFECTS: creates an Ellipse with given x and y radii at position (0,0)

Ellipse(double x_rad_in, double y_rad_in);

//EFFECTS: returns the area of this Ellipse

virtual double area() const override;

//MODIFIES: canvas

//EFFECTS: draws this shape onto canvas

virtual void draw(Canvas *canvas) const override;

private:

// Radii of the ellipse

double x_rad, y_rad;

};

////////////////////////// ^^^^^ DO NOT CHANGE ^^^^^ //////////////////////////

/********************************** CIRCLE ***********************************/

// TASK 1 - CIRCLE DECLARATION HERE

/********************************* RECTANGLE *********************************/

// TASK 2 - RECTANGLE DECLARATION HERE

#endif /* SHAPES_H */

#include "shapes.h" #include #include

using namespace std;

const double PI = 3.14159;

////////////////////////// vvvvv DO NOT CHANGE vvvvv ////////////////////////// /********************************** ELLIPSE **********************************/

Ellipse::Ellipse(double x_rad_in, double y_rad_in) : x_rad(x_rad_in), y_rad(y_rad_in) { }

double Ellipse::area() const { return PI * x_rad * y_rad; }

void Ellipse::draw(Canvas *canvas) const{ // Iterate over every canvas "pixel" for (int x = 0; x < Canvas::WIDTH; ++x){ for (int y = 0; y < Canvas::HEIGHT; ++y){

double x_diff = x - get_x_pos(); double y_diff = y - get_y_pos();

// If the ellipse contains the (x,y), such that // // (x - x_pos)^2 (y - y_pos)^2 // (---------) + (---------) <= 1 // ( x_rad ) ( y_rad ) // // then set the canvas pixel to ON if (pow((x_diff/x_rad),2) + pow((y_diff/y_rad),2) <= 1) { canvas->set_pixel(x, y, true); } } } }

////////////////////////// ^^^^^ DO NOT CHANGE ^^^^^ //////////////////////////

/********************************** CIRCLE ***********************************/

// TASK 1 - CIRCLE IMPLEMENTATION HERE

/********************************* RECTANGLE *********************************/

// TASK 2 - RECTANGLE IMPLEMENTATION HERE

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

Readings In Database Systems

Authors: Michael Stonebraker

2nd Edition

0934613656, 9780934613651

More Books

Students also viewed these Databases questions

Question

2 What supply is and what affects it.

Answered: 1 week ago

Question

3 How supply and demand together determine market equilibrium.

Answered: 1 week ago