diff --git a/Lab6/main.cpp b/Lab6/main.cpp index fead8eb..8e59b2f 100644 --- a/Lab6/main.cpp +++ b/Lab6/main.cpp @@ -17,12 +17,47 @@ #include "mydrawing.h" using namespace std; +Image boatScene; + +void buildBoat(){ + + cout << "Build Boat" << endl; + + // Boat + boatScene.add(new Line(250,500,550,500,GraphicsContext::YELLOW)); + boatScene.add(new Line(250,500,150,400,GraphicsContext::YELLOW)); + boatScene.add(new Line(150,400,650,400,GraphicsContext::YELLOW)); + boatScene.add(new Line(650,400,550,500,GraphicsContext::YELLOW)); + boatScene.add(new Line(400,400,400,100,GraphicsContext::YELLOW)); + + // Sail + boatScene.add(new Triangle(400,100,300,150,400,200,GraphicsContext::YELLOW)); + + // Waves + boatScene.add(new Line(0,550,100,450,GraphicsContext::BLUE)); + boatScene.add(new Line(100,450,100,550,GraphicsContext::BLUE)); + boatScene.add(new Line(100,550,200,450,GraphicsContext::BLUE)); + boatScene.add(new Line(200,450,200,550,GraphicsContext::BLUE)); + boatScene.add(new Line(200,550,300,450,GraphicsContext::BLUE)); + boatScene.add(new Line(300,450,300,550,GraphicsContext::BLUE)); + boatScene.add(new Line(300,550,400,450,GraphicsContext::BLUE)); + boatScene.add(new Line(400,450,400,550,GraphicsContext::BLUE)); + boatScene.add(new Line(400,550,500,450,GraphicsContext::BLUE)); + boatScene.add(new Line(500,450,500,550,GraphicsContext::BLUE)); + boatScene.add(new Line(500,550,600,450,GraphicsContext::BLUE)); + boatScene.add(new Line(600,450,600,550,GraphicsContext::BLUE)); + boatScene.add(new Line(600,550,700,450,GraphicsContext::BLUE)); + boatScene.add(new Line(700,450,700,550,GraphicsContext::BLUE)); + boatScene.add(new Line(700,550,800,450,GraphicsContext::BLUE)); +} + int main(void){ GraphicsContext* gc = new X11Context(800,600,GraphicsContext::BLACK); gc->setColor(GraphicsContext::GREEN); MyDrawing md; + md.paint(gc); gc->runLoop(&md); diff --git a/Lab6/mydrawing.cpp b/Lab6/mydrawing.cpp index 141116a..fef0811 100644 --- a/Lab6/mydrawing.cpp +++ b/Lab6/mydrawing.cpp @@ -1,5 +1,4 @@ #include "mydrawing.h" -//#include using namespace std; MyDrawing::MyDrawing() @@ -123,4 +122,5 @@ void setLargePixel(GraphicsContext *gc, int x, int y) gc->setPixel(x - 1, y); gc->setPixel(x - 1, y - 1); gc->setPixel(x, y - 1); - gc->setPixel(x + 1, y - 1); \ No newline at end of file + gc->setPixel(x + 1, y - 1); +} \ No newline at end of file diff --git a/Lab6/mydrawing.h b/Lab6/mydrawing.h index 4559d50..e4b440f 100644 --- a/Lab6/mydrawing.h +++ b/Lab6/mydrawing.h @@ -3,9 +3,7 @@ #include #include "drawbase.h" -#include "gcontext.h" #include "image.h" -#include "matrix.h" using namespace std; class MyDrawing: public DrawingBase{ diff --git a/Lab6/shape.h b/Lab6/shape.h index 3090a65..d0cdd57 100644 --- a/Lab6/shape.h +++ b/Lab6/shape.h @@ -13,8 +13,7 @@ #include #include "x11context.h" #include "matrix.h" -#include "mydrawing.h" -//#include "viewcontext.h" +#include "viewcontext.h" using namespace std; diff --git a/Lab6/viewcontext.cpp b/Lab6/viewcontext.cpp index 7f192dd..c6f0f5d 100644 --- a/Lab6/viewcontext.cpp +++ b/Lab6/viewcontext.cpp @@ -1,11 +1,72 @@ // - ViewContext Class - +#include #include "viewcontext.h" -ViewContext::ViewContext(){ - compMatrix = new Matrix(3,3); +ViewContext::ViewContext(int width, int height){ + compMatrix = new Matrix(4,4); + prevTransform = new Matrix(4,4); } -void ViewContext::model_to_device(){ +void ViewContext::model_to_device(Matrix* coord){ + (*coord) = (*compMatrix)*(*coord); +} -} \ No newline at end of file +void ViewContext::rotate(int deg){ + // Store previous transform + *prevTransform = *compMatrix; + Matrix temp = Matrix::identity(4); + // Input transformation + temp[0][0] = cos(deg*M_PI/180); + temp[0][1] = sin(deg*M_PI/180); + temp[1][0] = -sin(deg*M_PI/180); + temp[1][0] = cos(deg*M_PI/180); + // Add transformation to comp matrix + *compMatrix = temp*(*compMatrix); +} + + // Scaling - Around center of screen +void ViewContext::scale(int mult){ + // Store previous transform + *prevTransform = *compMatrix; + // Create identity matrix + Matrix temp = Matrix::identity(4); + // Input transformation + temp[0][0] = mult; + temp[1][1] = mult; + temp[2][2] = mult; + // Add transformation to comp matrix + *compMatrix = temp*(*compMatrix); +} + +// Translation +void ViewContext::translate(int dx, int dy, int dz){ + // Store previous transform + *prevTransform = *compMatrix; + Matrix temp = Matrix::identity(4); + // Input transformation + temp[0][3] = dx; + temp[1][3] = dy; + temp[2][3] = dz; + // Add transformation to comp matrix + *compMatrix = temp*(*compMatrix); +} +// Reset +void ViewContext::reset(){ + *compMatrix = (Matrix::identity(4))*(*compMatrix); +} + +// Undo +void ViewContext::undo(Matrix* coord){ + // Use temp to store current trans + Matrix temp = Matrix::identity(4); + + (*coord) = (*prevTransform)*(*coord); + *prevTransform = temp; + +} + +// Invert Colors +// void ViewContext::invert(){ + +// } \ No newline at end of file diff --git a/Lab6/viewcontext.h b/Lab6/viewcontext.h index c43c7e2..34a4626 100644 --- a/Lab6/viewcontext.h +++ b/Lab6/viewcontext.h @@ -5,15 +5,41 @@ class ViewContext{ - ViewContext(); - void model_to_device(); + public: + // Contructor - takes in current window width and height to account for origin + ViewContext(int width, int height); + // Destructor + ~ViewContext(); + + void model_to_device(Matrix* coord); + // Rotation - Around center of screen + void rotate(int deg); + + // Scaling - Around center of screen + void scale(int mult); + + // Translation + void translate(int dx, int dy, int dz); - // Rotation - Around center of screen - // Scaling - Around center of screen - // Translation - // Reset - // Invert Colors - Matrix* compMatrix; + // Reset + void reset(); + + // Undo previous transform + void undo(Matrix* coord); + + // Invert Colors + //void invert(); + + private: + // Window pixel width + int width; + // Window pixel height + int height; + // Matrix containing the composite of all current transformations + Matrix* compMatrix; + // Matrix containing the previous transformation + Matrix* prevTransform; + }; #endif \ No newline at end of file