void Figure bresenham1 for int int x1 x2 y1 y2 int dx dy sx sy x1 vert

 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
void Figure::bresenham1() {
for (int i=0; i<N; i++ ) {
int x1,x2,y1,y2,f=0;
int dx,dy,sx,sy;
x1 = vertex[i].x;
y1 = vertex[i].y;
x2 = vertex[(i+1)==N ? 0 : i+1].x;
y2 = vertex[(i+1)==N ? 0 : i+1].y;
dx = x2-x1 < 0 ? -(x2-x1) : x2-x1;
dy = y2-y1 < 0 ? -(y2-y1) : y2-y1;
sx = x1 < x2 ? 1 : x1 == x2 ? 0 : -1;
sy = y1 < y2 ? 1 : y1 == y2 ? 0 : -1;
if (dy > dx) {
int t = dx;
dx = dy;
dy = t;
f=1;
}
int I,m,w;
I = 255;
m = 2*I*dy;
w = 2*dx*I - m;
int e = m / 2;
RGBA* row = image + (SCREEN_WIDTH ) * y1;
row[x1].r = /*color.r - I +*/( f==1 ?((w-e)<0 ? 0 : w-e)/(2*dx) : e / (2*dx));
row[x1].g = color.g;
row[x1].b = color.b;
row[x1].a = color.a;
for (int j = 0; j< dx; j++) {
if (e>=w) {
if (f) {
x1+=sx;
} else y1+=sy;
e-=w;
} else e+=m;
if (f) {
y1+=sy;
} else x1+=sx;
RGBA* row = image + (SCREEN_WIDTH ) * y1;
row[x1].r = /*color.r - I +*/( (f==1) != ((sx*sy)==-1) ? I - e/(2*dx) : e / (2*dx));
row[x1].g = color.g;
row[x1].b = color.b;
row[x1].a = color.a;
}
}
}