Skip to content

Commit cfba345

Browse files
committed
Measuring code
1 parent 141bb4f commit cfba345

File tree

1 file changed

+52
-23
lines changed

1 file changed

+52
-23
lines changed

main.cpp

+52-23
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,5 @@
11
#include "tgaimage.h"
2-
3-
/*
4-
# First attempt
5-
6-
The goal of the first lesson is to render the wire mesh. To do this, we should learn how to draw line segments.
7-
We can simply read what Bresenham’s line algorithm is, but let’s write code ourselves. How does the simplest code
8-
that draws a line segment between (x0, y0) and (x1, y1) points look like? Apparently, something like this:
9-
10-
void line(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
11-
for (float t=0.; t<1.; t+=.01) {
12-
int x = x0 + (x1-x0)*t;
13-
int y = y0 + (y1-y0)*t;
14-
image.set(x, y, color);
15-
}
16-
}
17-
*/
2+
#include <chrono>
183

194
int absolute(int value) {
205
if (value < 0) {
@@ -33,12 +18,59 @@ void swap(int &a, int &b) {
3318
b = c;
3419
}
3520

21+
// Usage:
22+
// auto start = std::chrono::high_resolution_clock::now();
23+
// measure_since(start);
24+
void measure_since(std::chrono::steady_clock::time_point start) {
25+
auto stop = std::chrono::high_resolution_clock::now();
26+
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start);
27+
long long ns = duration.count();
28+
printf("Measured %lldns\n", ns);
29+
}
30+
3631
const TGAColor white = TGAColor(255, 255, 255, 255);
3732
const TGAColor red = TGAColor(255, 0, 0, 255);
3833
const TGAColor green = TGAColor(0, 255, 0, 255);
3934
const TGAColor blue = TGAColor(0, 0, 255, 255);
4035

41-
void line(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
36+
#define line line1
37+
38+
// Copyed from the lesson to test performance, I win!!!
39+
void line2(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
40+
auto start = std::chrono::high_resolution_clock::now();
41+
bool steep = false;
42+
if (std::abs(x0-x1)<std::abs(y0-y1)) {
43+
std::swap(x0, y0);
44+
std::swap(x1, y1);
45+
steep = true;
46+
}
47+
if (x0>x1) {
48+
std::swap(x0, x1);
49+
std::swap(y0, y1);
50+
}
51+
int dx = x1-x0;
52+
int dy = y1-y0;
53+
float derror = std::abs(dy/float(dx));
54+
float error = 0;
55+
int y = y0;
56+
for (int x=x0; x<=x1; x++) {
57+
if (steep) {
58+
image.set(y, x, color);
59+
} else {
60+
image.set(x, y, color);
61+
}
62+
error += derror;
63+
if (error>.5) {
64+
y += (y1>y0?1:-1);
65+
error -= 1.;
66+
}
67+
}
68+
measure_since(start);
69+
}
70+
71+
void line1(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
72+
auto start = std::chrono::high_resolution_clock::now();
73+
4274
int differenceX = x1-x0;
4375
int differenceXAbs = absolute(differenceX);
4476

@@ -55,12 +87,10 @@ void line(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
5587

5688
double percentageOfLineDone = 0.0;
5789
double increment = 1.0 / (double) differenceXAbs;
58-
for (int pixel = x0; pixel != x1; ) {
59-
int x = pixel;
90+
for (int x = x0; x <= x1; x++) {
6091
int y = y0 + (y1-y0) * percentageOfLineDone;
6192
image.set(x, y, color);
6293
percentageOfLineDone += increment;
63-
pixel++;
6494
}
6595
}
6696
else {
@@ -73,15 +103,14 @@ void line(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
73103

74104
double percentageOfLineDone = 0.0;
75105
double increment = 1.0 / (double) differenceYAbs;
76-
for (int pixel = y0; pixel != y1; ) {
106+
for (int y = y0; y <= y1; y++) {
77107
int x = x0 + (x1-x0) * percentageOfLineDone;
78-
int y = pixel;
79108
image.set(x, y, color);
80109
percentageOfLineDone += increment;
81-
pixel++;
82110
}
83111
}
84112

113+
measure_since(start);
85114
image.set(x0, y0, green);
86115
image.set(x1, y1, blue);
87116
}

0 commit comments

Comments
 (0)