Lect 3 CG 2011
Lect 3 CG 2011
Computer Graphics
Lecture 3
Line & Circle Drawing
Computer Graphics
Simple Line
Based on slope-intercept
algorithm from algebra:
y = mx + b
Simple approach:
increment x, solve for y
Floating point arithmetic
required
4
Computer Graphics
Does it Work?
It seems to work okay for lines with
a slope of 1 or less,
but doesn’t work well for lines with
slope greater than 1 – lines become
more discontinuous in appearance
and we must add more than 1 pixel
per column to make it work.
Solution? - use symmetry.
5
Computer Graphics
6
Computer Graphics
DDA algorithm
• DDA = Digital Differential Analyser
– finite differences
• Treat line as parametric equation in t :
7
Computer Graphics
dx
• Start at t = 0 xnew xold
dt
• At each step, increment t by dt ynew yold
dy
dt
• Choose appropriate value for dt dx x2 x1
• Ensure no pixels are missed: dy y2 y1
– Implies: dx and dy
1 1
dt dt
DDA algorithm
line(int x1, int y1, int x2, int y2)
{ n - range of t.
float x,y;
int dx = x2-x1, dy = y2-y1;
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;
}
}
9
Computer Graphics
DDA algorithm
• Still need a lot of floating point arithmetic.
– 2 ‘round’s and 2 adds per pixel.
10
Computer Graphics
Observation on lines.
while( n-- )
{
draw(x,y);
move right;
if( below line )
move up;
}
11
Computer Graphics
12
Computer Graphics
• So:
dy
y mx b and so y xb
dx
F ( x, y ) dy.x dx. y c 0
13
Computer Graphics
Decision variable.
Let’s assume dy/dx < 0.5 (we can use symmetry)
Evaluate F at point M 1
d F ( x p 1, y p )
2
Referred to as decision variable
NE
M
E
Decision variable.
Evaluate d for next pixel, Depends on whether E or NE Is chosen :
If E chosen :
1 1
d new F ( x p 2, y p ) a ( x p 2) b( y p ) c
2 2
But recall :
1
d old F ( x p 1, y p )
NE 2
1
M a ( x p 1) b( y p ) c
2
E
Previous Choices for
So :
d new d old a
Pixel Choices for Next pixel
(xp,yp) Current pixel d old dy
15
Computer Graphics
Decision variable.
If NE was chosen :
3 3
d new F ( x p 2, y p ) a ( x p 2) b( y p ) c
2 2
M So :
NE
d new d old a b
E
d old dy dx
Previous Choices for
Pixel Choices for Next pixel
(xp,yp) Current pixel
16
Computer Graphics
17
Computer Graphics
Initial value of d.
Start point is (x1,y1)
1 1
d start F ( x1 1, y1 ) a( x1 1) b( y1 ) c
2 2
b
ax1 by1 c a
2
b
F ( x1 , y1 ) a
2
d start dy dx / 2
Conventional to multiply by 2 to remove fraction doesn’t effect sign.
18
Computer Graphics
Midpoint algorithm
void MidpointLine(int
x1,y1,x2,y2) while (x < x2) {
{ if (d<= 0) {
int dx=x2-x1; d+=incrE;
x++
int dy=y2-y1; } else {
int d=2*dy-dx; d+=incrNE;
x++;
int increE=2*dy; y++;
int incrNE=2*(dy-dx); }
WritePixel(x,y);
x=x1; }
y=y1; }
WritePixel(x,y);
19
Computer Graphics
Circle drawing.
• Can also use Bresenham to draw circles.
M
SE
20
Computer Graphics
Circle drawing.
• Implicit form for a circle is:
2 2 2
f ( x, y ) ( x xc ) ( y yc ) r
If SE is chosen d new d old (2 x p 2 y p 5)
If E is chosen d new d old (2 x p 3)
21
Computer Graphics
22
Computer Graphics
Gupta-Sproull algorithm.
• Calculate the distance of the line and the pixel
center
• Adjust the colour according to the distance
Computer Graphics
Gupta-Sproull algorithm.
Calculate distance using features of mid-point algorithm
D v cos
vdx
dx 2 dy 2
dy
NE
dx
v Angle =
M D
25
Computer Graphics
So y (ax c )
yp+1
m
b D v
yp
E
For pixel E: xp
xp+1
x p 1 x p 1 y p 1 y p v y y p 1
So:
a ( x p 1) c
v yp
b
Computer Graphics
b dx xp E
xp+1
So:
vdx a ( x p 1) by p c F ( x p 1, y p ) / 2
Computer Graphics
2a ( x p 1) 2by p 2c py D v
2a ( x p 1) 2b( y p 1 / 2) 2b / 2 2c xp E
xp+1
F ( x p 1, y p 1 / 2) b
F (M ) b
d b
d dx
Computer Graphics