Computer Graphics: Line & Circle Drawing
Computer Graphics: Line & Circle Drawing
Computer Graphics: Line & Circle Drawing
Computer Graphics
Lecture 3
Line & Circle Drawing
Computer Graphics
2
Towards the Ideal Line
We can only do a discrete approximation
Illuminate pixels as close to the true path as
possible, consider bi-level display only
Pixels are either lit or not lit
Computer Graphics
3
What is an ideal line
Must appear straight and continuous
Only possible axis-aligned and 45
o
lines
Must interpolate both defining end points
Must have uniform density and intensity
Consistent within a line and over all lines
What about antialiasing?
Must be efficient, drawn quickly
Lots of them are required!!!
Computer Graphics
4
Simple Line
Based on slope-intercept
algorithm from algebra:
y = mx + b
Simple approach:
increment x, solve for y
Floating point arithmetic
required
Computer Graphics
5
Does it Work?
It seems to work okay for lines with
a slope of 1 or less,
but doesnt 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.
Computer Graphics
6
Modify algorithm per octant
OR, increment along x-axis if dy<dx else increment along y-axis
Computer Graphics
7
DDA algorithm
DDA = Digital Differential Analyser
finite differences
Treat line as parametric equation in t :
) ( ) (
) ( ) (
1 2 1
1 2 1
y y t y t y
x x t x t x
+ =
+ =
) , (
) , (
2 2
1 1
y x
y x
Start point -
End point -
Computer Graphics
8
DDA Algorithm
Start at t = 0
At each step, increment t by dt
Choose appropriate value for dt
Ensure no pixels are missed:
Implies: and
Set dt to maximum of dx and dy
) ( ) (
) ( ) (
1 2 1
1 2 1
y y t y t y
x x t x t x
+ =
+ =
dt
dy
y y
dt
dx
x x
old new
old new
+ =
+ =
1 <
dt
dx
1 <
dt
dy
1 2
1 2
y y dy
x x dx
=
=
Computer Graphics
9
DDA algorithm
line(int x1, int y1, int x2, int y2)
{
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;
}
}
n - range of t.
Computer Graphics
10
DDA algorithm
Still need a lot of floating point arithmetic.
2 rounds and 2 adds per pixel.
Is there a simpler way ?
Can we use only integer arithmetic ?
Easier to implement in hardware.
Computer Graphics
11
Observation on lines.
while( n-- )
{
draw(x,y);
move right;
if( below line )
move up;
}
Computer Graphics
12
Testing for the side of a line.
Need a test to determine which side of a line
a pixel lies.
Write the line in implicit form:
0 ) , ( = + + = c by ax y x F
If (b<0) F<0 for points above the line, F>0 for
points below.
Computer Graphics
13
Testing for the side of a line.
Need to find coefficients a,b,c.
Recall explicit, slope-intercept form :
So:
0 ) , ( = + + = c by ax y x F
b x
dx
dy
y b mx y + = + = so and
0 . . ) , ( = + = c y dx x dy y x F
Computer Graphics
Decision variable.
Previous
Pixel
(xp,yp)
Choices for
Current pixel
Choices for
Next pixel
Lets assume dy/dx < 0.5 (we can use symmetry)
Evaluate F at point M
Referred to as decision variable
)
2
1
, 1 ( + + =
p p
y x F d
M
NE
E
Computer Graphics
15
Decision variable.
Evaluate d for next pixel, Depends on whether E or NE Is chosen :
If E chosen :
c y b x a y x F d
p p p p new
+ + + + = + + = )
2
1
( ) 2 ( )
2
1
, 2 (
But recall :
c y b x a
y x F d
p p
p p old
+ + + + =
+ + =
)
2
1
( ) 1 (
)
2
1
, 1 (
So :
dy d
a d d
old
old new
+ =
+ =
M
E
NE
Previous
Pixel
(xp,yp)
Choices for
Current pixel
Choices for
Next pixel
Computer Graphics
16
Decision variable.
If NE was chosen :
c y b x a y x F d
p p p p new
+ + + + = + + = )
2
3
( ) 2 ( )
2
3
, 2 (
So :
dx dy d
b a d d
old
old new
+ =
+ + =
M
E
NE
Previous
Pixel
(xp,yp)
Choices for
Current pixel
Choices for
Next pixel
Computer Graphics
17
Summary of mid-point algorithm
Choose between 2 pixels at each step based
upon the sign of a decision variable.
Update the decision variable based upon
which pixel is chosen.
Start point is simply first endpoint (x1,y1).
Need to calculate the initial value for d
Computer Graphics
18
Initial value of d.
2
)
2
1
( ) 1 ( )
2
1
, 1 (
1 1
1 1 1 1
b
a c by ax
c y b x a y x F d
start
+ + + + =
+ + + + = + + =
But (x1,y1) is a point on the line, so F(x1,y1) =0
2 / dx dy d
start
=
Conventional to multiply by 2 to remove fraction doesnt effect sign.
2
) , (
1 1
b
a y x F + + =
Start point is (x1,y1)
Computer Graphics
19
Midpoint algorithm
void MidpointLine(int
x1,y1,x2,y2)
{
int dx=x2-x1;
int dy=y2-y1;
int d=2*dy-dx;
int increE=2*dy;
int incrNE=2*(dy-dx);
x=x1;
y=y1;
WritePixel(x,y);
while (x < x2) {
if (d<= 0) {
d+=incrE;
x++
} else {
d+=incrNE;
x++;
y++;
}
WritePixel(x,y);
}
}
Computer Graphics
20
Circle drawing.
Can also use Bresenham to draw circles.
Use 8-fold symmetry
Choices for
Next pixel
M
E
SE
Previous
Pixel
Choices for
Current pixel
Computer Graphics
21
Circle drawing.
Implicit form for a circle is:
) 3 2 ( chosen is E If
) 5 2 2 ( chosen is SE If
+ + =
+ + =
p old new
p p old new
x d d
y x d d
Functions are linear equations in terms of (xp,yp)
Termed point of evaluation
2 2 2
) ( ) ( ) , ( r y y x x y x f
c c
+ =
Computer Graphics
22
Summary of line drawing so far.
Explicit form of line
Inefficient, difficult to control.
Parametric form of line.
Express line in terms of parameter t
DDA algorithm
Implicit form of line
Only need to test for side of line.
Bresenham algorithm.
Can also draw circles.
Computer Graphics
23
Problems with Bresenham algorithm
Pixels are drawn as a single line unequal
line intensity with change in angle.
Pixel density = n pixels/mm
Pixel density = \2.n pixels/mm
Can draw lines in darker colours
according to line direction.
-Better solution : antialiasing !
-(eg. Gupta-Sproull algorithm)
Computer Graphics
Gupta-Sproull algorithm.
Calculate the distance of the line and the pixel
center
Adjust the colour according to the distance
Computer Graphics
25
Gupta-Sproull algorithm.
M
NE
Calculate distance using features of mid-point algorithm
D
Angle = C
v
dy
dx
2 2
cos
dy dx
vdx
v D
+
=
= |
E
Computer Graphics
Gupta-Sproull algorithm (cont)
Recall from the midpoint algorithm:
So
For pixel E:
So:
( ) fraction) avoid to (doubled 0 2 ) , ( = + + = c by ax y x F
= y
=
+1 p
x
= v
x
p
NE
m
E
x
p+1
Line to draw
v
y
p
y
p
+1
O
D
b
c ax
+ ) (
=
+1 p
y
p
y = v
1 +
p
y y 1 +
p
x
p
p
y
b
c x a
+ + ) 1 (
Computer Graphics
Gupta-Sproull algorithm (cont)
From previous slide:
From the midpoint computation,
So:
dx b =
p
p
y
b
c x a
v
+ +
=
) 1 (
2 / ) , 1 ( ) 1 (
p p p p
y x F c by x a vdx + = + + + =
x
p
NE
m
E
x
p+1
Line to draw
v
y
p
y
p+1
O
D
Computer Graphics
Gupta-Sproull algorithm (cont)
From the midpoint algorithm, we had the decision
variable (remember?)
Going back to our previous equation:
)
2
1
, ( ) (
1
+ = =
+ p p
y x F M F d
x
p
NE
m
E
x
p+1
Line to draw
v
y
p
y
p
+1
O
D
) , 1 ( 2
p p
y x F vdx + =
c by x a
p p
2 2 ) 1 ( 2 + + + =
c b y b x a
p p
2 2 / 2 ) 2 / 1 ( 2 ) 1 ( 2 + + + + =
b y x F
p p
+ + = ) 2 / 1 , 1 (
b M F = ) (
b d =
dx d + =
Computer Graphics
Gupta-Sproull algorithm (cont)
So,
And the denominator is constant
Since we are blurring the line, we also need to
compute the distances to points y
p
1 and y
p
+ 1
vdx dx dx v y
vdx dx dx v y
p
p
2 2 ) 1 ( 2 1 for numerator
2 2 ) 1 ( 2 1 for numerator
+ = + +
=
2 2
2 dy dx
dx d
D
+
+
=
Computer Graphics
Gupta-Sproull algorithm (cont)
If the NE pixel had been chosen:
vdx dx dx v y
vdx dx dx v y
p
p
2 2 ) 1 ( 2 for numerator
2 2 ) 1 ( 2 2 for numerator
+ = +
= +
dx d
b d
b M F
b y x F
c b y b x a
c by x a
y x F vdx
p p
p p
p p
p p
=
+ =
+ =
+ + + =
+ + + + + =
+ + + + =
+ + =
) (
) 2 / 1 , 1 (
2 2 / 2 ) 2 / 1 ( 2 ) 1 ( 2
2 ) 1 ( 2 ) 1 ( 2
) 1 , 1 ( 2
Computer Graphics
Gupta-Sproull algorithm (cont)
Compute midpoint line algorithm, with the following alterations:
At each iteration of the algorithm:
If the E pixel is chosen, set numerator = d + dx
If the NE pixel is chosen, set numerator = d dx
Update d as in the regular algorithm
Compute D = numerator/denominator
Color the current pixel according to D
Compute D
upper
= (2dx-2vdx)/denominator
Compute D
lower
= (2dx+2vdx)/denominator
Color upper and lower accordingly