Tải bản đầy đủ (.pdf) (38 trang)

Lecture computer graphics and virtual reality slides lesson 3 basic entity algorithms

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.05 MB, 38 trang )

Lesson 3

Basic entity algorithms

Trinh Thanh Trung School of ICT, HUST

Content

1. Line representation

1. DDA algorithm
2. Bresenham algorithm
3. Mid-point algorithm

2. Circle & ellipse representation
3. Character representation

Rasterization: Background

■ Converting a continuous object in the world into
a discrete object in the computer

■ We need to lit the pixels instead of drawing a
continuous line

1.
Line representation

Line drawing P(x2 , y2)

■ Line representation u


y = k x + b
P(x1, y1)
with
m
k = (y2-y1)/( x2-x1)

or
𝑦 − 𝑦1 = 𝑦2 − 𝑦1
𝑥 − 𝑥1 𝑥2 − 𝑥1

(explicit form)

Line drawing

■ From linear algebra
y = k x + b

■ Simple approach:

1. increment x
2. calculate y
3. cast y to an integer
4. draw (x, (int)y)

PROBLEM?

Problems

■ It seems to work okay
for lines with a slope of 1

or less
■ Doesn’t work well for
lines with slope greater
than 1
■ Lines become more
discontinuous in
appearance
■ We must add more
than 1 pixel per column to
make it work.

DDA algorithm

■ DDA = Digital Differential Analyser
■ Treat line as parametric equation in t

□ Start point (x1, y1)
□ End point (x2, y2)

■ From linear algebra
y = k x + b

□ When 0 xi+1 = xi + 1
yi+1 = yi + k

DDA algorithm: Pseudo code

ddaline(x1, y1, x2, y2)
begin

m =(x2-x1)/(y2-y1);
x = x1;
y = y1;
k = 1/m;
putpixel(x,y);
while x begin
x = x+1;
y = y+k;
putpixel(x,round(y));
end;

end;

DDA algorithm: General algorithms

■ Choose appropriate value
for the number of steps (n)

■ Start at t = 0

■ At each step, increment t by
1/n

■ Ensure no pixels are missed:

□ Implies: and

■ Set n to maximum of dx and
dy


DDA algorithm: General algorithms

ddaline(int x1, int y1, int x2, int y2)

{

float x,y;

int dx = x2-x1, dy = y2-y1; n – range of t

int n = max(abs(dx),abs(dy));

float dt = n, dxdt = dx/dt, dydt = dy/dt;

x = x1; y = y1;

while(n--) {

point(round(x),round(y));

x += dxdt;

y += dydt;

}

}

DDA Algorithm: Cons


■ Need a lot of floating point arithmetic.

□ 2 ‘round’s and 2 adds per pixel.

■ Can we use only integer arithmetic?

□ Easier to implement in hardware.

Bresenham algorithm

■ Developed in 1962 at IBM

■ Uses only integer addition, subtraction and bit
shifting, all of which are very cheap operations in
standard computer architectures

■ Chooses the integer y

corresponding to the pixel

center that is closest to the

ideal (fractional) y for the 2 d2
same x; on successive
d1
columns y can remain the 1

same or increase by 1 0


0 12

Bresenham algorithm: pseudo code

d2 = y - yi = k(xi +1) + b - yi
d1 = yi+1 - y = yi + 1 - k(xi + 1) - b
■If d1  d2 => yi+1 = yi + 1
else d1 > d2 => yi+1 = yi
■D = d1 - d2 = -2k(xi + 1) + 2yi - 2b + 1
■Pi = xD = x (d1 - d2)
Pi = -2yxi + 2xyi + c
Pi+1 - Pi = -2y(xi+1 - xi) + 2x(yi+1 - yi)
■If Pi  0  yi+1 = yi + 1

Pi+1 = Pi - 2y + 2x
■If Pi > 0  yi+1 = yi

Pi+1 = Pi - 2y
P1 = x(d1 - d2)
P1 = -2y + x

Mid-point algorithm

■ At step p, assume pixel (xp, yp) was lit
■ Check where the line intersects with x= xp+1
■ Lit (xp+1, yp) or (xp+1, yp+1) , whichever is
■ closer to the intersection

Mid-point algorithm


■ With the implicit form of a line
ax + by + c = 0

■ For every (xi , yi)

axi + byi + c = 0  (xi , yi ) on line
axi + byi + c  0  (xi , yi ) above line
axi + byi + c  0  (xi , yi ) below line

Mid-point algorithm

■ Assume slope is less than 1 and current pixel is
(xi, yi)
■ Next mid-point will be (xi + 1, yi + ½)
■ We calculate di (called decision variable)

 1

di = a(xi +1)+ b yi +  + c

 2

Mid-point algorithm

■ If di > 0: Mid-point is below line

□ A will be chosen

■ If di < 0: Mid-point is above line


□ B will be chosen

A

Previous M Choices for
Pixel B Next pixel
(xi,yi)
Choices for
Current pixel

Mid-point algorithm

■ If A is chosen: We update the decision variable

 3  3
 xi + 2, yi +   di+1 = a(xi + 2)+ b yi +  + c
 2  2

= di + a + b

A

Previous M Choices for
Pixel B Next pixel
(xi,yi)
Choices for
Current pixel

Mid-point algorithm


■ If B is chosen: We update the decision variable

 1  1
 xi + 2, yi +   di+1 = a(xi + 2) + b yi +  + c
 2  2
= di + a

A

Previous M Choices for
Pixel B Next pixel
(xi,yi)
Choices for
Current pixel


×