diff --git a/Lab5/Lab5 b/Lab5/Lab5 index 02568ca..055a42b 100755 Binary files a/Lab5/Lab5 and b/Lab5/Lab5 differ diff --git a/Lab5/boat.txt b/Lab5/boat.txt new file mode 100644 index 0000000..67970dc --- /dev/null +++ b/Lab5/boat.txt @@ -0,0 +1,106 @@ +Line +Color: ffff00 +P1: 250 500 0 +P2: 550 500 0 + +Line +Color: ffff00 +P1: 250 500 0 +P2: 150 400 0 + +Line +Color: ffff00 +P1: 150 400 0 +P2: 650 400 0 + +Line +Color: ffff00 +P1: 650 400 0 +P2: 550 500 0 + +Line +Color: ffff00 +P1: 400 400 0 +P2: 400 100 0 + +Triangle +Color: ffff00 +P1: 400 100 0 +P2: 300 150 0 +P3: 400 200 0 + +Line +Color: 0000ff +P1: 0 550 0 +P2: 100 450 0 + +Line +Color: 0000ff +P1: 100 450 0 +P2: 100 550 0 + +Line +Color: 0000ff +P1: 100 550 0 +P2: 200 450 0 + +Line +Color: 0000ff +P1: 200 450 0 +P2: 200 550 0 + +Line +Color: 0000ff +P1: 200 550 0 +P2: 300 450 0 + +Line +Color: 0000ff +P1: 300 450 0 +P2: 300 550 0 + +Line +Color: 0000ff +P1: 300 550 0 +P2: 400 450 0 + +Line +Color: 0000ff +P1: 400 450 0 +P2: 400 550 0 + +Line +Color: 0000ff +P1: 400 550 0 +P2: 500 450 0 + +Line +Color: 0000ff +P1: 500 450 0 +P2: 500 550 0 + +Line +Color: 0000ff +P1: 500 550 0 +P2: 600 450 0 + +Line +Color: 0000ff +P1: 600 450 0 +P2: 600 550 0 + +Line +Color: 0000ff +P1: 600 550 0 +P2: 700 450 0 + +Line +Color: 0000ff +P1: 700 450 0 +P2: 700 550 0 + +Line +Color: 0000ff +P1: 700 550 0 +P2: 800 450 0 + diff --git a/Lab5/image.cpp b/Lab5/image.cpp index cf404c5..12dfb5b 100644 --- a/Lab5/image.cpp +++ b/Lab5/image.cpp @@ -10,21 +10,19 @@ */ #include "image.h" +#include +#include - -// TODO Image::Image(){ shapes.clear(); } -// TODO Image::Image(const Image& from){ for(int i = 0; i < from.shapes.size(); i++){ shapes.push_back(from.shapes[i]->clone()); } } -// TODO Image::~Image(){ for(int i = 0; i < shapes.size(); i++){ delete shapes[i]; @@ -32,31 +30,29 @@ Image::~Image(){ shapes.clear(); } -// TODO Image &Image::operator=(const Image& rhs){ - for(int i = 0; i < shapes.size(); i++){ - delete shapes[i]; - } - shapes.clear(); - for(int i = 0; i < rhs.shapes.size(); i++){ - shapes.push_back(rhs.shapes[i]->clone()); + if(this != &rhs){ + for(int i = 0; i < shapes.size(); i++){ + delete shapes[i]; + } + shapes.clear(); + for(int i = 0; i < rhs.shapes.size(); i++){ + shapes.push_back(rhs.shapes[i]->clone()); + } } return *this; } -// TODO void Image::add(Shape* shape){ - shapes.push_back(shape); + shapes.push_back(shape->clone()); } -// TODO void Image::draw(GraphicsContext* gc){ for(int i = 0; i < shapes.size(); i++){ shapes[i]->draw(gc); } } -// TODO void Image::out(std::ostream& os){ for(int i = 0; i < shapes.size(); i++){ shapes[i]->out(os); @@ -64,12 +60,102 @@ void Image::out(std::ostream& os){ } } -// TODO void Image::in(std::istream& is){ - + string line; + string token; + int shapeCoords[3][3]; + getline(is, line); + istringstream lineStream(line); + do{ + lineStream >> token; + switch(token[0]){ + case 'L': + { + // Next Line + getline(is, line); + lineStream.str(line); + lineStream.clear(); + // Iterate past "Color:" + lineStream >> token; + // Get actual color code + lineStream >> token; + stringstream hexColor(token); + // Convert hex code to unsigned int + uint32_t color; + hexColor >> hex >> color; + // hexColor >> color; + // Points + for(int i = 0; i < 2; i++){ + getline(is, line); + lineStream.str(line); + lineStream.clear(); + // Iterate past "P#:" + lineStream >> token; + // Get each coord + for(int j = 0; j < 3; j++){ + lineStream >> token; + shapeCoords[i][j] = stoi(token); + } + } + shapes.push_back( + new Line(shapeCoords[0][0],shapeCoords[0][1], + shapeCoords[1][0],shapeCoords[1][1],color) + ); + break; + } + case 'T': + { + // Next Line + getline(is, line); + lineStream.str(line); + lineStream.clear(); + // Iterate past "Color:" + lineStream >> token; + // Get actual color code + lineStream >> token; + stringstream hexColor(token); + // Convert hex code to unsigned int + uint32_t color; + hexColor >> hex >> color; + // hexColor >> color; + // Points + for(int i = 0; i < 3; i++){ + getline(is, line); + lineStream.str(line); + lineStream.clear(); + // Iterate past "P#:" + lineStream >> token; + // Get each coord + for(int j = 0; j < 3; j++){ + lineStream >> token; + shapeCoords[i][j] = stoi(token); + } + } + shapes.push_back( + new Triangle(shapeCoords[0][0],shapeCoords[0][1], + shapeCoords[1][0],shapeCoords[1][1], + shapeCoords[2][0],shapeCoords[2][1],color) + ); + break; + } + default: + { + cout << "Invalid Shape"; + } + + } + // Go to blank line + getline(is, line); + lineStream.str(line); + lineStream.clear(); + // Get next line + getline(is, line); + lineStream.str(line); + lineStream.clear(); + lineStream >> token; + } while(!is.eof()); // Loop until end of file } -// TODO void Image::erase(){ for(int i = 0; i < shapes.size(); i++){ delete shapes[i]; diff --git a/Lab5/image.h b/Lab5/image.h index 03d9f5d..c65e28f 100644 --- a/Lab5/image.h +++ b/Lab5/image.h @@ -12,24 +12,43 @@ */ #include #include "shape.h" +#include "line.h" +#include "triangle.h" #include "x11context.h" #include using namespace std; class Image{ public: + // Image constructor Image(); + + // Image copy constructor Image(const Image& from); + + // Image destructor ~Image(); + + // Image assignment operator Image& operator=(const Image& rhs); + + // Adds a passed in shape to the image void add(Shape* shape); + + // Draws all the shapes in the image void draw(GraphicsContext* gc); + + // Outputs all of the shape data to an output stream void out(std::ostream& os); + + // Inputs all the shape data from a given input stream void in(std::istream& is); + + // Erases all the shapes in the image void erase(); + private: vector shapes; - }; #endif \ No newline at end of file diff --git a/Lab5/line.cpp b/Lab5/line.cpp index 2d89d67..719c424 100644 --- a/Lab5/line.cpp +++ b/Lab5/line.cpp @@ -11,6 +11,7 @@ #include "shape.h" #include "matrix.h" #include "line.h" +#include Line::Line(int x0, int y0, int x1, int y1, uint32_t color){ coords = new Matrix(3, 3); @@ -30,7 +31,7 @@ Line::Line(int x0, int y0, int x1, int y1, uint32_t color){ (*coords)[2][1] = 1.0; (*coords)[2][2] = 1.0; - this->color = color; + this->color = color & 0x00FFFFFF; } Line::Line(const Line &from){ @@ -73,16 +74,12 @@ void Line::draw(GraphicsContext *gc){ } void Line::out(std::ostream& os) const{ - os << "-Line-" << endl; - os << "Color: " << color << endl; - - os << "Point 1 - X: " << (*coords)[0][0]; - os << " Y: " << (*coords)[0][1]; - os << " Z: " << (*coords)[0][2] << endl; - - os << "Point 2 - X: " << (*coords)[1][0]; - os << " Y: " << (*coords)[1][1]; - os << " Z: " << (*coords)[1][2] << endl; + os << "Line" << endl; + os << "Color: " << hex << setw(6) << setfill('0') << color << endl; + os << "P1: " << (*coords)[0][0] << " " << (*coords)[0][1]; + os << " " << (*coords)[0][2] << endl; + os << "P2: " << (*coords)[1][0] << " " << (*coords)[1][1]; + os << " " << (*coords)[1][2] << endl; } diff --git a/Lab5/line.h b/Lab5/line.h index 5c31430..e848e2d 100644 --- a/Lab5/line.h +++ b/Lab5/line.h @@ -19,12 +19,25 @@ class Line: public Shape{ public: // Line constructor Line(int x0, int y0, int x1, int y1, uint32_t color); - Line(const Line& from); + + // Line copy constructor + Line(const Line& from); + + // Line destructor ~Line(); + + // Line assignment operator Line& operator=(const Line& rhs); + + // Draw function void draw(GraphicsContext *gc); + + // Outputs line data to os void out(std::ostream& os) const; + + // Clones a line Shape* clone(); + }; #endif \ No newline at end of file diff --git a/Lab5/main.cpp b/Lab5/main.cpp index 2288f2d..b581122 100644 --- a/Lab5/main.cpp +++ b/Lab5/main.cpp @@ -10,6 +10,8 @@ */ #include #include +#include +#include #include "x11context.h" #include "shape.h" #include "line.h" @@ -19,68 +21,77 @@ using namespace std; GraphicsContext* gc = new X11Context(800,600,GraphicsContext::BLACK); +vector boatScene; + void part1Test(){ + cout << "Part 1 Test" << endl; + // Boat - Shape* LT1 = new Line(250,500,550,500,GraphicsContext::YELLOW); - Shape* LT2 = new Line(250,500,150,400,GraphicsContext::YELLOW); - Shape* LT3 = new Line(150,400,650,400,GraphicsContext::YELLOW); - Shape* LT4 = new Line(650,400,550,500,GraphicsContext::YELLOW); - Shape* LT5 = new Line(400,400,400,100,GraphicsContext::YELLOW); + boatScene.push_back(new Line(250,500,550,500,GraphicsContext::YELLOW)); + boatScene.push_back(new Line(250,500,150,400,GraphicsContext::YELLOW)); + boatScene.push_back(new Line(150,400,650,400,GraphicsContext::YELLOW)); + boatScene.push_back(new Line(650,400,550,500,GraphicsContext::YELLOW)); + boatScene.push_back(new Line(400,400,400,100,GraphicsContext::YELLOW)); // Sail - Shape* TT1 = new Triangle(400,100,300,150,400,200,GraphicsContext::YELLOW); + boatScene.push_back(new Triangle(400,100,300,150,400,200,GraphicsContext::YELLOW)); - // Waves - Shape* LT8 = new Line(0,550,100,450,GraphicsContext::BLUE); - Shape* LT9 = new Line(100,450,100,550,GraphicsContext::BLUE); - Shape* LT10 = new Line(100,550,200,450,GraphicsContext::BLUE); - Shape* LT11 = new Line(200,450,200,550,GraphicsContext::BLUE); - Shape* LT12 = new Line(200,550,300,450,GraphicsContext::BLUE); - Shape* LT13 = new Line(300,450,300,550,GraphicsContext::BLUE); - Shape* LT14 = new Line(300,550,400,450,GraphicsContext::BLUE); - Shape* LT15 = new Line(400,450,400,550,GraphicsContext::BLUE); - Shape* LT16 = new Line(400,550,500,450,GraphicsContext::BLUE); - Shape* LT17 = new Line(500,450,500,550,GraphicsContext::BLUE); - Shape* LT18 = new Line(500,550,600,450,GraphicsContext::BLUE); - Shape* LT19 = new Line(600,450,600,550,GraphicsContext::BLUE); - Shape* LT20 = new Line(600,550,700,450,GraphicsContext::BLUE); - Shape* LT21 = new Line(700,450,700,550,GraphicsContext::BLUE); - Shape* LT22 = new Line(700,550,800,450,GraphicsContext::BLUE); + boatScene.push_back(new Line(0,550,100,450,GraphicsContext::BLUE)); + boatScene.push_back(new Line(100,450,100,550,GraphicsContext::BLUE)); + boatScene.push_back(new Line(100,550,200,450,GraphicsContext::BLUE)); + boatScene.push_back(new Line(200,450,200,550,GraphicsContext::BLUE)); + boatScene.push_back(new Line(200,550,300,450,GraphicsContext::BLUE)); + boatScene.push_back(new Line(300,450,300,550,GraphicsContext::BLUE)); + boatScene.push_back(new Line(300,550,400,450,GraphicsContext::BLUE)); + boatScene.push_back(new Line(400,450,400,550,GraphicsContext::BLUE)); + boatScene.push_back(new Line(400,550,500,450,GraphicsContext::BLUE)); + boatScene.push_back(new Line(500,450,500,550,GraphicsContext::BLUE)); + boatScene.push_back(new Line(500,550,600,450,GraphicsContext::BLUE)); + boatScene.push_back(new Line(600,450,600,550,GraphicsContext::BLUE)); + boatScene.push_back(new Line(600,550,700,450,GraphicsContext::BLUE)); + boatScene.push_back(new Line(700,450,700,550,GraphicsContext::BLUE)); + boatScene.push_back(new Line(700,550,800,450,GraphicsContext::BLUE)); - (*LT1).draw(gc); - (*LT2).draw(gc); - (*LT3).draw(gc); - (*LT4).draw(gc); - (*LT5).draw(gc); - (*TT1).draw(gc); - (*LT8).draw(gc); - (*LT9).draw(gc); - (*LT10).draw(gc); - (*LT11).draw(gc); - (*LT12).draw(gc); - (*LT13).draw(gc); - (*LT14).draw(gc); - (*LT15).draw(gc); - (*LT16).draw(gc); - (*LT17).draw(gc); - (*LT18).draw(gc); - (*LT19).draw(gc); - (*LT20).draw(gc); - (*LT21).draw(gc); - (*LT22).draw(gc); + for(int i = 0; i < boatScene.size(); i++){ + boatScene[i]->draw(gc); + } } +// Run part 1 test first void part2Test(){ + cout << "Part 2 Test" << endl; Image image1; - for(int i = 0; i < 10; i++){ - image1.add(new Line(rand()%800,rand()%600,rand()%800,rand()%600,rand())); - } - for(int i = 0; i < 10; i++){ - image1.add(new Triangle(rand()%800,rand()%600,rand()%800,rand()%600,rand()%800,rand()%600,rand())); - } + Line L1(300,400,400,600,0x00FF00); + image1.add(&L1); image1.draw(gc); + image1.erase(); + sleep(3); + gc->clear(); + Image boat; + for(int i = 0; i < boatScene.size(); i++){ + boat.add(boatScene[i]); + } + Image boat2(boat); + boat2.draw(gc); + sleep(5); + gc->clear(); + sleep(1); + ofstream boatImage; + boatImage.open("boat.txt"); + boat.out(boatImage); + boatImage.close(); + Image image2; + ifstream imageFile2("boat.txt", ifstream::in); + image2.in(imageFile2); + image2.draw(gc); + boat2.erase(); + image2.erase(); + boat.erase(); + for(int i = 0; i < boatScene.size(); i++){ + delete boatScene[i]; + } } int main(void) @@ -92,8 +103,7 @@ int main(void) part2Test(); sleep(5); gc->clear(); - - sleep(20); + delete gc; diff --git a/Lab5/shape.h b/Lab5/shape.h index c400520..8d49839 100644 --- a/Lab5/shape.h +++ b/Lab5/shape.h @@ -20,23 +20,31 @@ class Shape{ public: // Constructor Shape(); + // Copy Constructor Shape(const Shape& from); + // Destructor virtual ~Shape(); + // Draw shape virtual void draw(GraphicsContext *gc) = 0; + // Print to output stream virtual void out(std::ostream& os)const = 0; + // Virtual Constructor virtual Shape* clone() = 0; // Pure virtual "=0" protected: // Matrix containing the coords of each point in the shape Matrix* coords; + // RGB color uint32_t color; + // Assignment Operator virtual Shape& operator=(const Shape& rhs); + }; #endif \ No newline at end of file diff --git a/Lab5/triangle.cpp b/Lab5/triangle.cpp index 0881d76..867cf67 100644 --- a/Lab5/triangle.cpp +++ b/Lab5/triangle.cpp @@ -11,6 +11,7 @@ #include "shape.h" #include "matrix.h" #include "triangle.h" +#include Triangle::Triangle(int x0, int y0, int x1, int y1, int x2, int y2, uint32_t color){ coords = new Matrix(4,3); @@ -35,7 +36,7 @@ Triangle::Triangle(int x0, int y0, int x1, int y1, int x2, int y2, uint32_t colo (*coords)[3][1] = 1.0; (*coords)[3][2] = 1.0; - this->color = color; + this->color = color & 0x00FFFFFF; } Triangle::Triangle(const Triangle &from){ @@ -69,22 +70,14 @@ Triangle& Triangle::operator=(const Triangle& rhs){ } void Triangle::out(std::ostream& os) const{ - os << "-Triangle-" << endl; - os << "Color: " << color << endl; - - os << "Point 1 - X: " << (*coords)[0][0]; - os << " Y: " << (*coords)[0][1]; - os << " Z: " << (*coords)[0][2] << endl; - - - os << "Point 2 - X: " << (*coords)[1][0]; - os << " Y: " << (*coords)[1][1]; - os << " Z: " << (*coords)[1][2] << endl; - - - os << "Point 3 - X: " << (*coords)[2][0]; - os << " Y: " << (*coords)[2][1]; - os << " Z: " << (*coords)[2][2] << endl; + os << "Triangle" << endl; + os << "Color: " << hex << setw(6) << setfill('0') << color << endl; + os << "P1: " << (*coords)[0][0] << " " << (*coords)[0][1]; + os << " " << (*coords)[0][2] << endl; + os << "P2: " << (*coords)[1][0] << " " << (*coords)[1][1]; + os << " " << (*coords)[1][2] << endl; + os << "P3: " << (*coords)[2][0] << " " << (*coords)[2][1]; + os << " " << (*coords)[2][2] << endl; } diff --git a/Lab5/triangle.h b/Lab5/triangle.h index 93bf442..920cb9a 100644 --- a/Lab5/triangle.h +++ b/Lab5/triangle.h @@ -18,15 +18,26 @@ class Triangle: public Shape{ public: // Triangle constructor - Triangle(int x0, int y0, int x1, int y1, int x2, int y2, uint32_t color); + Triangle(int x0, int y0, int x1, int y1, int x2, int y2, + uint32_t color); + + // Triangle copy constructor Triangle(const Triangle& from); + + // Triangle destructor ~Triangle(); + + // Triangle assignment operator Triangle& operator=(const Triangle& rhs); + + // Draw function void draw(GraphicsContext *gc); + + // Outputs line data to os void out(std::ostream& os) const; + + // Clones a triangle Shape* clone(); - private: - Matrix * coords; }; #endif \ No newline at end of file