11_algoritmi de decupare
DESCRIPTION
Algoritmi de decupare. BresenhamTRANSCRIPT
1
Algoritmi de decupare
• Decuparea capetelor
xmin < x < xmax si ymin < y < ymax punct interior
• Analiza capetelor unui segment de dreapta:
– acceptare
– rejectare
– intersectie
• Rezolvarea ecuatiilor folosind y = mx + b – ecuatia dreptei
(Xmin , Ymin)
(Xmax , Ymax)
2
Algoritmi de decupare
• Ecuatia parametrica a unei drepte
X = x0 + t(x1 – x0) 0 < t < 1
Y = y0 + t(y1 – y0)
P(t) = P0 + t(P1 – P0)
• Punct de intersectie cu segmentul de dreapta daca
tintersectie este in intervalul[0,1]
3
Algoritmul de decupare linii Cohen-Sutherland
Decuparea se realizeaza fata de o zona dreptunghiulara.
segment inclus
A
B
segment disjunct
A
B
segment intersectat
A
B
A
B
segment intersectat
I1
I1
I2
4
Algoritmul de decupare linii Cohen-Sutherland
• Se considera punctele de max / min
– xmin<=x<=xmax
– ymin<=y<=ymax
ymin
ymax
xmin xmax
5
Algoritmul de decupare linii Cohen-Sutherland
ymin
ymax
xmin xmax
• D1- ambele capete in interior acceptare triviala
• D2- ambele capete in exterior rejectare
• D3,D4 – trebuie sa se calculeze punctele de intersectie
D1
D2
D3D4
6
Algoritmul de decupare linii Cohen-Sutherland
• Codificare b4b3b2b1
– b4 = 1 y>ymax
– b3 = 1 y<ymin
– b2 = 1 x>xmax
– b1 = 1 x<xmin
ymin
ymax
xmin xmax
0000
1000
0100
0001 0010
1001
0101 0110
1010
7
Algoritmul de decupare linii Cohen-Sutherland
• Segmentul P1P2
• Se codifica P1, P2
• c1 = codificare(P1)
• c2 = codificare(P2)
ymin
ymax
xmin xmax
0000
1000
0100
0001 0010
1001
0101 0110
1010
D1 D2
D3
D5
If c1 = c2 = 0
acceptare triviala
If c1 & c2 = true
rejectare triviala
D4
8
Algoritmul de decupare linii Cohen-Sutherland
9
Algoritmul de decupare linii Cohen-Sutherland
ymin
ymax
xmin xmax
0000
1000
0100
0001 0010
1001
0101 0110
1010
P2
P1
10
Algoritmul de decupare linii Cohen-Sutherland
ymin
ymax
xmin xmax
0000
1000
0100
0001 0010
1001
0101 0110
1010
I1
P1
11
Algoritmul de decupare linii Cohen-Sutherland
ymin
ymax
xmin xmax
0000
1000
0100
0001 0010
1001
0101 0110
1010
P2
P1
12
Algoritmul de decupare linii Cohen-Sutherland
ymin
ymax
xmin xmax
0000
1000
0100
0001 0010
1001
0101 0110
1010
P2
I1
13
Algoritmul de decupare linii Cohen-Sutherland
ymin
ymax
xmin xmax
0000
1000
0100
0001 0010
1001
0101 0110
1010
I2
I1
14
Algoritmul de decupare linii Cohen-Sutherland
codificare(x, y, xmin, xmax, ymin, ymax)
{
c = 0;
if(x < xmin) c |= 1;
if(x > xmax) c |= 2;
if(y < ymin) c |= 4;
if(y > ymax) c |= 8;
intoarce c;
}
• Codificare b4b3b2b1
– b1 = 1 x<xmin
– b2 = 1 x>xmax
– b3 = 1 y<ymin
– b4 = 1 y>ymax
15
Algoritmul de decupare linii Cohen-Sutherland
decupare(x1, y1, x2, y2, xmin, ymin, xmax, ymax)
{
cod1 = codificare(x1, y1, xmin, ymin, xmax, ymax);
cod2 = codificare(x2, y2, xmin, ymin, xmax, ymax);
repeta
{
if(cod1 == 0 && cod2 == 0)
{(x1, y1) – (x2, y2) – segment inclus; stop;}
if(cod1 & cod2)
{(x1, y1) – (x2, y2) – segment disjunct; stop;}
16
Algoritmul de decupare linii Cohen-Sutherland
if(cod1 != 0) outc = cod1;else outc = cod2;
if(outc & 1) //intersectie cu x = xmin{calculeaza (x, y) punctul de intersectie cu x = xmin}else
if(outc & 2) //intersectie cu x = xmax{calculeaza (x, y) punctul de intersectie cu x = xmax}else
if(outc & 4) //intersectie cu y = ymin{calculeaza (x, y) punctul de intersectie cu y = ymin}else
if(outc & 8) //intersectie cu y = ymax{calculeaza (x, y) punctul de intersectie cu y = ymax}
17
Algoritmul de decupare linii Cohen-Sutherland
if(outc == cod1)
{
x1 = x; y1 = y;
cod1 = codificare(x1, y1, xmin, ymin, xmax, ymax);
}
else
{
x2 = x; y2 = y;
cod2 = codificare(x2, y2, xmin, ymin, xmax, ymax);
}
} //repeta
if(segment inclus)
(x1,y1) – (x2,y2) segmentul inclus
}
18
Algoritmul de decupare linii Cohen-Sutherland
ymin
ymax
xmin xmax
0000
1000
0100
0001 0010
1001
0101 0110
1010
P2
P1
19
Algoritmul de decupare linii Cohen-Sutherland
ymin
ymax
xmin xmax
0000
1000
0100
0001 0010
1001
0101 0110
1010I1
P1
20
Algoritmul de decupare linii Cohen-Sutherland
ymin
ymax
xmin xmax
0000
1000
0100
0001 0010
1001
0101 0110
1010I2
P1
21
Algoritmul de decupare linii Cohen-Sutherland
ymin
ymax
xmin xmax
0000
1000
0100
0001 0010
1001
0101 0110
1010
P2
P1
22
Algoritmul de decupare linii Cohen-Sutherland
ymin
ymax
xmin xmax
0000
1000
0100
0001 0010
1001
0101 0110
1010
P2
I1
23
Algoritmul de decupare linii Cohen-Sutherland
ymin
ymax
xmin xmax
0000
1000
0100
0001 0010
1001
0101 0110
1010
P2
I2
24
Algoritmul de decupare linii Cohen-Sutherland
ymin
ymax
xmin xmax
0000
1000
0100
0001 0010
1001
0101 0110
1010
I3
I2
25
Algoritmul de decupare linii Cohen-Sutherland
ymin
ymax
xmin xmax
0000
1000
0100
0001 0010
1001
0101 0110
1010
P2
P1
26
Algoritmul de decupare linii Cohen-Sutherland
ymin
ymax
xmin xmax
0000
1000
0100
0001 0010
1001
0101 0110
1010
P2I1
27
Algoritmul de decupare linii Cyrus-Beck
28
Algoritmul de decupare linii Cyrus-Beck
Se determina t pentru intersectia P0 P1 cu
latura Ei:
Ni • [P(t) – PEi] = 0
Ni • [P0 + (P1 – P0)t – PEi] = 0
Ni • [P0 – PEi] + Ni • [P1 – P0]t = 0
Se noteaza cu D vectorul de la P0 la P1
D = (P1 – P0),
Ni 0
D 0 ( P1 P0)
Ni • D 0 (Ei nu e paralela cu D)
Di
N
iEPP
iN
t
]0
[
29
Algoritmul de decupare linii Cyrus-Beck
•Se elimina valorile lui t din afara intervalului [0,1]
B
A D
C
30
• P0 (t=0) P1 (t=1):
– punct potential de intrare (Ni • D < 0) PI
– Punct potential de iesire (Ni • D > 0) PE
Algoritmul de decupare linii Cyrus-Beck
B
A D
C
I2
I1
I4
I3
31
Algoritmul de decupare linii Cyrus-Beck
B
A D
C
PE
PI
PI
PE
•If tPE < tPI nu exista intersectii
32
Algoritmul de decupare linii Cyrus-Beck
B
A
PE
PI
PI
PE
33
Algoritmul de decupare linii Cyrus-Beck
decupare(x1,y1,x2,y2,xmin,ymin,xmax,ymax)
{calculeaza Ni si alege PEi pentru fiecare latura;
if P1 = P2 decuparea unui punct;
else
{
t_PI = 0; t_PE = 1;
for fiecare punct de intersectie
if Ni • D 0 latura paralela cu segmentul de decupat
{
calculeaza t;
se clasifica punctul in PI sau PE
(folosind semnul Ni • D < 0 PI, > 0 PE);
if (PI) t_PI = max(t_PI,t);
if (PE) t_PE = min(t_PE,t);
}
}
34
Algoritmul de decupare linii Cyrus-Beck
if t_PI > t_PE
return nu exista intersectie
else
return P(t_PI) si P(t_PE) segmentul decupat
}
}
35
Algoritmul de decupare linii Cyrus-Beck
• D = P1 – P0 = (x1 – x0, y1 – y0)
• PEipunct oarecare pe latura de decupare
36
Algoritmul Liang-Barsky
• Pentru un punct de intersectie PI cu
tPI >tPE stop
• Clasificarea punctelor de intersectie
– punct potential de intrare (Ni • D < 0) PI
– punct potential de iesire (Ni • D > 0) PE
37
Algoritmul Liang-Barsky>
• Clasificarea punctelor de intersectie
– punct potential de intrare (Ni • D < 0) PI
– Punct potential de iesire (Ni • D > 0) PE
• dx = x1-x0, dy = y1-y0
• dx > 0 sau dy > 0 => PI
• dx < 0 sau dy < 0 => PE
38
Algoritmul Liang-Barsky
bool Decupare_L(numitor, numarator, t_PI, t_PE)
{
//numitor = -(Ni.D): dx, -dx, dy, -dy
//numarator = Ni.(P1-PEi)
if(numitor > 0) //intersectie PI
{
calculeaza t = numarator/numitor;
if(t > t_PE) return FALSE;
if(t > t_PI) t_PI = t;
}
39
Algoritmul Liang-Barsky
if(numitor <0) //intersectie PE
{
calculeaza t = numarator/numitor;
if(t < t_PI) return FALSE;
if(t < t_PE) t_PE = t;
}
return TRUE;
}
40
Algoritmul Liang-Barsky
Decupare(x1,y1,x2,y2)
{
t_PI=0; t_PE = 1;
if (Decupare_L(dx, xmin-x1, t_PI, t_PE) == TRUE)//xmin
if (Decupare_L(-dx, x1-xmax, t_PI, t_PE) == TRUE)//xmax
if (Decupare_L(dy, ymin-xy, t_PI, t_PE) == TRUE)//ymin
if (Decupare_L(-dy, y1-ymax, t_PI, t_PE) == TRUE)//ymax
{
if(t_PE < 1)
{ x2 = x1 + t_PE*dx; y2 = y1 + t_PE * dy; }
if(t_PI > 0)
{ x1 = x1 + t_PI*dx; y1 = y1 + t_PI * dy; }
}
}
41
Decuparea poligoanelor
42
Algoritmul de decupare poligoane Sutherland-
Hodgman
43
Algoritmul de decupare poligoane
Sutherland-Hodgman
•s – varful poligonului analizat la pasul anterior
•p – varful poligonului analizat la pasul curent
ext int
s
p
nu se pastreaza
nici un varf
ext int
s p
se pastreaza
punctul de intersectie
I si varful p
ext int
s
p
se pastreaza
varful p
ext int
sp
se pastreaza punctul
de intersectie I
I
I
44
Algoritmul de decupare poligoane
Sutherland-Hodgman
45
Algoritmul de decupare poligoane
Sutherland-Hodgman
46
Algoritmul de decupare poligoane
Sutherland-Hodgman
decuparePoligon(varfuri, nv, varfuri_dec, nv_dec, L){
nv_dec = 0;s = varfuri[nv-1];for(j = 0; j < nv, j++){
p = varfuri[j];if(p este in semiplanul interior corespunzator laturii L)
if(s este in semiplanul interior corespunzator laturii L)varfuri_dec[nv_dec++] = p;
47
Algoritmul de decupare poligoane
Sutherland-Hodgman
else //s exterior, p interior{
i = punctul de intersectie dintre latura sp si latura L;varfuri_dec[nv_dec++] = i;varfuri_dec[nv_dec++] = p;
}
48
Algoritmul de decupare poligoane
Sutherland-Hodgman
else //p exteriorif(s este in semiplanul interior corespunzator laturii lat){
i = punctul de intersectie dintre latura sp si latura L;varfuri_dec[nv_dec++] = i;
}s = p;
} //for}
49
Algoritmul de decupare poligoane
Sutherland-Hodgman
decupare(varfuri, nv, varfuri_dec, nv_dec, zona_decupare)
{
decuparePoligon(varfuri, nv, varfuri_dec, nv_dec, L1);
decuparePoligon(varfuri_dec, nv_dec, varfuri, nv, L2);
decuparePoligon(varfuri, nv, varfuri_dec, nv_dec, L3);
decuparePoligon(varfuri_dec, nv_dec, varfuri, nv, L4);
}
50
Algoritmul de decupare poligoane
Sutherland-Hodgman
decuparePoligon(varfuri, nv, varfuri_dec, nv_dec, L){
nv_dec = 0;s = varfuri[nv-1];for(j = 0; j < nv, j++){
p = varfuri[j];if(p este in semiplanul interior corespunzator laturii L)
if(s este in semiplanul interior corespunzator laturii L)varfuri_dec[nv_dec++] = p;
else //s exterior, p interior{
i = punctul de intersectie dintre latura sp si latura L;varfuri_dec[nv_dec++] = i;varfuri_dec[nv_dec++] = p;
}else //p exterior
if(s este in semiplanul interior corespunzator laturii lat){
i = punctul de intersectie dintre latura sp si latura L;varfuri_dec[nv_dec++] = i;
}s = p;
} //for}
51
Algoritmul de decupare poligoane
Sutherland-Hodgman
52
Algoritmul de decupare poligoane
Sutherland-Hodgman
53
Algoritmul de decupare poligoane
Sutherland-Hodgman
Algoritmul Weiler-Atherton
In -> Out Parcurge conturul pana cand
(a) Se determina un nou punct de intersectie
(b) Se intalneste un varf deja adaugat
Algoritmul Weiler-Atherton
Ext -> Int Int -> Int
Algoritmul Weiler-Atherton
Continua de la ultimul varf de
intersectie de pe poligonOut -> Int
Algoritmul Weiler-Atherton
Int -> Out
Algoritmul Weiler-Atherton
Algoritmul Weiler-Atherton
60
Decuparea in 3D
61
Decuparea in 3D – Cohen Sutherland
bit 6
Far
bit 5
Near
bit 4
Top
bit 3
Bottom
bit 2
Right
bit 1
Left