lab 5 part 2 complete

This commit is contained in:
2022-05-10 01:45:01 -05:00
parent 780c2bba3c
commit 6a425d5300
10 changed files with 345 additions and 102 deletions

BIN
Lab5/Lab5

Binary file not shown.

106
Lab5/boat.txt Normal file
View File

@@ -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

View File

@@ -10,21 +10,19 @@
*/ */
#include "image.h" #include "image.h"
#include <sstream>
#include <fstream>
// TODO
Image::Image(){ Image::Image(){
shapes.clear(); shapes.clear();
} }
// TODO
Image::Image(const Image& from){ Image::Image(const Image& from){
for(int i = 0; i < from.shapes.size(); i++){ for(int i = 0; i < from.shapes.size(); i++){
shapes.push_back(from.shapes[i]->clone()); shapes.push_back(from.shapes[i]->clone());
} }
} }
// TODO
Image::~Image(){ Image::~Image(){
for(int i = 0; i < shapes.size(); i++){ for(int i = 0; i < shapes.size(); i++){
delete shapes[i]; delete shapes[i];
@@ -32,8 +30,8 @@ Image::~Image(){
shapes.clear(); shapes.clear();
} }
// TODO
Image &Image::operator=(const Image& rhs){ Image &Image::operator=(const Image& rhs){
if(this != &rhs){
for(int i = 0; i < shapes.size(); i++){ for(int i = 0; i < shapes.size(); i++){
delete shapes[i]; delete shapes[i];
} }
@@ -41,22 +39,20 @@ Image &Image::operator=(const Image& rhs){
for(int i = 0; i < rhs.shapes.size(); i++){ for(int i = 0; i < rhs.shapes.size(); i++){
shapes.push_back(rhs.shapes[i]->clone()); shapes.push_back(rhs.shapes[i]->clone());
} }
}
return *this; return *this;
} }
// TODO
void Image::add(Shape* shape){ void Image::add(Shape* shape){
shapes.push_back(shape); shapes.push_back(shape->clone());
} }
// TODO
void Image::draw(GraphicsContext* gc){ void Image::draw(GraphicsContext* gc){
for(int i = 0; i < shapes.size(); i++){ for(int i = 0; i < shapes.size(); i++){
shapes[i]->draw(gc); shapes[i]->draw(gc);
} }
} }
// TODO
void Image::out(std::ostream& os){ void Image::out(std::ostream& os){
for(int i = 0; i < shapes.size(); i++){ for(int i = 0; i < shapes.size(); i++){
shapes[i]->out(os); shapes[i]->out(os);
@@ -64,12 +60,102 @@ void Image::out(std::ostream& os){
} }
} }
// TODO
void Image::in(std::istream& is){ 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(){ void Image::erase(){
for(int i = 0; i < shapes.size(); i++){ for(int i = 0; i < shapes.size(); i++){
delete shapes[i]; delete shapes[i];

View File

@@ -12,24 +12,43 @@
*/ */
#include <iostream> #include <iostream>
#include "shape.h" #include "shape.h"
#include "line.h"
#include "triangle.h"
#include "x11context.h" #include "x11context.h"
#include <vector> #include <vector>
using namespace std; using namespace std;
class Image{ class Image{
public: public:
// Image constructor
Image(); Image();
// Image copy constructor
Image(const Image& from); Image(const Image& from);
// Image destructor
~Image(); ~Image();
// Image assignment operator
Image& operator=(const Image& rhs); Image& operator=(const Image& rhs);
// Adds a passed in shape to the image
void add(Shape* shape); void add(Shape* shape);
// Draws all the shapes in the image
void draw(GraphicsContext* gc); void draw(GraphicsContext* gc);
// Outputs all of the shape data to an output stream
void out(std::ostream& os); void out(std::ostream& os);
// Inputs all the shape data from a given input stream
void in(std::istream& is); void in(std::istream& is);
// Erases all the shapes in the image
void erase(); void erase();
private: private:
vector<Shape*> shapes; vector<Shape*> shapes;
}; };
#endif #endif

View File

@@ -11,6 +11,7 @@
#include "shape.h" #include "shape.h"
#include "matrix.h" #include "matrix.h"
#include "line.h" #include "line.h"
#include <iomanip>
Line::Line(int x0, int y0, int x1, int y1, uint32_t color){ Line::Line(int x0, int y0, int x1, int y1, uint32_t color){
coords = new Matrix(3, 3); 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][1] = 1.0;
(*coords)[2][2] = 1.0; (*coords)[2][2] = 1.0;
this->color = color; this->color = color & 0x00FFFFFF;
} }
Line::Line(const Line &from){ Line::Line(const Line &from){
@@ -73,16 +74,12 @@ void Line::draw(GraphicsContext *gc){
} }
void Line::out(std::ostream& os) const{ void Line::out(std::ostream& os) const{
os << "-Line-" << endl; os << "Line" << endl;
os << "Color: " << color << endl; os << "Color: " << hex << setw(6) << setfill('0') << color << endl;
os << "P1: " << (*coords)[0][0] << " " << (*coords)[0][1];
os << "Point 1 - X: " << (*coords)[0][0]; os << " " << (*coords)[0][2] << endl;
os << " Y: " << (*coords)[0][1]; os << "P2: " << (*coords)[1][0] << " " << (*coords)[1][1];
os << " Z: " << (*coords)[0][2] << endl; os << " " << (*coords)[1][2] << endl;
os << "Point 2 - X: " << (*coords)[1][0];
os << " Y: " << (*coords)[1][1];
os << " Z: " << (*coords)[1][2] << endl;
} }

View File

@@ -19,12 +19,25 @@ class Line: public Shape{
public: public:
// Line constructor // Line constructor
Line(int x0, int y0, int x1, int y1, uint32_t color); Line(int x0, int y0, int x1, int y1, uint32_t color);
// Line copy constructor
Line(const Line& from); Line(const Line& from);
// Line destructor
~Line(); ~Line();
// Line assignment operator
Line& operator=(const Line& rhs); Line& operator=(const Line& rhs);
// Draw function
void draw(GraphicsContext *gc); void draw(GraphicsContext *gc);
// Outputs line data to os
void out(std::ostream& os) const; void out(std::ostream& os) const;
// Clones a line
Shape* clone(); Shape* clone();
}; };
#endif #endif

View File

@@ -10,6 +10,8 @@
*/ */
#include <unistd.h> #include <unistd.h>
#include <iostream> #include <iostream>
#include <fstream>
#include <vector>
#include "x11context.h" #include "x11context.h"
#include "shape.h" #include "shape.h"
#include "line.h" #include "line.h"
@@ -19,68 +21,77 @@ using namespace std;
GraphicsContext* gc = new X11Context(800,600,GraphicsContext::BLACK); GraphicsContext* gc = new X11Context(800,600,GraphicsContext::BLACK);
vector<Shape*> boatScene;
void part1Test(){ void part1Test(){
cout << "Part 1 Test" << endl;
// Boat // Boat
Shape* LT1 = new Line(250,500,550,500,GraphicsContext::YELLOW); boatScene.push_back(new Line(250,500,550,500,GraphicsContext::YELLOW));
Shape* LT2 = new Line(250,500,150,400,GraphicsContext::YELLOW); boatScene.push_back(new Line(250,500,150,400,GraphicsContext::YELLOW));
Shape* LT3 = new Line(150,400,650,400,GraphicsContext::YELLOW); boatScene.push_back(new Line(150,400,650,400,GraphicsContext::YELLOW));
Shape* LT4 = new Line(650,400,550,500,GraphicsContext::YELLOW); boatScene.push_back(new Line(650,400,550,500,GraphicsContext::YELLOW));
Shape* LT5 = new Line(400,400,400,100,GraphicsContext::YELLOW); boatScene.push_back(new Line(400,400,400,100,GraphicsContext::YELLOW));
// Sail // 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 // Waves
Shape* LT8 = new Line(0,550,100,450,GraphicsContext::BLUE); boatScene.push_back(new Line(0,550,100,450,GraphicsContext::BLUE));
Shape* LT9 = new Line(100,450,100,550,GraphicsContext::BLUE); boatScene.push_back(new Line(100,450,100,550,GraphicsContext::BLUE));
Shape* LT10 = new Line(100,550,200,450,GraphicsContext::BLUE); boatScene.push_back(new Line(100,550,200,450,GraphicsContext::BLUE));
Shape* LT11 = new Line(200,450,200,550,GraphicsContext::BLUE); boatScene.push_back(new Line(200,450,200,550,GraphicsContext::BLUE));
Shape* LT12 = new Line(200,550,300,450,GraphicsContext::BLUE); boatScene.push_back(new Line(200,550,300,450,GraphicsContext::BLUE));
Shape* LT13 = new Line(300,450,300,550,GraphicsContext::BLUE); boatScene.push_back(new Line(300,450,300,550,GraphicsContext::BLUE));
Shape* LT14 = new Line(300,550,400,450,GraphicsContext::BLUE); boatScene.push_back(new Line(300,550,400,450,GraphicsContext::BLUE));
Shape* LT15 = new Line(400,450,400,550,GraphicsContext::BLUE); boatScene.push_back(new Line(400,450,400,550,GraphicsContext::BLUE));
Shape* LT16 = new Line(400,550,500,450,GraphicsContext::BLUE); boatScene.push_back(new Line(400,550,500,450,GraphicsContext::BLUE));
Shape* LT17 = new Line(500,450,500,550,GraphicsContext::BLUE); boatScene.push_back(new Line(500,450,500,550,GraphicsContext::BLUE));
Shape* LT18 = new Line(500,550,600,450,GraphicsContext::BLUE); boatScene.push_back(new Line(500,550,600,450,GraphicsContext::BLUE));
Shape* LT19 = new Line(600,450,600,550,GraphicsContext::BLUE); boatScene.push_back(new Line(600,450,600,550,GraphicsContext::BLUE));
Shape* LT20 = new Line(600,550,700,450,GraphicsContext::BLUE); boatScene.push_back(new Line(600,550,700,450,GraphicsContext::BLUE));
Shape* LT21 = new Line(700,450,700,550,GraphicsContext::BLUE); boatScene.push_back(new Line(700,450,700,550,GraphicsContext::BLUE));
Shape* LT22 = new Line(700,550,800,450,GraphicsContext::BLUE); boatScene.push_back(new Line(700,550,800,450,GraphicsContext::BLUE));
(*LT1).draw(gc); for(int i = 0; i < boatScene.size(); i++){
(*LT2).draw(gc); boatScene[i]->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);
} }
// Run part 1 test first
void part2Test(){ void part2Test(){
cout << "Part 2 Test" << endl;
Image image1; Image image1;
for(int i = 0; i < 10; i++){ Line L1(300,400,400,600,0x00FF00);
image1.add(new Line(rand()%800,rand()%600,rand()%800,rand()%600,rand())); image1.add(&L1);
}
for(int i = 0; i < 10; i++){
image1.add(new Triangle(rand()%800,rand()%600,rand()%800,rand()%600,rand()%800,rand()%600,rand()));
}
image1.draw(gc); 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) int main(void)
@@ -93,7 +104,6 @@ int main(void)
sleep(5); sleep(5);
gc->clear(); gc->clear();
sleep(20);
delete gc; delete gc;

View File

@@ -20,23 +20,31 @@ class Shape{
public: public:
// Constructor // Constructor
Shape(); Shape();
// Copy Constructor // Copy Constructor
Shape(const Shape& from); Shape(const Shape& from);
// Destructor // Destructor
virtual ~Shape(); virtual ~Shape();
// Draw shape // Draw shape
virtual void draw(GraphicsContext *gc) = 0; virtual void draw(GraphicsContext *gc) = 0;
// Print to output stream // Print to output stream
virtual void out(std::ostream& os)const = 0; virtual void out(std::ostream& os)const = 0;
// Virtual Constructor // Virtual Constructor
virtual Shape* clone() = 0; // Pure virtual "=0" virtual Shape* clone() = 0; // Pure virtual "=0"
protected: protected:
// Matrix containing the coords of each point in the shape // Matrix containing the coords of each point in the shape
Matrix* coords; Matrix* coords;
// RGB color // RGB color
uint32_t color; uint32_t color;
// Assignment Operator // Assignment Operator
virtual Shape& operator=(const Shape& rhs); virtual Shape& operator=(const Shape& rhs);
}; };
#endif #endif

View File

@@ -11,6 +11,7 @@
#include "shape.h" #include "shape.h"
#include "matrix.h" #include "matrix.h"
#include "triangle.h" #include "triangle.h"
#include <iomanip>
Triangle::Triangle(int x0, int y0, int x1, int y1, int x2, int y2, uint32_t color){ Triangle::Triangle(int x0, int y0, int x1, int y1, int x2, int y2, uint32_t color){
coords = new Matrix(4,3); 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][1] = 1.0;
(*coords)[3][2] = 1.0; (*coords)[3][2] = 1.0;
this->color = color; this->color = color & 0x00FFFFFF;
} }
Triangle::Triangle(const Triangle &from){ Triangle::Triangle(const Triangle &from){
@@ -69,22 +70,14 @@ Triangle& Triangle::operator=(const Triangle& rhs){
} }
void Triangle::out(std::ostream& os) const{ void Triangle::out(std::ostream& os) const{
os << "-Triangle-" << endl; os << "Triangle" << endl;
os << "Color: " << color << endl; os << "Color: " << hex << setw(6) << setfill('0') << color << endl;
os << "P1: " << (*coords)[0][0] << " " << (*coords)[0][1];
os << "Point 1 - X: " << (*coords)[0][0]; os << " " << (*coords)[0][2] << endl;
os << " Y: " << (*coords)[0][1]; os << "P2: " << (*coords)[1][0] << " " << (*coords)[1][1];
os << " Z: " << (*coords)[0][2] << endl; os << " " << (*coords)[1][2] << endl;
os << "P3: " << (*coords)[2][0] << " " << (*coords)[2][1];
os << " " << (*coords)[2][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;
} }

View File

@@ -18,15 +18,26 @@
class Triangle: public Shape{ class Triangle: public Shape{
public: public:
// Triangle constructor // 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(const Triangle& from);
// Triangle destructor
~Triangle(); ~Triangle();
// Triangle assignment operator
Triangle& operator=(const Triangle& rhs); Triangle& operator=(const Triangle& rhs);
// Draw function
void draw(GraphicsContext *gc); void draw(GraphicsContext *gc);
// Outputs line data to os
void out(std::ostream& os) const; void out(std::ostream& os) const;
// Clones a triangle
Shape* clone(); Shape* clone();
private:
Matrix * coords;
}; };
#endif #endif