lab 5 progress

This commit is contained in:
2022-04-25 19:10:06 -05:00
parent 75ac03dd08
commit bf5490504c
10 changed files with 405 additions and 26 deletions

91
Lab5/line.cpp Normal file
View File

@@ -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);
}

View File

@@ -1,8 +1,10 @@
#ifndef line_h
#define line_h
/** /**
* @file line.h * @file line.h
* @author your name (you@domain.com) * @author Trevor Barnes (barnestr@msoe.edu)
* @brief * @brief
* @version 0.1 * @version 1.0
* @date 2022-04-12 * @date 2022-04-12
* *
* @copyright Copyright (c) 2022 * @copyright Copyright (c) 2022
@@ -11,11 +13,17 @@
#include "shape.h" #include "shape.h"
#include "matrix.h" #include "matrix.h"
#include "x11context.h"
class Line: public Shape{ class Line: public Shape{
public: 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: #endif
}

View File

@@ -8,18 +8,30 @@
* @copyright Copyright (c) 2022 * @copyright Copyright (c) 2022
* *
*/ */
#include "x11context.h"
#include <unistd.h> #include <unistd.h>
#include <iostream> #include <iostream>
#include "x11context.h"
#include "shape.h"
#include "line.h"
#include "triangle.h"
using namespace std; using namespace std;
int main(void) int main(void)
{ {
GraphicsContext* gc = new X11Context(800,600,GraphicsContext::BLACK); 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(5);
sleep(10);
delete gc; delete gc;

View File

@@ -4,10 +4,10 @@ CC = g++
CFLAGS = -c -MMD -g CFLAGS = -c -MMD -g
LFLAGS = -lX11 LFLAGS = -lX11
# Change w/ every new project # 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) OBJECTS = $(SOURCES:.cpp=.o)
# Change w/ every new project # Change w/ every new project
EXECUTABLE = Lab4 EXECUTABLE = Lab5
all: $(EXECUTABLE) $(SOURCES) all: $(EXECUTABLE) $(SOURCES)

87
Lab5/row.cpp Normal file
View File

@@ -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 <stdexcept>
#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;
}
}

51
Lab5/row.h Normal file
View File

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

View File

@@ -1,8 +1,8 @@
/** /**
* @file shape.cpp * @file shape.cpp
* @author your name (you@domain.com) * @author Trevor Barnes (barnestr@msoe.edu)
* @brief * @brief
* @version 0.1 * @version 1.0
* @date 2022-04-12 * @date 2022-04-12
* *
* @copyright Copyright (c) 2022 * @copyright Copyright (c) 2022
@@ -11,5 +11,19 @@
#include "shape.h" #include "shape.h"
Shape::Shape(){ Shape::Shape(){
this->RGB = (uint32_t) 0x00FFFFFF; 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;
} }

View File

@@ -1,5 +1,8 @@
#ifndef shape_h
#define shape_h
#include <iostream> #include <iostream>
#include "x11context.h" #include "x11context.h"
#include "matrix.h"
using namespace std; using namespace std;
@@ -11,20 +14,19 @@ class Shape{
Shape(const Shape& from); Shape(const Shape& from);
// Destructor // Destructor
virtual ~Shape(); virtual ~Shape();
// Virtual Constructor
virtual Shape* clone();
// Draw shape // Draw shape
virtual void draw(GraphicsContext gc); virtual void draw(GraphicsContext *gc);
// Print to output stream // Print to output stream
virtual void out(std::ostream& os, const Shape& rhs); virtual void out(std::ostream& os, const Shape& rhs);
// Virtual Constructor
virtual Shape* clone() = 0; // Pure virtual "=0"
protected: protected:
// Matrix containing the coords of each point in the shape
Matrix* coords;
// RGB color
uint32_t color;
// Assignment Operator // Assignment Operator
Shape& operator=(const Shape& rhs); Shape& operator=(const Shape& rhs);
private:
uint32_t RGB;
}; };
#endif

View File

@@ -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]);
}

View File

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