From e381f5d7cae4d2fb6e19eb187db45c9e5fa79594 Mon Sep 17 00:00:00 2001 From: barnestr Date: Mon, 2 May 2022 22:07:53 -0500 Subject: [PATCH] lab 5 part 1 done --- Lab5/line.cpp | 53 ++++++++++++++++++++++++----------------------- Lab5/line.h | 1 + Lab5/main.cpp | 9 ++++---- Lab5/shape.cpp | 9 +------- Lab5/shape.h | 16 +++++++++++--- Lab5/triangle.cpp | 22 +++++++++++++++++++- Lab5/triangle.h | 3 ++- 7 files changed, 69 insertions(+), 44 deletions(-) diff --git a/Lab5/line.cpp b/Lab5/line.cpp index 33e691a..2d89d67 100644 --- a/Lab5/line.cpp +++ b/Lab5/line.cpp @@ -1,7 +1,7 @@ /** * @file line.cpp * @author Trevor Barnes (barnestr@msoe.edu) - * @brief + * @brief * @version 1.0 * @date 2022-04-12 * @@ -35,28 +35,14 @@ Line::Line(int x0, int y0, int x1, int y1, uint32_t color){ Line::Line(const Line &from){ this->color = from.color; - this->coords = new Matrix(4,3); - // for (int i = 0; i < 2; i++) - // { - // for (int j = 0; j < 2; j++) - // { - // // Needs a look - // this->coords[i][j] = from.coords[i][j]; - // //(*coords)[i][j] = (*from.coords)[i][j]; - // } - // } - this->coords[0][0] = from.coords[0][0]; - this->coords[0][1] = from.coords[0][1]; - this->coords[0][2] = from.coords[0][2]; - - this->coords[1][0] = from.coords[1][0]; - this->coords[1][1] = from.coords[1][1]; - this->coords[1][2] = from.coords[1][2]; - - this->coords[2][0] = from.coords[2][0]; - this->coords[2][1] = from.coords[2][1]; - this->coords[2][2] = from.coords[2][2]; - + this->coords = new Matrix(3,3); + for (int i = 0; i < 2; i++) + { + for (int j = 0; j < 2; j++) + { + (*coords)[i][j] = (*from.coords)[i][j]; + } + } } Line::~Line(){ @@ -68,12 +54,12 @@ Line &Line::operator=(const Line &rhs){ { this->color = rhs.color; delete coords; - coords = new Matrix(3, 3); + coords = new Matrix(3,3); for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { - this->coords[i][j] = rhs.coords[i][j]; + (*coords)[i][j] = (*rhs.coords)[i][j]; } } } @@ -86,6 +72,21 @@ void Line::draw(GraphicsContext *gc){ (*coords)[1][0], (*coords)[1][1]); } -Shape *Line::clone(){ +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; + +} + + +Shape* Line::clone(){ return new Line(*this); } \ No newline at end of file diff --git a/Lab5/line.h b/Lab5/line.h index 309ce77..5c31430 100644 --- a/Lab5/line.h +++ b/Lab5/line.h @@ -23,6 +23,7 @@ class Line: public Shape{ ~Line(); Line& operator=(const Line& rhs); void draw(GraphicsContext *gc); + void out(std::ostream& os) const; Shape* clone(); }; diff --git a/Lab5/main.cpp b/Lab5/main.cpp index 4af7ccb..a7b8f31 100644 --- a/Lab5/main.cpp +++ b/Lab5/main.cpp @@ -23,13 +23,12 @@ int main(void) Shape* T1 = new Triangle(250, 100, 50, 320, 125, 200, GraphicsContext::RED); Shape* L2 = L1->clone(); Shape* T2 = T1->clone(); - Shape* L3(L1); - Shape* T3(T1); delete L1; delete T1; - (*L3).draw(gc); - (*T3).draw(gc); - + (*L2).draw(gc); + (*T2).draw(gc); + (*L2).out(cout); + (*T2).out(cout); sleep(5); diff --git a/Lab5/shape.cpp b/Lab5/shape.cpp index aaab2fe..42e6b7a 100644 --- a/Lab5/shape.cpp +++ b/Lab5/shape.cpp @@ -13,17 +13,10 @@ Shape::Shape(){ this->color = (uint32_t) 0x00FFFFFF; } -Shape::Shape(const Shape& from){ - this->color = from.color; - this->coords = from.coords; -} +Shape::Shape(const Shape& from){}; Shape::~Shape(){}; -void Shape::draw(GraphicsContext *gc){} - -void Shape::out(std::ostream& os, const Shape& rhs){} - Shape& Shape::operator=(const Shape& rhs){ return *this; } diff --git a/Lab5/shape.h b/Lab5/shape.h index 7858231..c400520 100644 --- a/Lab5/shape.h +++ b/Lab5/shape.h @@ -1,5 +1,15 @@ #ifndef shape_h #define shape_h +/** + * @file shape.h + * @author Trevor Barnes (barnestr@msoe.edu) + * @brief + * @version 1.0 + * @date 2022-04-12 + * + * @copyright Copyright (c) 2022 + * + */ #include #include "x11context.h" #include "matrix.h" @@ -15,9 +25,9 @@ class Shape{ // Destructor virtual ~Shape(); // Draw shape - virtual void draw(GraphicsContext *gc); + virtual void draw(GraphicsContext *gc) = 0; // Print to output stream - virtual void out(std::ostream& os, const Shape& rhs); + virtual void out(std::ostream& os)const = 0; // Virtual Constructor virtual Shape* clone() = 0; // Pure virtual "=0" protected: @@ -26,7 +36,7 @@ class Shape{ // RGB color uint32_t color; // Assignment Operator - Shape& operator=(const Shape& rhs); + virtual Shape& operator=(const Shape& rhs); }; #endif \ No newline at end of file diff --git a/Lab5/triangle.cpp b/Lab5/triangle.cpp index 08c9a07..0881d76 100644 --- a/Lab5/triangle.cpp +++ b/Lab5/triangle.cpp @@ -68,7 +68,27 @@ Triangle& Triangle::operator=(const Triangle& rhs){ return *this; } -Shape * Triangle::clone(){ +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; +} + + +Shape* Triangle::clone(){ return new Triangle(*this); } diff --git a/Lab5/triangle.h b/Lab5/triangle.h index a3e5d4f..93bf442 100644 --- a/Lab5/triangle.h +++ b/Lab5/triangle.h @@ -23,7 +23,8 @@ class Triangle: public Shape{ ~Triangle(); Triangle& operator=(const Triangle& rhs); void draw(GraphicsContext *gc); - Shape * clone(); + void out(std::ostream& os) const; + Shape* clone(); private: Matrix * coords;