-
Notifications
You must be signed in to change notification settings - Fork 0
/
aaline.cpp
292 lines (242 loc) · 6.6 KB
/
aaline.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include <algorithm>
#include <cmath>
#include <SDL.h>
#include <stdio.h>
#include "aaline.h"
void Draw_Pixel(SDL_Surface* s, int x, int y,
uint8_t r, uint8_t g, uint8_t b, uint8_t a){
Uint8 c_r, c_g, c_b, c_a;
Uint8* buf8;
Uint32* buf32;
//In range?
if(x < 0 || y < 0 || x >= s->w || y >= s->h)
return;
switch(s->format->BytesPerPixel){
case 3:
buf8 = (Uint8*)s->pixels + y*s->pitch + x;
c_r = *(buf8+s->format->Rshift/8);
c_g = *(buf8+s->format->Gshift/8);
c_b = *(buf8+s->format->Bshift/8);
*(buf8+s->format->Rshift/8) = (r*a/255) + (c_r*(255-a)/255);
*(buf8+s->format->Gshift/8) = (g*a/255) + (c_g*(255-a)/255);
*(buf8+s->format->Bshift/8) = (b*a/255) + (c_b*(255-a)/255);
break;
case 4:
buf32 = (Uint32 *)s->pixels + y*s->pitch/4 + x;
SDL_GetRGBA(*buf32, s->format, &c_r, &c_g, &c_b, &c_a);
r = (r*a) / 255 + (c_r*(255-a)) /255;
g = (g*a) / 255 + (c_g*(255-a)) /255;
b = (b*a) / 255 + (c_b*(255-a)) /255;
*buf32 = SDL_MapRGBA(s->format, r, g, b, 255);
break;
default:
//Unsupported bit depth...
break;
}
return;
}
void Draw_AALine(SDL_Surface* screen, float x0, float y0,
float x1, float y1, float thick,
uint8_t r, uint8_t g, uint8_t b, uint8_t a){
float lower, upper;
float delta;
//Thickness less than screen diagonal
delta = sqrtf(screen->w*screen->w + screen->h*screen->h) + 1.0f;
if(thick > delta){
thick = delta;
}
//Cap ends off, X dimension
lower = 0.0f - thick;
upper = screen->w + thick;
x0 = std::min(upper, std::max(lower, x0));
x1 = std::min(upper, std::max(lower, x1));
//Cap ends off, Y dimension
lower = 0.0f - thick;
upper = screen->h + thick;
y0 = std::min(upper, std::max(lower, y0));
y1 = std::min(upper, std::max(lower, y1));
//Steeper than shallow?
if(abs(y1-y0) > abs(x1-x0)){
//Re-order to go bottom to top
if(y0 > y1){
std::swap(x0, x1);
std::swap(y0, y1);
}
//Adjust thickness by slope factor
thick = thick * sqrtf(float(x1-x0)*(x1-x0) + float(y1-y0)*(y1-y0)) / float(y1-y0);
//Calculate lower/upper/delta wrt X
delta = float(x1-x0) / float(y1-y0);
lower = x0 - thick*0.5f;
upper = x0 + thick*0.5f;
//Loop over each scanline, draw
for(int y=y0; y<=y1; y++){
int rl = int(lower);
int ru = int(upper);
float pl = 1.0f - (lower - rl);
float pu = upper - ru;
//Plot edge then interior pixels
Draw_Pixel(screen, rl, y, r, g, b, a*pl);
Draw_Pixel(screen, ru, y, r, g, b, a*pu);
for(int x=rl+1; x<ru; x++){
Draw_Pixel(screen, x, y, r, g, b, a);
}
//Update bounds
lower += delta;
upper += delta;
}
//Draw caps
if(x0 != x1){
float deltal, deltau;
//Reset bounds
lower = x1 - thick*0.5f;
upper = x1 + thick*0.5f;
//Choose which gets different delta
if(x0 < x1){
deltal = delta;
deltau = -1.0f / delta;
} else{
deltal = -1.0f / delta;
deltau = delta;
}
//Update bounds
lower += deltal;
upper += deltau;
//Draw end cap
for(int y=y1+1; lower<upper; y++){
int rl = int(lower);
int ru = int(upper);
float pl = 1.0f - (lower - rl);
float pu = upper - ru;
//Plot edge then interior pixels
Draw_Pixel(screen, rl, y, r, g, b, a*pl);
Draw_Pixel(screen, ru+1, y, r, g, b, a*pu);
for(int x=rl+1; x<=ru; x++){
Draw_Pixel(screen, x, y, r, g, b, a);
}
//Update bounds
lower += deltal;
upper += deltau;
}
//Reset bounds
lower = x0 - thick*0.5f;
upper = x0 + thick*0.5f;
//Update bounds
std::swap(deltal, deltau);
lower -= deltal;
upper -= deltau;
//Draw start cap
for(int y=y0-1; lower<upper; y--){
int rl = int(lower);
int ru = int(upper);
float pl = 1.0f - (lower - rl);
float pu = upper - ru;
//Plot edge then interior pixels
Draw_Pixel(screen, rl, y, r, g, b, a*pl);
Draw_Pixel(screen, ru+1, y, r, g, b, a*pu);
for(int x=rl+1; x<=ru; x++){
Draw_Pixel(screen, x, y, r, g, b, a);
}
//Update bounds
lower -= deltal;
upper -= deltau;
}
}
return;
}
//Must have been more shallow
//Reorder left to right
if(x0 > x1){
std::swap(x0, x1);
std::swap(y0, y1);
}
//Adjust thickness by slope factor
thick = thick * sqrtf(float(x1-x0)*(x1-x0) + float(y1-y0)*(y1-y0)) / float(x1-x0);
//Calculate lower/upper/delta wrt Y
delta = float(y1-y0) / float(x1-x0);
lower = y0 - thick*0.5f;
upper = y0 + thick*0.5f;
//Loop over each scanline, draw
for(int x=x0; x<=x1; x++){
int rl = int(lower);
int ru = int(upper);
float pl = 1.0f - (lower - rl);
float pu = upper - ru;
//Plot edge pixels
Draw_Pixel(screen, x, rl, r, g, b, a*pl);
Draw_Pixel(screen, x, ru+1, r, g, b, a*pu);
for(int y=rl+1; y<=ru; y++){
Draw_Pixel(screen, x, y, r, g, b, a);
}
//Update bounds
lower += delta;
upper += delta;
}
//Draw caps
if(y0 != y1){
float deltal, deltau;
//Reset bounds
lower = y1 - thick*0.5f;
upper = y1 + thick*0.5f;
//Choose which gets different delta
if(y0 < y1){
deltal = delta;
deltau = -1.0f / delta;
} else{
deltal = -1.0f / delta;
deltau = delta;
}
//Update bounds
lower += deltal;
upper += deltau;
//Draw end cap
for(int x=x1+1; lower<upper; x++){
int rl = int(lower);
int ru = int(upper);
float pl = 1.0f - (lower - rl);
float pu = upper - ru;
//Plot edge then interior pixels
Draw_Pixel(screen, x, rl, r, g, b, a*pl);
Draw_Pixel(screen, x, ru+1, r, g, b, a*pu);
for(int y=rl+1; y<=ru; y++){
Draw_Pixel(screen, x, y, r, g, b, a);
}
//Update bounds
lower += deltal;
upper += deltau;
}
//Reset bounds
lower = y0 - thick*0.5f;
upper = y0 + thick*0.5f;
//Update bounds
std::swap(deltal, deltau);
lower -= deltal;
upper -= deltau;
//Draw start cap
for(int x=x0-1; lower<upper; x--){
int rl = int(lower);
int ru = int(upper);
float pl = 1.0f - (lower - rl);
float pu = upper - ru;
//Plot edge then interior pixels
Draw_Pixel(screen, x, rl, r, g, b, a*pl);
Draw_Pixel(screen, x, ru+1, r, g, b, a*pu);
for(int y=rl+1; y<=ru; y++){
Draw_Pixel(screen, x, y, r, g, b, a);
}
//Update bounds
lower -= deltal;
upper -= deltau;
}
}
return;
}
void Draw_AALine(SDL_Surface* screen, float x0, float y0, float x1, float y1, float thick, uint32_t color){
Uint8 r, g, b;
SDL_GetRGB(color, screen->format, &r, &g, &b);
Draw_AALine(screen, x0+0.5f, y0+0.5f, x1+0.5f, y1+0.5f, thick, r, g, b, 0xFF);
}
void Draw_AALine(SDL_Surface* screen, float x0, float y0, float x1, float y1, uint32_t color){
Uint8 r, g, b;
SDL_GetRGB(color, screen->format, &r, &g, &b);
Draw_AALine(screen, x0+0.5f, y0+0.5f, x1+0.5f, y1+0.5f, 1.0f, r, g, b, 0xFF);
}