Skip to content

Commit

Permalink
More measures against 5th and the improved version in issue ssloy#28
Browse files Browse the repository at this point in the history
Somehow my version seems to still win :D gotta test the wireframe test
  • Loading branch information
OAguinagalde committed Feb 27, 2022
1 parent cfba345 commit b70f58d
Showing 1 changed file with 92 additions and 6 deletions.
98 changes: 92 additions & 6 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ const TGAColor red = TGAColor(255, 0, 0, 255);
const TGAColor green = TGAColor(0, 255, 0, 255);
const TGAColor blue = TGAColor(0, 0, 255, 255);

#define line line1

// Copyed from the lesson to test performance, I win!!!
void line2(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
void line4th(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
auto start = std::chrono::high_resolution_clock::now();
bool steep = false;
if (std::abs(x0-x1)<std::abs(y0-y1)) {
Expand Down Expand Up @@ -66,6 +64,80 @@ void line2(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
}
}
measure_since(start);
}

void line5th(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
auto start = std::chrono::high_resolution_clock::now();

bool steep = false;
if (std::abs(x0-x1)<std::abs(y0-y1)) {
std::swap(x0, y0);
std::swap(x1, y1);
steep = true;
}
if (x0>x1) {
std::swap(x0, x1);
std::swap(y0, y1);
}
int dx = x1-x0;
int dy = y1-y0;
int derror2 = std::abs(dy)*2;
int error2 = 0;
int y = y0;
for (int x=x0; x<=x1; x++) {
if (steep) {
image.set(y, x, color);
} else {
image.set(x, y, color);
}
error2 += derror2;
if (error2 > dx) {
y += (y1>y0?1:-1);
error2 -= dx*2;
}
}
measure_since(start);
}

void line5thImprovedIssue28(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
auto start = std::chrono::high_resolution_clock::now();

bool steep = false;
if (std::abs(x0-x1)<std::abs(y0-y1)) {
std::swap(x0, y0);
std::swap(x1, y1);
steep = true;
}
if (x0>x1) {
std::swap(x0, x1);
std::swap(y0, y1);
}
int dx = x1-x0;
int dy = y1-y0;
int derror2 = std::abs(dy)*2;
int error2 = 0;
int y = y0;
const int yincr = (y1>y0? 1 : -1);
if(steep) {
for(int x = x0; x<=x1; ++x) {
image.set(y, x, color);
error2 += derror2;
if(error2 > dx) {
y += (y1>y0? 1 : -1);
error2 -= dx*2;
}
}
} else {
for(int x = x0; x<=x1; ++x) {
image.set(x, y, color);
error2 += derror2;
if(error2 > dx) {
y += yincr;
error2 -= dx*2;
}
}
}
measure_since(start);
}

void line1(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
Expand Down Expand Up @@ -117,9 +189,23 @@ void line1(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {

int main(int argc, char** argv) {
TGAImage image(100, 100, TGAImage::RGB);
line(13, 20, 80, 40, image, white);
line(20, 13, 40, 80, image, red);
line(80, 40, 13, 20, image, red);

line4th(13, 20, 80, 40, image, white);
line4th(20, 13, 40, 80, image, red);
line4th(80, 40, 13, 20, image, red);

line5th(13, 20, 80, 40, image, white);
line5th(20, 13, 40, 80, image, red);
line5th(80, 40, 13, 20, image, red);

line5thImprovedIssue28(13, 20, 80, 40, image, white);
line5thImprovedIssue28(20, 13, 40, 80, image, red);
line5thImprovedIssue28(80, 40, 13, 20, image, red);

line1(13, 20, 80, 40, image, white);
line1(20, 13, 40, 80, image, red);
line1(80, 40, 13, 20, image, red);

image.flip_vertically(); // i want to have the origin at the left bottom corner of the image
image.write_tga_file("output.tga");
return 0;
Expand Down

0 comments on commit b70f58d

Please sign in to comment.