Компьютерная графика 1

advertisement
1.
§1.1.
.
,
,
,
,
.
380
760
.
,
.
,
,
,
.
80%
,
3%.
.
,
.
,
,
,
.
,
,
.
.
.
,
.
,
,
,
.
(
,
.
.)
(
7
.
130
.).
,
,
,
.
,
,
,
.
,
.
,
»
.
400
500
600
),
»
»
)
).
500
600
700
(
(
(
,
,
.
1853 .
3
.
:
1.
.
,
.
,
,
.
:
,
,
.
2.
4
,
: cC
rR
gG bB ,
,
c1C
c2 C
(r1
r2 ) R ( g1
g 2 )G (b1
3.
c, r , g , b
0.
:
b2 ) B .
,
,
,
.
5
§1.2.
1.2.1.
RGB
,
,
(R),
0
RGB,
(B).
(G)
.
(0,0,0),
1,
(
)
(1,0,1),
(1,1,1).
,
,
(
. 1.1.
,
RGB.
RGB
. 1.1).
RGB
,
.
,
1920,
1930-
.
,
R, G
.
B
,
,
(
),
.
,
: cC
rR
gG bB ,
cC
rR
2-
gG bB .
,
, ,
,
.
,
(
)
.
RGB,
. 1.2.
6
RGB
. 1.2
.
RGB:
1.2.2.
CIE XYZ
CIE
CIE XYZ
1931
. CIE Commission Internationale de
x( ) , y( ) , z( ) ,
(X, Y
( . 1.3).
CIE (
l'Eclairage).
,
Z)
. 1.3.
CIE
XYZ
CIE 1931
,
,
,
,
.
X, Y, Z
,
(
. 1.4).
. 1.4.
CIE XYZ
(
Y
Z
x, y, z),
:
x
y
z
X
X Y
Y
X Y
Z
X Y
Z
Z
Z
,
,
.
,
x, y, z.
X,
x+y+z=1
,
7
.
CIE.
Y
(
xyY).
RGB
CIE XYZ:
(x, y, Y),
,
Y
x
y
Y
X
Y
Z
,
.
Y
(1 x
y
y)
RGB(xr, yr, Yr), (xg, yg, Yg), (xb, yb, Yb),
zr=1-xr-yr, zg=1-xg-yg, zb=1-xb-yb
:
Yr
xr
yr
Yr
Yr
zr
yr
X
Y
Z
Yg
xg
yg
Yg
Yg
zg
yg
Yb
xb
yb
Yb
Yb
zb
yb
R
G .
B
R, G, B
(
[0; 255]).
.
[0; 1],
255,
CIE XYZ
RGB
,
.
,
(
,
X+Y+Z=1
).
Oxy
CIE (
. 1.5.
.
,
CIE
,
.
,
.
,
R, G, B (
CIE)
.
8
. 1.5).
,
.
,
.
CIE XYZ
,
( x, y , z )
1 1 1
( , , ).
3 3 3
,
,
.
,
. 1.5
,
.
XYZ
:
1)
.
(
),
.
RGB
.
2)
.
x, y, z
.
1.2.3.
L*u*v*
,
L*a*b*
,
,
.
CIE
1976
L*u*v*. L*
,
.
,
,
.
.
u*
L*, u*, v*:
v*
L* [0; 100];
u* [-200; 200], v* [-200; 200];
u*
u*);
v*
v*);
u* 0, v* 0,
L*,
(grayscale).
(L2*, u2 *, v2 *).
(L1*, u1*, v1 *)
,
L*u*v*
?
D
:
( L*1
L*2 ) 2
(u1*
u 2* ) 2
(v1*
v 2* ) 2 .
D>5
,
D>10
.
.
(
,
.
L*a*b*,
CIE XYZ
F(s)
).
L*u*v*:
s1 / 3 , s
F ( s)
1 29 2
( )
3 6
6 3
) ;
29
4
s
, 0
29
(
s
(
6 3
) .
29
9
u
L*
116 F (
4X
X 15Y
CIE XYZ
3Z
Y
) 16, u *
Yw
L*u*v*
13L* (u
u
Y
F 1(
v*
13L*
uw , v
L* 16
) Yw , X
116
L*a*b*
L*a*b*
( ,
0
100,
13L* (v
vw ) .
vw ,
9Y u
4 X 15v Y
, Z
4v
3u
CIE 1976
.
v X
.
L* (
),
)
116 F (
b*.
)
(
CIE XYZ
Y
) 16, a *
Yw
L*a*b*
(
),
—
).
L*a*b*:
F(s),
:
X
Y
500 [ F (
) F ( )], v *
Xw
Yw
L*u*v*,
200 [ F (
CIE XYZ:
L* 16
,
116
fy
10
u w ), v *
).
a*
L*
X
9Y
.
15Y 3Z
(Xw, Yw, Zw).
CIE XYZ:
*
u
13L*
, v
fx
fy
a*
,
500
fz
6
;
29
16
6
) 3 ( ) 2 Yw , 0
116
29
Y
Yw f y3 ,
Y
( fy
X
X w f x3 ,
X
( fx
Z
Z w f z3 ,
Z
( fz
fy
b*
.
200
fy
6
.
29
fx
6
.
29
fz
6
.
29
fy
6
;
29
16
6
) 3 ( )2 X w, 0
116
29
fx
6
;
29
16
6
) 3 ( )2 Z w , 0
116
29
fz
Y
Z
) F ( )] .
Yw
Zw
1.2.4.
YUV, YCbCr, YPbPr, YIQ
.
Y
CIE XYZ
.
,
,
,
Y.
.
YUV
[0; 1],
RGB-
PAL.
U, V
YUV
. 1.6. RGBY, U V
U
YUV
0, 299 R 0,587G 0,114 B;
U
0, 492 ( B Y )
V
0,877 ( R Y )
YPbPr
0,615 R 0,515G 0,100 B.
YUV
.
Kr.
RGB
:
Y
Kb
Kr
K r R (1 K r
Kb ) G
Pb
1
1
( B Y );
2 1 Kb
Pb
1
1
( R Y ).
2 1 Kr
,
,
YUV, Kb=0,114; Kr=0,299.
Pb , Pr
UV-
0,147 R 0, 289G 0, 436 B;
YCbCr YPbPr
Cb/Pb Cr/Pr). YPbPr
), YCbCr : Kb
(
. 1.6
RGB:
Y
V (
[-0,5; 0,5].
Y=0,5.
UV-
K b B;
RGB).
(
,
Kb=0,0722; Kr=0,2126,
.
: Y [0,1] ,
R, G, B [0;1]
,
[ 0,5; 0.5] .
,
:
Y
[min Y ; max Y ] , C b , C r
[min C ; max C ] ,
min Y , max Y , min C , max C
N
0.
11
RGB YCbCr
Kg
Y
1 Kr
min Y
Cb
Cr
:
Kb ;
(max Y
min C
min Y ) ( K r R
max C
max C
2
min C
max C
2
K b B);
min C
1
( Kr R
1 Kb
min C
1
((1 K r ) R
1 Kr
2
max C
Kg G
2
K g G (1 K b ) B);
Kg G
K b B).
minY=16, maxY=235, minC=16, maxC=240.
JPEG ( . 5.4.3)
8minY=0, maxY=255, minC=0, maxC=255.
YIQ
NTSC.
YUV,
YUV YIQ
UV=IQ
33 .
RGB
:
Y
0, 299 R 0,587G 0,114 B;
U
0,735 ( R Y ) 0,268 ( B Y )
0,596 R 0,274G 0,321B;
V
0,478 ( R Y ) 0,413 ( B Y )
0,211R 0,523G 0,311B.
:
.
1.2.5.
HSV HLS
HSV (
. ue, Saturation, Value ,
.
,H(
Oz), V -
,
. 1.7.
,
,
,
,
)
Ox, S (
Oz).
(
. 1.7).
HSV
,
,
).
(
)
,
,
.
(
,
.
,
.
12
,
S=0(
),
,
)
S
H
.
HLS (
. ue, Lightness, Saturation –
HSV.
,H(
Oz), L -
)
(
. 1.8).
(
.
. 1.8.
. 1.8,
H S
.
HLS
HSV
,
L
,
L=0,5; S=1)
(
(
,
Ox, S Oz).
. 1.8
V.
,
H(
,
,
,
HSV)
,
S = 0))
H
S.
§1.3.
(
(
,
)
,
,
.
.
,
(
,
,
, RGB),
.
,
1.3.1.
.
CMY
CMY
RGB:
.
RGB
CMY
,
,
,
(
. C – Cyan, M – Magenta, Y - Yellow),
,
.
CMY
:
C 1 R;
M 1 G;
Y 1 B.
(
(1, 1, 1)RGB = (0, 0, 0)CMY
(0, 0, 0)RGB = (1, 1, 1)CMY.
C, M
RGB)
Y
13
,
,
.
.
1.3.2.
CMYK
,"
CMY (
"
),
,
,
,
,
"
.
"
(
30
,
),
(1, 1, 1)CMY
,
.
,
.
.
:
CMY
.
;
(
CMY):
K min (C , M , Y );
C C K;
M M K;
Y Y K.
CMYK
(
. Black)
Blue
,
,
RGB.
,
,
,
,
RGB
CMY(K).
(
,
,
,
,
)
+
+
+
+
+
,
)
:
=
;
=
;
;
=
+
=
;
+
=
,
.
,
(
)
?
,
.
,
.
,
,
,
.
,
(
,
:
.
,
14
.
,
,
.
)
2.
§2.1.
,
,
,
(
).
,
,
.
.
"
",
,
,
(
. 2.1 ),
(
. 2.1 –
. 2.1 ).
. 2.1 –
4-
,
.
,
,
45°
.
,
.
.
,
,
.
,
.
,
,
,
.
,
F ( x, y )
(
).
dy
dx
0,
,
g ( x, y ) ,
:
y
x
g ( x, y )
,
i-
,
(xi,yi)
,
,
:
xi
1
xi
x,
yi
1
yi
y;
:
y ( y
1),
x
y > x,
y
x
g ( x, y ) ;
15
,
y
x,
y
x=1.
.
,
F ( x, y )
,
F (x
0.
,
(x,y)
x, y ) , F ( x, y
.
y)
)
F (x
,
x, y
i-
y) ,
x= 1, y= 1.
,
.
,
.
§2.2.
2.2.1.
(x1,y1)
y
x
(x2,y2).
y2
y 1,
,
x
x2
y2
y1
y2
x2
x2
y1
x1
const ,
x1 , ,
x1
,
y1
.
.
,
,
2.1.
,
,
.
2.1
procedure DrawLineDDA(x1, y1, x2, y2: integer);
var x, y, dx, dy: double;
begin
dx:= 1;
dy:= Abs(y2 - y1) / Abs(x2 - x1);
x:= x1;
y:= y1;
while (x <= x2)
do begin
DrawPixel(Round(x),Round(y));
x:= x + dx;
y:= y + dy;
end;
end;
. 2.2.
(3;3)
,
Y-
,
(
(25;11).
,
Y
)
DrawPixel.
,
,
,
,
.
,
16
,
.
2.2.2.
(x1,y1)
(x2,y2).
,
e ).
(
x
,
1,
e,
y
x
y2
x2
1
Y
y1
x1
1
,
2
const .
Y1
.
,
.
2.2.
2.2
procedure DrawLineBrezenham (x1, y1, x2, y2:
integer);
var x, y, dx, dy, i: integer;
e: double;
begin
x:= x1;
y:= y1;
dx:= x2 - x1;
dy:= y2 - y1;
e:= dy/dx;
. 2.3.
for i:= 1 to dx
do begin
DrawPixel(x,y);
while (e >= 1/2)
do begin
y:= y + 1;
e:=e – 1;
end;
x:= x + 1;
e:= e + dy/dx;
end;
DrawPixel(x,y);
end;
(3;3)
(25;11).
,
.
.
e
1
,
2
2dx .
.
2.3).
2.3
,
procedure DrawLineBrezenham(x1, y1, x2, y2:
integer);
var x, y, dx, dy, sx, sy, z, i: integer;
e: double;
ChangeFlag: boolean;
begin
x:= x1;
y:= y1;
dx:= Abs(x2 - x1);
dy:= Abs(y2 - y1);
sx:= Sign(x2 - x1); //
sy:= Sign(y2 - y1); //
17
//
,
,
dx
dy
if dy>dx
then begin
z:= dx;
dx:= dy;
dy:= z;
ChangeFlag:= True;
end
else ChangeFlag:= False;
. 2.4 .
//
1/2
2*dx
e:= 2*dy-dx;
for i:= 1 to dx
do begin
DrawPixel(x,y);
//
0
while e>=0
do begin
if ChangeFlag then x:= x+sx else y:= y+sy;
e:= e - 2*dx;
end;
if ChangeFlag then y:= y+sy else x:= x+sx;
e:= e + 2*dy;
end;
DrawPixel(x,y);
end;
(3;3)
(25;11).
(25;3)
(3;11).
. 2.4 .
8-
.
,
(
,
.
).
.
§2.3.
R
.
(R,0)
.
,
:
"
"(
. 2.5).
. 2.5.
(I
,
( x 1) 2
y2
R2 ,
x2
( y 1) 2
R2
)
(x,y),
( x 1) 2
( y 1) 2
R2 .
5
(
(x,y)
(x+1,y)
1
(x,y-1)
(x+1,y-1)
4
5
. 2.6.
18
(0,R)
,
3
2
. 2.6).
( x 1) 2
( y 1) 2
R2 .
=0,
(x+1,y-1)
(
3);
(x+1,y-1)
<0,
1
2;
>0,
(x+1,y-1)
4
5.
<0, >0.
<0:
( x 1) 2
y2
R2
( x 1) 2
( y 1) 2
R2 .
<0,
,
.
.
1
o
:
1 2y
.
,
2
o
0
y>0.
:
( x 1) 2
,
y2
(y-1)2 / 2 ( x 1) 2
=/
R2
( x 1) 2
( y 1) 2
R2
( y 1) 2
2 y 1 2(
,
R2
y) 1 .
.
>0:
( x 1) 2
( y 1) 2
R2
x2
( y 1) 2
R2 .
<0,
,
(x+1,y-1).
.
4
o
:
( x 1) 2
,
( y 1) 2
(x+1)2 / 2 ( x 1) 2
=/
R2
( y 1) 2
x2
R2
( y 1) 2
2 x 1 2(
,
x) 1 .
.
5
o
R2
:
2x 1 0
.
(0,R).
,
,
x>0.
=2(1-R).
.
.
:
( x 1) 2
i 1
(x2
:
y2
i 1
(x2
y2
x2
x2
y2
R2
R2
R2 ) 2y 1
2y 1 R2
y 1)
R2
2x 1
y2
i
2( x
2x 1 y2
i
x2
2x 1 y 2
i
x2
R 2 ) 2x 1
( y 1) 2
y2
R2
R 2 ) 2x 2 y 2
( x 1) 2
i 1
(x2
:
( y 1) 2
2y 1 R2
2y 1
19
(
,
2.4).
.
2.4
procedure DrawCircleBrezenham(R: integer);
var x, y, delta: integer;
//
procedure DoDiag;
begin
x:= x + 1;
y:= y - 1;
delta:= delta + 2*(x-y+1);
end;
//
procedure DoHoriz;
begin
x:= x + 1;
delta:= delta + 2*x + 1;
end;
//
procedure DoVert;
begin
y:= y - 1;
delta:= delta - 2*y + 1;
end;
begin
x:= 0;
y:= R;
delta:= 2*(1-R);
//
I
(
//
II
while (y>=0)
do begin
DrawPixel(x,y);
if (delta = 0) then DoDiag
else if (delta < 0)
then begin
if (2*(delta+y)-1 <= 0)
then DoHoriz
else DoDiag;
end
else begin
if (2*(delta-x)-1 <= 0)
then DoDiag
else DoVert;
end;
end;
end;
. 2.7 .
, R=40
. 2.7 .
, R=11
y>=x,
)
. 2.7
.
,
,
2
.
.
:
.
.
while,
20
:
,
,
45 ,
90 ,
.
§2.4.
t0< t1<…<tm-1<tm.
n,
X(t),Y(t),
i
[ti,ti+1]
[t0, tm],
:
0..m 1
n 2,
:
(i )
X
Y
(i )
an t n
(i )
(i )
bn t
(i)
n
(i )
a n 1t n
bn 1t
1
...
n 1
...
(i)
a0 ,
b0 ;
(i )
(n-1)
[t0, tm]:
X (t )
C
Y (t )
C
n 1
n 1
[t 0 , t m ],
.
[t 0 , t m ]
(x0,y0), …, (xm,ym).
(x0,y0), …, (xm,ym) (
t0,…,t m
,
,
. 2.8).
(x1,y1)
(x0,y0)
(xm,ym)
. 2.8.
,
2 (n+1) m
(i)a (j)
.
2 (m-1) (n-1)
2 (n-1)
4m
(
,
)
,
.
,
)
(n-1)
,
,
(i)b(j).
.
,
.
,
.
(
(xi,yi),
,
).
.
,
.
.
.
u0< u1<…<um-1<um, v0< v1<…<vm-1<vm.
X(u,v), Y(u,v), Z(u,v),
n
x
n
X
pq
n
[ui,ui+1] [vj,vj+1]:
y:
n
a ijpq (u u p ) i (v v q ) j ,
(u , v )
i 0 j 0
n
p
n
Y pq (u , v )
bijpq (u
u p ) i (v v q ) j ,
0..m 1, q
0..k 1
i 0 j 0
n
n
Z pq (u , v)
cijpq (u u p ) i (v vq ) j ;
i 0 j 0
(n-1)X (u , v)
Y (u , v)
Z (u , v)
.
Cn
,
1, n 1
C
n 1,n 1
C
n 1,n 1
.
[ R ],
[ R ],
[ R ].
.
21
§2.5.
2.5.1.
,
.
,
,
,
.
,
.
«
XX-
60-
»(
.
)
1962
(
«
»),
,
,
.
,
.
,
,
t,
:
Pi j (t )
(1 t ) Pi j 1 (t ) tPi j1 1 (t ) ,
–
,
n-
P (t )
–
.
n
0
P (t ) .
,
4
.
P1
P(1,1)
P(0,2)
P2
P(0,3)
P(1,2)
P(0,1)
P(2,1)
P0
P3
. 2.9.
2.9
.
P0
, P3
.
0
,
(P0,P1), (P1,P2)
,
1
P2
P0
,
t
,
P3,
.
1
.
2
t : (1 t ) ,
,
t
(P2,P3)
.
P(0,1), P(1,1)
,
,
P(0,2)
P1
P(1,2).
t.
P(0,3),
.
t,
.
.
22
P(2,1).
1-
P01
(1 t ) P0
tP1 ,
P11
(1 t ) P1
tP2 ,
P21
(1 t ) P2
tP3 .
:
2-
:
P02 (t )
(1 t ) P01 (t ) tP11 (t )
(1 t ) 2 P0
2t (1 t ) P1
t 2 P2 ,
P12 (t )
(1 t ) P11 (t ) tP21 (t )
(1 t ) 2 P1
2t (1 t ) P2
t 2 P3 .
P 03 (t ) :
,
P03 (t )
(1 t ) P02 (t ) tP12 (t )
(1 t ) 3 Po
3t (1 t ) 2 P1
(1 t ) 2 P01 (t ) 2t (1 t ) P11 (t ) t 2 P21 (t )
3t 2 (1 t ) P2
.
t 3 P3 .
Pi ( xi , y i , z i ), i
N+1
n
0..n
n
: P n (t )
Bin (t ) Pi ,
Bin (t )
i 0
Bin (t )
n!
i!(n i )!
C in
C in t i (1 t ) n i ,
.
.
3
0
3
P (t )
2
(1 t ) Po
3t 2 (1 t ) P2
3t (1 t ) P1
t 3 P3
t
3
0
P (t )
:
3
t ( P3
3P2
3P1
2
P0 ) t (3P0
6 P1
3P2 ) t (3P1
3P0 ) P0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
N2
,
N1
,
.
. 3.1 .
N2
,
,
N1>N2 (
,
. 3.1 )
3
.
.
3
:
,
(
.
)
,
.
,
,
.
.
(
)
.
,
.
28
§3.2.
,
,
,
.
,
2
:
:
o
,
;
:
o
,
.
4-
8-
,
(
4-
8-
.
,
3.2
,
8-
)
3.2
4-
.
. 3.2 . 43.2
,
. 3.2 8-
.
,
4-
8-
.
(
43.1 , 3.1 ).
3.1
3.1
:
:
1.
:
.
OldColor
(x0, y0).
1.
bColor
(x1,
y1)
2.
(x0, y0).
OldColor,
NewColor.
3.
//
procedure Fill(X,Y: integer);
begin
if (Pixels[X,Y] = OldColor)
then begin
Pixels[X,Y]:= NewColor;
Fill(X+1,Y);
Fill(X-1,Y);
Fill(X,Y+1);
Fill(X,Y-1);
end;
end;
//
OldColor:= Pixels[x0,y0]; //
NewColor:= clRed;
//
Fill(x0,y0);
//
2.
bColor (
2.
),
NewColor.
3.
2.
//
procedure Fill(X,Y: integer);
begin
if (Pixels[X,Y] <> bColor) and
(Pixels[X,Y] <> NewColor)
then begin
Pixels[X,Y]:= NewColor;
Fill(X+1,Y);
Fill(X-1,Y);
Fill(X,Y+1);
Fill(X,Y-1);
end;
end;
//
bColor:= Pixels[x1,y1]; //
NewColor:= clRed;
//
Fill(x0,y0);
//
29
,
.
,
,
:
"
,
first out").
" ("last in
Push,
Pop,
(
3.2 , 3.2 ):
3.2
3.2
:
:
procedure Fill(x0,y0: integer;
OldColor, NewColor: TColor);
var Stack: array of TPoint;
aPoint: TPoint;
X,Y: integer;
Procedure Fill(x0,y0: integer;
bColor, NewColor: TColor);
var Stack: array of TPoint;
aPoint: TPoint;
X,Y: integer;
//
procedure Push(X,Y: integer);
begin
SetLength(Stack, Length(Stack)+1);
Stack[High(Stack)]:= Point(X,Y);
end;
//
function Pop: TPoint;
begin
Result:= Stack[High(Stack)];
SetLength(Stack, Length(Stack) - 1);
end;
//
procedure Push(X,Y: integer);
begin
SetLength(Stack, Length(Stack)+1);
Stack[High(Stack)]:= Point(X,Y);
end;
//
function Pop: TPoint;
begin
Result:= Stack[High(Stack)];
SetLength(Stack, Length(Stack) - 1);
end;
//
begin
SetLength(Stack, 0);
Push(x0,y0);
//
begin
SetLength(Stack, 0);
Push(x0,y0);
while (Length(Stack) > 0)
do begin
aPoint:= Pop;
X:= aPoint.X;
Y:= aPoint.Y;
if (Pixels[X,Y] = OldColor)
then begin
Pixels[x,y]:= NewColor;
Push(X+1,Y);
Push(X-1,Y);
Push(X,Y+1);
Push(X,Y-1);
end;
end;
end;
while (Length(Stack) > 0)
do begin
aPoint:= Pop;
X:= aPoint.X;
Y:= aPoint.Y;
if (Pixels[X,Y] <> bColor) and
(Pixels[X,Y] <> NewColor)
then begin
Pixels[X,Y]:= NewColor;
Push(X+1,Y);
Push(X-1,Y);
Push(X,Y+1);
Push(X,Y-1);
end;
end;
end;
,
.
O(N),
,
N
.
(
1.
2.
3.
3.3).
.
X
(X,Y)
,
Y.
,
.
30
:
4.
XL
XR
.
5.
[XL, XR]
Y-1
Y+1
.
.
6.
.
,
2.
3.3
procedure Fill(x0,y0: integer; bColor, NewColor: TColor);
var Stack: array of TPoint;
aPoint: TPoint;
X,Y: integer;
xTmp, yTmp: integer;
Flag: boolean;
xL, xR, xStart: integer;
//
Push
begin
SetLength(Stack, 0);
Push(x0,y0);
//
Pop
while (Length(Stack) > 0)
//
do begin
aPoint:= Pop;
//
X:= aPoint.X;
Y:= aPoint.Y;
if (Pixels[X,Y] <> bColor) and (Pixels[X,Y] <> NewColor)
then begin
Pixels[x,y]:= NewColor;
//
xTmp:= X;
X:= X + 1;
while (Pixels[X,Y] <> bColor) and (Pixels[X,Y] <> NewColor)
do begin
Pixels[x,y]:= NewColor;
X:= X + 1;
end;
xR:= X;
//
,
X:= xTmp;
//
X
X:= X - 1;
while (Pixels[X,Y] <> bColor) and (Pixels[X,Y] <> NewColor)
do begin
Pixels[x,y]:= NewColor;
X:= X - 1;
end;
xL:= X + 1;
//
,
//
//
Y:= Y + 1;
//
X:= xL;
//
[xL; xR]
while (X <= xR)
do begin
Flag:= False;
while (Pixels[X,Y] <> bColor) and (Pixels[X,Y] <> NewColor) and (X < xR)
do begin
if not Flag then Flag:= True;
//
!
X:= X + 1;
//
end;
if Flag
then begin
//
,
31
//
if (X = xR) and (Pixels[X,Y] <> bColor) and (Pixels[X,Y] <> NewColor)
then Push(X,Y) else Push(X-1,Y);
Flag:= False;
end;
XStart:= X;
//
,
while ((Pixels[X,Y] = bColor) or (Pixels[X,Y] = NewColor)) and (X < xR)
do X:= X + 1;
if X = XStart then X:= X + 1;
//
,
end;
end;
end;
end;
X
(
).
,
;
(
).
. 3.3
.
. 3.3.
.
,
.
3-
,
,
,
,
,
,
,
.
(LIFO).
§3.3.
.
,
,
,
,
,
.
(x1,y1)
,
(x2,y2)
x
(x2, y2) -
,
,
y-
y2 y1.
(
(x1, y1)
).
,
.
(
,
. 3.4.
32
. 3.4):
;
(
)
.
;
.
,
,
.
,
,
.
(
.)
,
.
3.3.1.
1.
y=yi,
i
y1
y2.
,
yi
3.4
3.4):
(x1, y1) - (x2, y2)
do begin
y:= Trunc(y1)+1;
//
dx:= (x2 - x1)/(y2 - y1); //
x:= x1 + dx*(y - y1);
//
while (y <= y2)
//
do begin
PutToList(x, y);
//
y:= y + 1;
x:= x + dx;
end;
end;
2.
3.
y
X
y=1
x
,
y
(
yi)
.
Y-
[x2i-1, x2i].
3.3.2.
,
,
,
(
),
,
.
.
(
Y)
3.5
.
(x1, y1) - (x2, y2)
do begin
//
{y, dx, x}
y:= Trunc(y1)+1;
//
dx:= (x2 - x1)/(y2 - y1); //
x:= x1 + dx*(y - y1);
//
end;
;
y:=
[0] .Y;
3.5.
y
X
//
//
//
(
,
while (
)
//
do begin
DrawLine(x2i-1,y,x2i,y);
y:= y+1;
do begin
if (y >
,
.y2) then
.y = y);
//
//
(x2i-1, x2i)
)
(-
)
(- )
y
//
33
else begin
.x:=
while (
do
end;
end;
.x + dx;
).x >
.x)
//
x
_
;
,
.y = y);
,
//
//
.y = y);
end;
,
,
,
,
y,
(
).
while
,
x
dx.
,
.
(
),
.
3.3.3.
,
.
. 3.5.
1.
2.
xmax.
Y
,
i-
(
),
x=xmax.
2
3.
.
4
2
1
3
5
. 3.5.
,
:
;
o
o
,
,
.
,
3.4.
.
(
)
,
I(x1,y1):= I(x1,y1) XOR 1.
,
,
,
.
34
,
, "
,
"
Download