@@ -33,10 +33,8 @@ const TGAColor red = TGAColor(255, 0, 0, 255);
33
33
const TGAColor green = TGAColor(0 , 255 , 0 , 255 );
34
34
const TGAColor blue = TGAColor(0 , 0 , 255 , 255 );
35
35
36
- #define line line1
37
-
38
36
// Copyed from the lesson to test performance, I win!!!
39
- void line2 (int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
37
+ void line4th (int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
40
38
auto start = std::chrono::high_resolution_clock::now ();
41
39
bool steep = false ;
42
40
if (std::abs (x0-x1)<std::abs (y0 -y1 )) {
@@ -66,6 +64,80 @@ void line2(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
66
64
}
67
65
}
68
66
measure_since (start);
67
+ }
68
+
69
+ void line5th (int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
70
+ auto start = std::chrono::high_resolution_clock::now ();
71
+
72
+ bool steep = false ;
73
+ if (std::abs (x0-x1)<std::abs (y0 -y1 )) {
74
+ std::swap (x0, y0 );
75
+ std::swap (x1, y1 );
76
+ steep = true ;
77
+ }
78
+ if (x0>x1) {
79
+ std::swap (x0, x1);
80
+ std::swap (y0 , y1 );
81
+ }
82
+ int dx = x1-x0;
83
+ int dy = y1 -y0 ;
84
+ int derror2 = std::abs (dy)*2 ;
85
+ int error2 = 0 ;
86
+ int y = y0 ;
87
+ for (int x=x0; x<=x1; x++) {
88
+ if (steep) {
89
+ image.set (y, x, color);
90
+ } else {
91
+ image.set (x, y, color);
92
+ }
93
+ error2 += derror2;
94
+ if (error2 > dx) {
95
+ y += (y1 >y0 ?1 :-1 );
96
+ error2 -= dx*2 ;
97
+ }
98
+ }
99
+ measure_since (start);
100
+ }
101
+
102
+ void line5thImprovedIssue28 (int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
103
+ auto start = std::chrono::high_resolution_clock::now ();
104
+
105
+ bool steep = false ;
106
+ if (std::abs (x0-x1)<std::abs (y0 -y1 )) {
107
+ std::swap (x0, y0 );
108
+ std::swap (x1, y1 );
109
+ steep = true ;
110
+ }
111
+ if (x0>x1) {
112
+ std::swap (x0, x1);
113
+ std::swap (y0 , y1 );
114
+ }
115
+ int dx = x1-x0;
116
+ int dy = y1 -y0 ;
117
+ int derror2 = std::abs (dy)*2 ;
118
+ int error2 = 0 ;
119
+ int y = y0 ;
120
+ const int yincr = (y1 >y0 ? 1 : -1 );
121
+ if (steep) {
122
+ for (int x = x0; x<=x1; ++x) {
123
+ image.set (y, x, color);
124
+ error2 += derror2;
125
+ if (error2 > dx) {
126
+ y += (y1 >y0 ? 1 : -1 );
127
+ error2 -= dx*2 ;
128
+ }
129
+ }
130
+ } else {
131
+ for (int x = x0; x<=x1; ++x) {
132
+ image.set (x, y, color);
133
+ error2 += derror2;
134
+ if (error2 > dx) {
135
+ y += yincr;
136
+ error2 -= dx*2 ;
137
+ }
138
+ }
139
+ }
140
+ measure_since (start);
69
141
}
70
142
71
143
void line1 (int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
@@ -117,9 +189,23 @@ void line1(int x0, int y0, int x1, int y1, TGAImage &image, TGAColor color) {
117
189
118
190
int main (int argc, char ** argv) {
119
191
TGAImage image (100 , 100 , TGAImage::RGB);
120
- line (13 , 20 , 80 , 40 , image, white);
121
- line (20 , 13 , 40 , 80 , image, red);
122
- line (80 , 40 , 13 , 20 , image, red);
192
+
193
+ line4th (13 , 20 , 80 , 40 , image, white);
194
+ line4th (20 , 13 , 40 , 80 , image, red);
195
+ line4th (80 , 40 , 13 , 20 , image, red);
196
+
197
+ line5th (13 , 20 , 80 , 40 , image, white);
198
+ line5th (20 , 13 , 40 , 80 , image, red);
199
+ line5th (80 , 40 , 13 , 20 , image, red);
200
+
201
+ line5thImprovedIssue28 (13 , 20 , 80 , 40 , image, white);
202
+ line5thImprovedIssue28 (20 , 13 , 40 , 80 , image, red);
203
+ line5thImprovedIssue28 (80 , 40 , 13 , 20 , image, red);
204
+
205
+ line1 (13 , 20 , 80 , 40 , image, white);
206
+ line1 (20 , 13 , 40 , 80 , image, red);
207
+ line1 (80 , 40 , 13 , 20 , image, red);
208
+
123
209
image.flip_vertically (); // i want to have the origin at the left bottom corner of the image
124
210
image.write_tga_file (" output.tga" );
125
211
return 0 ;
0 commit comments