diff --git a/Lab5/line.cpp b/Lab5/line.cpp new file mode 100644 index 0000000..33e691a --- /dev/null +++ b/Lab5/line.cpp @@ -0,0 +1,91 @@ +/** + * @file line.cpp + * @author Trevor Barnes (barnestr@msoe.edu) + * @brief + * @version 1.0 + * @date 2022-04-12 + * + * @copyright Copyright (c) 2022 + * + */ +#include "shape.h" +#include "matrix.h" +#include "line.h" + +Line::Line(int x0, int y0, int x1, int y1, uint32_t color){ + coords = new Matrix(3, 3); + + // Point 1 + (*coords)[0][0] = x0; + (*coords)[0][1] = y0; + (*coords)[0][2] = 0.0; + + // Point 2 + (*coords)[1][0] = x1; + (*coords)[1][1] = y1; + (*coords)[1][2] = 0.0; + + // Ones + (*coords)[2][0] = 1.0; + (*coords)[2][1] = 1.0; + (*coords)[2][2] = 1.0; + + this->color = 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]; + +} + +Line::~Line(){ + delete coords; +} + +Line &Line::operator=(const Line &rhs){ + if (&rhs != this) + { + this->color = rhs.color; + delete coords; + 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]; + } + } + } + return *this; +} + +void Line::draw(GraphicsContext *gc){ + gc->setColor(color); + gc->drawLine((*coords)[0][0], (*coords)[0][1], + (*coords)[1][0], (*coords)[1][1]); +} + +Shape *Line::clone(){ + return new Line(*this); +} \ No newline at end of file diff --git a/Lab5/line.h b/Lab5/line.h index fb2613d..309ce77 100644 --- a/Lab5/line.h +++ b/Lab5/line.h @@ -1,8 +1,10 @@ +#ifndef line_h +#define line_h /** * @file line.h - * @author your name (you@domain.com) + * @author Trevor Barnes (barnestr@msoe.edu) * @brief - * @version 0.1 + * @version 1.0 * @date 2022-04-12 * * @copyright Copyright (c) 2022 @@ -11,11 +13,17 @@ #include "shape.h" #include "matrix.h" +#include "x11context.h" class Line: public Shape{ public: + // Line constructor + Line(int x0, int y0, int x1, int y1, uint32_t color); + Line(const Line& from); + ~Line(); + Line& operator=(const Line& rhs); + void draw(GraphicsContext *gc); + Shape* clone(); +}; - private: - - -} \ No newline at end of file +#endif \ No newline at end of file diff --git a/Lab5/main.cpp b/Lab5/main.cpp index f718cba..4af7ccb 100644 --- a/Lab5/main.cpp +++ b/Lab5/main.cpp @@ -8,18 +8,30 @@ * @copyright Copyright (c) 2022 * */ -#include "x11context.h" #include #include +#include "x11context.h" +#include "shape.h" +#include "line.h" +#include "triangle.h" using namespace std; int main(void) -{ +{ GraphicsContext* gc = new X11Context(800,600,GraphicsContext::BLACK); - - + Shape* L1 = new Line(200, 150, 200, 300, GraphicsContext::GREEN); + 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); - sleep(10); + + sleep(5); delete gc; diff --git a/Lab5/makefile b/Lab5/makefile index b564bdb..c0b10e0 100644 --- a/Lab5/makefile +++ b/Lab5/makefile @@ -4,10 +4,10 @@ CC = g++ CFLAGS = -c -MMD -g LFLAGS = -lX11 # Change w/ every new project -SOURCES = main.cpp gcontext.cpp x11context.cpp +SOURCES = main.cpp gcontext.cpp x11context.cpp row.cpp matrix.cpp shape.cpp line.cpp triangle.cpp OBJECTS = $(SOURCES:.cpp=.o) # Change w/ every new project -EXECUTABLE = Lab4 +EXECUTABLE = Lab5 all: $(EXECUTABLE) $(SOURCES) diff --git a/Lab5/row.cpp b/Lab5/row.cpp new file mode 100644 index 0000000..18e6642 --- /dev/null +++ b/Lab5/row.cpp @@ -0,0 +1,87 @@ +/** + * @file row.cpp + * @author Trevor Barnes (barnestr@msoe.edu) + * @brief Contains the main functionality for row matrices utilizing arrays of + * double values + * @version 1.0 + * @date 2022-03-22 + * + * @copyright Copyright (c) 2022 + * + */ +#include +#include "row.h" +using namespace std; + +// parameterized constructor +Row::Row(unsigned int length){ + // Set matrix length private variable to passed in length + this->length = length; + // Create new array in heap for row_data + this->row_data = new double[length]; + // Clear all values in new array to 0 + clear(); +} + +// copy constructor +Row::Row(const Row& from){ + // New row matrix gets length from previous matrix + this->length = from.length; + // Create new array in heap with new length + this->row_data = new double[this->length]; + // Copy all row_data values over to new array + for(int i = 0; i < this->length; i++) { + this->row_data[i] = from[i]; + } +} + +// destructor +Row::~Row(){ + // Check for valid length then free the heap memory + if(length > 0 ) { + delete[] row_data; + } +} + +// access operator (const) +double Row::operator[](unsigned int column) const{ + if (column >= length) { + throw(out_of_range("Column is out of range")); + } + return row_data[column]; +} + +// access operator (non-const) +double& Row::operator[](unsigned int column){ + if (column >= length) { + throw(out_of_range("Column is out of range")); + } + return row_data[column]; +} + +// assignment operator +Row& Row::operator= (const Row& rhs){ + if(&rhs != this){ + // Delete the current row matrix + if(length > 0){ + delete[] this->row_data; + } + // New row matrix gets length from previous matrix + this->length = rhs.length; + // Create new array in heap with new length + this->row_data = new double[this->length]; + // Copy all row_data values over to new array + for(int i = 0; i < this->length; i ++) { + this->row_data[i] = rhs.row_data[i]; + } + } + // Return address of the Row + return *this; +} + +// clear row data +void Row::clear(){ + for(int i = 0; i < length; i++) { + this->row_data[i] = 0; + } +} \ No newline at end of file diff --git a/Lab5/row.h b/Lab5/row.h new file mode 100644 index 0000000..c825715 --- /dev/null +++ b/Lab5/row.h @@ -0,0 +1,51 @@ +#ifndef row_h +#define row_h +class Row{ + public: + /* Parameterized constructor + * Takes in length and creates a row matrix with values cleared + * to zero + */ + Row(unsigned int length); + + /* Copy constructor + * Create a new row matrix with the same size and values as the + * from matrix + */ + Row(const Row& from); + + /* Destructor + * Correctly delete any heap memory + */ + ~Row(); + + /* Access operator (const version) + * Allow access to row matrix data + * Should return an exception if column is too large + */ + double operator[](unsigned int column) const; + + /* Access operator (non const version) + * Allow access to row matrix data + * Should return an exception if column is too large + */ + double& operator[] (unsigned int column); + + /* Assignment operator + * 1. Check if two sides are the same object + * 2. Delete the current row matrix + * 3. Create a new row matrix with the same size and values as + * the rhs matrix + */ + Row& operator= (const Row& rhs); + + /* Clear all data values to zero + */ + void clear(); + private: + // Row matrix data + double * row_data; + // Size of row matrix + unsigned int length; +}; +#endif diff --git a/Lab5/shape.cpp b/Lab5/shape.cpp index 20ea8f5..aaab2fe 100644 --- a/Lab5/shape.cpp +++ b/Lab5/shape.cpp @@ -1,8 +1,8 @@ /** * @file shape.cpp - * @author your name (you@domain.com) + * @author Trevor Barnes (barnestr@msoe.edu) * @brief - * @version 0.1 + * @version 1.0 * @date 2022-04-12 * * @copyright Copyright (c) 2022 @@ -11,5 +11,19 @@ #include "shape.h" Shape::Shape(){ - this->RGB = (uint32_t) 0x00FFFFFF; -} \ No newline at end of file + this->color = (uint32_t) 0x00FFFFFF; +} +Shape::Shape(const Shape& from){ + this->color = from.color; + this->coords = from.coords; +} + +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 92cddd9..7858231 100644 --- a/Lab5/shape.h +++ b/Lab5/shape.h @@ -1,5 +1,8 @@ +#ifndef shape_h +#define shape_h #include #include "x11context.h" +#include "matrix.h" using namespace std; @@ -11,20 +14,19 @@ class Shape{ Shape(const Shape& from); // Destructor virtual ~Shape(); - // Virtual Constructor - virtual Shape* clone(); // Draw shape - virtual void draw(GraphicsContext gc); + virtual void draw(GraphicsContext *gc); // Print to output stream virtual void out(std::ostream& os, const Shape& rhs); - - + // 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 Shape& operator=(const Shape& rhs); - private: - uint32_t RGB; }; - - +#endif \ No newline at end of file diff --git a/Lab5/triangle.cpp b/Lab5/triangle.cpp index e69de29..08c9a07 100644 --- a/Lab5/triangle.cpp +++ b/Lab5/triangle.cpp @@ -0,0 +1,83 @@ +/** + * @file triangle.cpp + * @author Trevor Barnes (barnestr@msoe.edu) + * @brief + * @version 1.0 + * @date 2022-04-12 + * + * @copyright Copyright (c) 2022 + * + */ +#include "shape.h" +#include "matrix.h" +#include "triangle.h" + +Triangle::Triangle(int x0, int y0, int x1, int y1, int x2, int y2, uint32_t color){ + coords = new Matrix(4,3); + + // Point 1 + (*coords)[0][0] = x0; + (*coords)[0][1] = y0; + (*coords)[0][2] = 0.0; + + // Point 2 + (*coords)[1][0] = x1; + (*coords)[1][1] = y1; + (*coords)[1][2] = 0.0; + + // Point 3 + (*coords)[2][0] = x2; + (*coords)[2][1] = y2; + (*coords)[2][2] = 0.0; + + // Ones + (*coords)[3][0] = 1.0; + (*coords)[3][1] = 1.0; + (*coords)[3][2] = 1.0; + + this->color = color; +} + +Triangle::Triangle(const Triangle &from){ + this->coords = new Matrix(4,3); + (*this).color = from.color; + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 2; j++) + { + (*coords)[i][j] = (*from.coords)[i][j]; + } + } +} + +Triangle::~Triangle(){ + delete coords; +} + +Triangle& Triangle::operator=(const Triangle& rhs){ + if(&rhs != this){ + this->color = rhs.color; + delete coords; + coords = new Matrix(4,3); + for(int i = 0; i < 3; i++){ + for(int j = 0; j < 2; j++){ + this->coords[i][j] = rhs.coords[i][j]; + } + } + } + return *this; +} + +Shape * Triangle::clone(){ + return new Triangle(*this); +} + +void Triangle::draw(GraphicsContext *gc){ + gc->setColor(color); + gc->drawLine((*coords)[0][0], (*coords)[0][1], + (*coords)[1][0], (*coords)[1][1]); + gc->drawLine((*coords)[1][0], (*coords)[1][1], + (*coords)[2][0], (*coords)[2][1]); + gc->drawLine((*coords)[2][0], (*coords)[2][1], + (*coords)[0][0], (*coords)[0][1]); +} \ No newline at end of file diff --git a/Lab5/triangle.h b/Lab5/triangle.h index e69de29..a3e5d4f 100644 --- a/Lab5/triangle.h +++ b/Lab5/triangle.h @@ -0,0 +1,31 @@ +#ifndef triangle_h +#define triangle_h +/** + * @file triangle.h + * @author Trevor Barnes (barnestr@msoe.edu) + * @brief + * @version 1.0 + * @date 2022-04-12 + * + * @copyright Copyright (c) 2022 + * + */ + +#include "shape.h" +#include "matrix.h" +#include "x11context.h" + +class Triangle: public Shape{ + public: + // Triangle constructor + Triangle(int x0, int y0, int x1, int y1, int x2, int y2, uint32_t color); + Triangle(const Triangle& from); + ~Triangle(); + Triangle& operator=(const Triangle& rhs); + void draw(GraphicsContext *gc); + Shape * clone(); + private: + Matrix * coords; + +}; +#endif \ No newline at end of file