-
Notifications
You must be signed in to change notification settings - Fork 0
/
wu_line+antialiasing.cpp
155 lines (141 loc) · 3.72 KB
/
wu_line+antialiasing.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
/***********************************************************************
This code is generated by the AlgoPascal translator
This code is distributed under the ALGLIB license
(see http://www.alglib.net/copyrules.php for details)
***********************************************************************/
#include "ap.h"
/*-----------------------------------------------
This routines must be defined by the programmer:
void setpixel(int x, int y, double alpha);
-----------------------------------------------*/
void drawwuline(double x1, double y1, double x2, double y2);
double myfrac(double x);
/*************************************************************************
<<<<<<< HEAD
РиÑование линии Ñ Ð¸Ñпользованием метода Ву длÑ
антиалиаÑинга.
Кординаты начала и конца могут быть дробными.
=======
Ðèñîâàíèå ëèíèè ñ èñïîëüçîâàíèåì ìåòîäà Âó äëÿ
àíòèàëèàñèíãà.
Êîðäèíàòû íà÷àëà è êîíöà ìîãóò áûòü äðîáíûìè.
>>>>>>> 58e3d87... Added Wu's antialiasing algorithms for circle, ellipsis, line
*************************************************************************/
void drawwuline(double x1, double y1, double x2, double y2)
{
double grad;
double xd;
double yd;
double length;
double xm;
double ym;
double xgap;
double ygap;
double xend;
double yend;
double xf;
double yf;
double brightness1;
double brightness2;
int x;
int y;
int ix1;
int ix2;
int iy1;
int iy2;
bool wasexchange;
int tmpint;
double tmpreal;
xd = x2-x1;
yd = y2-y1;
if( xd==0&&yd==0 )
{
return;
}
if( fabs(xd)>fabs(yd) )
{
wasexchange = false;
}
else
{
wasexchange = true;
tmpreal = x1;
x1 = y1;
y1 = tmpreal;
tmpreal = x2;
x2 = y2;
y2 = tmpreal;
tmpreal = xd;
xd = yd;
yd = tmpreal;
}
if( x1>x2 )
{
tmpreal = x1;
x1 = x2;
x2 = tmpreal;
tmpreal = y1;
y1 = y2;
y2 = tmpreal;
xd = x2-x1;
yd = y2-y1;
}
grad = yd/xd;
xend = ap::ifloor(x1+0.5);
yend = y1+grad*(xend-x1);
xgap = 1-myfrac(x1+0.5);
ix1 = ap::ifloor(x1+0.5);
iy1 = ap::ifloor(yend);
brightness1 = (1-myfrac(yend))*xgap;
brightness2 = myfrac(yend)*xgap;
if( wasexchange )
{
setpixel(iy1, ix1, brightness1);
setpixel(iy1+1, ix1, brightness2);
}
else
{
setpixel(ix1, iy1, brightness1);
setpixel(ix1, iy1+1, brightness2);
}
yf = yend+grad;
xend = ap::ifloor(x2+0.5);
yend = y2+grad*(xend-x2);
xgap = 1-myfrac(x2-0.5);
ix2 = ap::ifloor(x2+0.5);
iy2 = ap::ifloor(yend);
brightness1 = (1-myfrac(yend))*xgap;
brightness2 = myfrac(yend)*xgap;
if( wasexchange )
{
setpixel(iy2, ix2, brightness1);
setpixel(iy2+1, ix2, brightness2);
}
else
{
setpixel(ix2, iy2, brightness1);
setpixel(ix2, iy2+1, brightness2);
}
for(x = ix1+1; x <= ix2-1; x++)
{
brightness1 = 1-myfrac(yf);
brightness2 = myfrac(yf);
if( wasexchange )
{
setpixel(ap::ifloor(yf), x, brightness1);
setpixel(ap::ifloor(yf)+1, x, brightness2);
}
else
{
setpixel(x, ap::ifloor(yf), brightness1);
setpixel(x, ap::ifloor(yf)+1, brightness2);
}
yf = yf+grad;
}
}
double myfrac(double x)
{
double result;
result = x-ap::ifloor(x);
return result;
}