lab4 submitted
This commit is contained in:
@@ -1,6 +1,20 @@
|
|||||||
|
/**
|
||||||
|
* @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 "x11context.h"
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
@@ -11,7 +25,7 @@ int main(void)
|
|||||||
gc->setColor(GraphicsContext::GREEN);
|
gc->setColor(GraphicsContext::GREEN);
|
||||||
gc->setPixel(10,10);
|
gc->setPixel(10,10);
|
||||||
gc->setPixel(30,30);
|
gc->setPixel(30,30);
|
||||||
gc->drawLine(100,100,500,500);
|
gc->drawLine(100,100,100,500);
|
||||||
gc->setColor(GraphicsContext::RED);
|
gc->setColor(GraphicsContext::RED);
|
||||||
gc->drawLine(100,500,500,500);
|
gc->drawLine(100,500,500,500);
|
||||||
gc->setColor(GraphicsContext::BLUE);
|
gc->setColor(GraphicsContext::BLUE);
|
||||||
@@ -20,7 +34,67 @@ int main(void)
|
|||||||
gc->drawLine(500,100,100,100);
|
gc->drawLine(500,100,100,100);
|
||||||
gc->setColor(GraphicsContext::MAGENTA);
|
gc->setColor(GraphicsContext::MAGENTA);
|
||||||
gc->drawCircle(300,300,200);
|
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;
|
delete gc;
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,9 @@
|
|||||||
#include "x11context.h"
|
#include "x11context.h"
|
||||||
#include "drawbase.h"
|
#include "drawbase.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
/**
|
/**
|
||||||
* The only constructor provided. Allows size of window and background
|
* The only constructor provided. Allows size of window and background
|
||||||
* color be specified.
|
* color be specified.
|
||||||
@@ -206,28 +208,73 @@ int X11Context::getWindowHeight()
|
|||||||
|
|
||||||
// Bresenham Implementation
|
// Bresenham Implementation
|
||||||
void X11Context::drawLine(int x1, int y1, int x2, int y2){
|
void X11Context::drawLine(int x1, int y1, int x2, int y2){
|
||||||
//setPixel(x1,y1);
|
bool steep;
|
||||||
// Check direction of line
|
|
||||||
if(abs(y2-y1) < abs(x2-x1)){
|
if(abs(y2-y1) < abs(x2-x1)){
|
||||||
|
// Steep slope
|
||||||
|
steep = true;
|
||||||
if(x1 > x2){
|
if(x1 > x2){
|
||||||
//swap
|
// Swap to change drawing direction
|
||||||
} else {
|
int temp = x1;
|
||||||
//swap
|
x1 = x2;
|
||||||
|
x2 = temp;
|
||||||
|
temp = y1;
|
||||||
|
y1 = y2;
|
||||||
|
y2 = temp;
|
||||||
}
|
}
|
||||||
} else {
|
} 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 dx = x2-x1;
|
||||||
int dy = y2-y1;
|
int dy = y2-y1;
|
||||||
int D = 2*dy-dx;
|
int D = 2*dy-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;
|
int y = y1;
|
||||||
for(int i = x1; i < x2; i++){
|
for(int i = x1; i < x2; i++){
|
||||||
setPixel(i,y);
|
setPixel(i,y);
|
||||||
if(D > 0){
|
if(D > 0){
|
||||||
y = y+1;
|
y += c;
|
||||||
D = D-2*dx;
|
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ class X11Context : public GraphicsContext
|
|||||||
void setPixel(int x, int y);
|
void setPixel(int x, int y);
|
||||||
unsigned int getPixel(int x, int y);
|
unsigned int getPixel(int x, int y);
|
||||||
void clear();
|
void clear();
|
||||||
//void drawLine(int x1, int y1, int x2, int y2);
|
|
||||||
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);
|
void drawCircle(int x, int y, unsigned int radius);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user