You are on page 1of 29

Bi ging ha my tnh

(Cp nht thng 9/2009)


Ging vin:
ng Nguyn c Tin V Quc Hong L Phong

Xt on thng c h s gc 0 < m < 1 v


Dx > 0.
im cn chn (xi+1 , yi+1) bc th (i+1) s l
mt trong hai trng hp nh hnh v sau:

HCMUS - 2009

Bi ging ha my tnh ng Nguyn c Tin - V Quc Hong - L Phong

HCMUS - 2009

Bi ging ha my tnh ng Nguyn c Tin - V Quc Hong - L Phong

[2.5] Ngi ta c th ci thin tc ci t thut ton v on thng bng cch


ch cn v mt na on thng, phn cn li ly i xng na on thng v.
Hy ci t minh ha trn giy vi thut ton Bresenham/MidPoint. Gi s im
u l x1, y1, im cui l x2, y2 v ta c 0 < (y2 y1)/(x2 x1) 1, x1 > x2.
Thi gian: 30 pht.

HCMUS - 2009

Bi ging ha my tnh ng Nguyn c Tin - V Quc Hong - L Phong

void LineBres (int x1, int y1, int x2, int y2)
{
int Dx, Dy, p, Const1, Const2;
int x, y;
Dx = x2 - x1; Dy = y2 - y1;
p = 2*Dy Dx;
[2.5] Ngi ta c th ci thin tc
Const1 = 2*Dy; Const2 = 2*(Dy-Dx);
ci t thut ton v on
x = x1; y = y1;
thng bng cch ch cn v mt
na on thng, phn cn li ly
putpixel(x, y, Color);
i xng na on thng v.
for(i=x1; i<x2; i++){
Hy ci t minh ha trn giy
if (p<0)
vi thut ton
p += Const1;
Bresenham/MidPoint. Gi s im
u l x1, y1, im cui l x2, y2
else{
v ta c
p += Const2;
0 < (y2 y1)/(x2 x1) 1,
y++;
x1 > x2.
}
x++;
putpixel(x, y, Color);
}
} // LineBres
HCMUS - 2009

Bi ging ha my tnh ng Nguyn c Tin - V Quc Hong - L Phong

void LineBres (int x1, int y1, int x2, int y2)
{
int Dx, Dy, p, Const1, Const2;
int x, y;
Dx = x2 - x1; Dy = y2 - y1;
p = 2*Dy Dx;
[2.5] Ngi ta c th ci thin tc
Const1 = 2*Dy; Const2 = 2*(Dy-Dx);
ci t thut ton v on
x = x1; y = y1;// x = x2; y = y2;
thng bng cch ch cn v mt
na on thng, phn cn li ly
putpixel(x, y, Color);
i xng na on thng v.
for(i=x1; i<x2; i++){
Hy ci t minh ha trn giy
if (p<0)
vi thut ton
p += Const1;
Bresenham/MidPoint. Gi s im
u l x1, y1, im cui l x2, y2
else{
v ta c
p += Const2;
0 < (y2 y1)/(x2 x1) 1,
y--; y++;
x1 > x2.
}
x--; x++;
putpixel(x, y, Color);
}
} // LineBres
HCMUS - 2009

Bi ging ha my tnh ng Nguyn c Tin - V Quc Hong - L Phong

HCMUS - 2009

Bi ging ha my tnh ng Nguyn c Tin - V Quc Hong - L Phong

void circleSym4(int R)
{
r2 = R * R;
putPixel(0, R);
putPixel(0, -R);
for (x = 1; x <= R; x++) {
y = round(sqrt(r2 - x*x));
putPixel(x, y);
putPixel(x, -y);
putPixel(-x, y);
putPixel(-x, -y);
}
}
HCMUS - 2009

Bi ging ha my tnh ng Nguyn c Tin - V Quc Hong - L Phong

void circleSym8(int R)
{
r2 = R * R;
putPixel(0, R); putPixel(0, -R);
putPixel(R, 0); putPixel(-R, 0);
x = 1; y = round(sqrt(r2 - x*x));
while (x < y) {
putPixel(x, y); putPixel(x, -y);
putPixel(-x, y);
putPixel(-x, -y);
putPixel(y, x); putPixel(y, -x);
putPixel(-y, x);
putPixel(-y, -x);
x++
y = round(sqrt(r2 - x*x));
}
}
HCMUS - 2009

Bi ging ha my tnh ng Nguyn c Tin - V Quc Hong - L Phong

HCMUS - 2009

Bi ging ha my tnh ng Nguyn c Tin - V Quc Hong - L Phong

10

HCMUS - 2009

Bi ging ha my tnh ng Nguyn c Tin - V Quc Hong - L Phong

11

HCMUS - 2009

Bi ging ha my tnh ng Nguyn c Tin - V Quc Hong - L Phong

12

HCMUS - 2009

Bi ging ha my tnh ng Nguyn c Tin - V Quc Hong - L Phong

13

HCMUS - 2009

Bi ging ha my tnh ng Nguyn c Tin - V Quc Hong - L Phong

14

p0 = ?

HCMUS - 2009

Bi ging ha my tnh ng Nguyn c Tin - V Quc Hong - L Phong

15

void CircleMidPoint (int R)


{
x = 0; y = R;
Put8Pixels(x, y);
p = 1 - R; // 5/4-R Gii thch ti sao?
while (x < y){
if (p < 0) {
p += 2*x + 3;
} else {
p += 2*(x -y) + 5;
y--;
}
x++;
Put8Pixels(x, y);
}
} // CircleMidPoint

HCMUS - 2009

Bi ging ha my tnh ng Nguyn c Tin - V Quc Hong - L Phong

16

V ng trn tm I(0, 0), bn knh R = 15

HCMUS - 2009

xi

yi

pi

Tnh ton

15

-14

1-15

15

-11

-14+2.0+3

15

-6

-11+2.1+3

15

-6+2.2+3

14

-18

1+2.(3-15)+5

14

-7

-18+2.4+3

14

-7+2.5+3

13

-5

6+2.(6-14)+5

13

12

-5+2.7+3

12

12+2.(8-13)+5

10

10

11

7+2.(9-12)+5

11

11

10

9+2.(10-11)+5

Bi ging ha my tnh ng Nguyn c Tin - V Quc Hong - L Phong

17

HCMUS - 2009

Bi ging ha my tnh ng Nguyn c Tin - V Quc Hong - L Phong

18

xi

yi

pi

Tnh ton

Delta1

Delta2

15

-14

1-15

-25

15

-11

-14+2.0+3

-23

15

-6

-11+2.1+3

-21

15

-6+2.2+3

-19

14

-18

1+2.(3-15)+5

11

-15

14

-7

-18+2.4+3

13

-13

14

-7+2.5+3

15

-11

13

-5

6+2.(6-14)+5

17

-7

13

12

-5+2.7+3

19

-5

12

12+2.(8-13)+5

21

-1

10

10

11

7+2.(9-12)+5

23

11

11

10

9+2.(10-11)+5

25

HCMUS - 2009

Bi ging ha my tnh ng Nguyn c Tin - V Quc Hong - L Phong

19

x = 0; y = r;
p = 1-r;
Delta1 = 2*x+3;
Delta2 = 2*(x-y)+5;
Put8Pixel(x,y);
while (x<y){
if (p<0) {
p += Delta1;
} else {
p += Delta2;
Delta2 += 2;
y--;
}
x++;
Delta1 += 2; Delta2 += 2;
Put8Pixel(x,y);
}

HCMUS - 2009

Bi ging ha my tnh ng Nguyn c Tin - V Quc Hong - L Phong

20

HCMUS - 2009

Bi ging ha my tnh ng Nguyn c Tin - V Quc Hong - L Phong

21

HCMUS - 2009

Bi ging ha my tnh ng Nguyn c Tin - V Quc Hong - L Phong

22

HCMUS - 2009

Bi ging ha my tnh ng Nguyn c Tin - V Quc Hong - L Phong

23

HCMUS - 2009

Bi ging ha my tnh ng Nguyn c Tin - V Quc Hong - L Phong

24

Cng thc tng qut ca Ellipse l:


b2x2 + a2y2 - a2b2 = 0

HCMUS - 2009

Bi ging ha my tnh ng Nguyn c Tin - V Quc Hong - L Phong

25

p = b2 - a2*b + a2/4;
Const1 = 2*b2; Const2 = 2*a2;
MaxX = a2/sqrt(a2+b2));
x = 0; y = b;
Delta2 = 2*a2*(1-y)+b2*(2*x+3);
Delta1 = b2*(2*x+3);
Put4Pixel(x,y);
while (x<MaxX){
if (p>=0){
p
+= Delta2;
Delta2 += Const2;
y--;
} else {
p += Delta1;
}
Delta2+=Const1; Delta1+=Const1;
x++;
Put4Pixel(x,y);
}
HCMUS - 2009

Bi ging ha my tnh ng Nguyn c Tin - V Quc Hong - L Phong

26

1. [2.8] S dng tng thut ton Bresenham,


xy dng thut ton v ng trn c tm l
gc ta , bn knh R.
2. Trong phng php v ng trn theo
Bresenham v MidPoint, thut ton no c li
hn? Gii thch.

HCMUS - 2009

Bi ging ha my tnh ng Nguyn c Tin - V Quc Hong - L Phong

27

3. * Hy a ra thut ton cho php v ng


cong bc 3 thng qua 4 im phn bit. Trong
: mi im p trn ng gia 2 im cho
trc th i v (i+1) c ta : P = (xi(t), yi(t)).
Trong :

Vi t [0, 1].

HCMUS - 2009

Bi ging ha my tnh ng Nguyn c Tin - V Quc Hong - L Phong

28

HCMUS - 2009

Bi ging ha my tnh ng Nguyn c Tin - V Quc Hong - L Phong

29