1
1
#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>
18
3
19
4
int absolute (int value) {
20
5
if (value < 0 ) {
@@ -33,12 +18,59 @@ void swap(int &a, int &b) {
33
18
b = c;
34
19
}
35
20
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
+
36
31
const TGAColor white = TGAColor(255 , 255 , 255 , 255 );
37
32
const TGAColor red = TGAColor(255 , 0 , 0 , 255 );
38
33
const TGAColor green = TGAColor(0 , 255 , 0 , 255 );
39
34
const TGAColor blue = TGAColor(0 , 0 , 255 , 255 );
40
35
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
+
42
74
int differenceX = x1-x0;
43
75
int differenceXAbs = absolute (differenceX);
44
76
@@ -55,12 +87,10 @@ void line(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
55
87
56
88
double percentageOfLineDone = 0.0 ;
57
89
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++) {
60
91
int y = y0 + (y1 -y0 ) * percentageOfLineDone;
61
92
image.set (x, y, color);
62
93
percentageOfLineDone += increment;
63
- pixel++;
64
94
}
65
95
}
66
96
else {
@@ -73,15 +103,14 @@ void line(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
73
103
74
104
double percentageOfLineDone = 0.0 ;
75
105
double increment = 1.0 / (double ) differenceYAbs;
76
- for (int pixel = y0 ; pixel ! = y1 ; ) {
106
+ for (int y = y0 ; y < = y1 ; y++ ) {
77
107
int x = x0 + (x1-x0) * percentageOfLineDone;
78
- int y = pixel;
79
108
image.set (x, y, color);
80
109
percentageOfLineDone += increment;
81
- pixel++;
82
110
}
83
111
}
84
112
113
+ measure_since (start);
85
114
image.set (x0, y0 , green);
86
115
image.set (x1, y1 , blue);
87
116
}
0 commit comments