diff --git a/Lab4/Lab4 b/Lab4/Lab4 deleted file mode 100755 index 4f3e1aa..0000000 Binary files a/Lab4/Lab4 and /dev/null differ diff --git a/Lab4/main.cpp b/Lab4/main.cpp index d7a712a..82d2d03 100644 --- a/Lab4/main.cpp +++ b/Lab4/main.cpp @@ -1,7 +1,21 @@ +/** + * @file main.cpp + * @author Trevor Barnes (barnestr@msoe.edu) + * @brief Driver for testing the changes made to x11context.cpp + * 5000 Lines Test Results: + * - X11 Default Implementation: 0.83 Seconds + * - Bresenham Implementation: 29.71 Seconds + * @version 1.0 + * @date 2022-04-05 + * + * @copyright Copyright (c) 2022 + * + */ #include "x11context.h" #include #include - +using namespace std; + int main(void) { GraphicsContext* gc = new X11Context(800,600,GraphicsContext::BLACK); @@ -11,7 +25,7 @@ int main(void) gc->setColor(GraphicsContext::GREEN); gc->setPixel(10,10); gc->setPixel(30,30); - gc->drawLine(100,100,500,500); + gc->drawLine(100,100,100,500); gc->setColor(GraphicsContext::RED); gc->drawLine(100,500,500,500); gc->setColor(GraphicsContext::BLUE); @@ -20,7 +34,67 @@ int main(void) gc->drawLine(500,100,100,100); gc->setColor(GraphicsContext::MAGENTA); gc->drawCircle(300,300,200); - sleep(10); + sleep(2); + gc->clear(); + + // Random/Negatives Test + gc-> setColor(rand()); + gc->drawLine(-100, 5000, 200, -590); + gc-> setColor(rand()); + gc->drawLine(rand()%800,rand()%600,rand()%800,rand()%600); + gc-> setColor(rand()); + gc->drawLine(rand()%800,rand()%600,rand()%800,rand()%600); + gc-> setColor(rand()); + gc->drawLine(rand()%800,rand()%600,rand()%800,rand()%600); + sleep(5); + gc->clear(); + // End Random/Negatives Test + + // 5000 lines test + // gc->setColor(GraphicsContext::GREEN); + // int total = 0; + // while(total < 5000){ + // for(int i = 0; (i < 800) && (total < 5000); i++){ + // gc->drawLine(i,0,i,600); + // total++; + // } + // gc-> setColor(rand()); + // cout << total << endl; + // } + // cout << total << endl; + // gc->clear(); + // End 5000 lines test + + // Draw boat + gc->setColor(GraphicsContext::YELLOW); + gc->drawLine(250,500,550,500); + gc->drawLine(250,500,150,400); + gc->drawLine(150,400,650,400); + gc->drawLine(650,400,550,500); + gc->drawLine(400,400,400,100); + + gc->drawLine(400,100,300,150); + gc->drawLine(300,150,400,200); + + // Draw waves + gc->setColor(GraphicsContext::BLUE); + gc->drawLine(0,550,100,450); + gc->drawLine(100,450,100,550); + gc->drawLine(100,550,200,450); + gc->drawLine(200,450,200,550); + gc->drawLine(200,550,300,450); + gc->drawLine(300,450,300,550); + gc->drawLine(300,550,400,450); + gc->drawLine(400,450,400,550); + gc->drawLine(400,550,500,450); + gc->drawLine(500,450,500,550); + gc->drawLine(500,550,600,450); + gc->drawLine(600,450,600,550); + gc->drawLine(600,550,700,450); + gc->drawLine(700,450,700,550); + gc->drawLine(700,550,800,450); + + sleep(5); delete gc; diff --git a/Lab4/x11context.cpp b/Lab4/x11context.cpp index bc8f0d1..cb50144 100644 --- a/Lab4/x11context.cpp +++ b/Lab4/x11context.cpp @@ -9,7 +9,9 @@ #include "x11context.h" #include "drawbase.h" #include +#include +using namespace std; /** * The only constructor provided. Allows size of window and background * color be specified. @@ -203,31 +205,76 @@ int X11Context::getWindowHeight() // XDrawLine(display, window, graphics_context, x1, y1, x2, y2); // XFlush(display); // } - + // Bresenham Implementation void X11Context::drawLine(int x1, int y1, int x2, int y2){ - //setPixel(x1,y1); - // Check direction of line + bool steep; if(abs(y2-y1) < abs(x2-x1)){ + // Steep slope + steep = true; if(x1 > x2){ - //swap - } else { - //swap + // Swap to change drawing direction + int temp = x1; + x1 = x2; + x2 = temp; + temp = y1; + y1 = y2; + y2 = temp; } } else { - + // Shallow slope + steep = false; + if(y1 > y2){ + // Swap to change drawing direction + int temp = x1; + x1 = x2; + x2 = temp; + temp = y1; + y1 = y2; + y2 = temp; + } } + // Initilize algorithm values int dx = x2-x1; int dy = y2-y1; int D = 2*dy-dx; - int y = y1; - for(int i = x1; i < x2; i++){ - setPixel(i,y); - if(D > 0){ - y = y+1; - D = D-2*dx; + // Will either be +1 or -1 + int c = 1; + + if(steep){ + // When |dx| > |dy| + if(dy<0){ + c = -1; + dy = -dy; + } + int D = 2*dy-dx; + int y = y1; + for(int i = x1; i < x2; i++){ + setPixel(i,y); + if(D > 0){ + y += c; + D += (2*(dy-dx)); + }else{ + D += 2*dy; + } + } + } else if(!steep){ + // When |dx| < |dy| + if(dx<0){ + c = -1; + dx = -dx; + } + int D = 2*dx-dy; + int x = x1; + for(int j = y1; j < y2; j++){ + setPixel(x,j); + if(D > 0){ + x += c; + D += (2*(dx-dy)); + } else { + D += 2*dx; + } } - D = D+2*dy; } } diff --git a/Lab4/x11context.h b/Lab4/x11context.h index 54a7af1..cec63f4 100644 --- a/Lab4/x11context.h +++ b/Lab4/x11context.h @@ -23,7 +23,6 @@ class X11Context : public GraphicsContext void setPixel(int x, int y); unsigned int getPixel(int x, int y); void clear(); - //void drawLine(int x1, int y1, int x2, int y2); void drawLine(int x1, int y1, int x2, int y2); void drawCircle(int x, int y, unsigned int radius);