Professional Documents
Culture Documents
Computer Graphics (CSE 4103)
Computer Graphics (CSE 4103)
University of Dhaka
Lecture 2-3
Basic Raster Graphics Algorithm for
Drawing 2D Primitives
Line Drawing
Line drawing is fundamental to computer graphics.
We must have fast and efficient line drawing functions.
B(x2,y2)
y=mx+c
A(x1,y1)
x1 x2 x
Line Drawing - Analytical Method..
Current (6,3)
Pixel To find (xi+1, yi+1):
(xi, yi) (5,2) (6,2)
xi+1 = x1+1
(6,1) yi+1 = ?
=>yi+1 = yi + mx
Formulating Incremental Method..
void IncrementalLineDraw(int x1, int y1, int x2, int y2)
{
double m = (double)(y2-y1)/(x2-x1);
double y=y1;
int x;
for (int x=x1 ; x <=x2 ; x++){
SetPixel(x, Round(y));
y=y+m;
}
}
Also known as Digital Differential Analyzer (DDA)
Formulating Incremental Method..
13
x Round(y) y+m
12
6 9 9.6
7 10 10.2 11
8 10 10.8 10
9 11 11.4
9
10 11 12.0
11 12 12.6 6 7 8 9 10 11 12 13
Incremental Method
Assumption:
Slope < 1
Current
Pixel
xi+1 = xi+1
yi+1 = Either yi or yi+1
Midpoint Algorithm..
F(x,y) < 0
(for any point above line) F(x,y) = 0
F (x,y) > 0
(for any point below line)
Midpoint Algorithm..
Line
( xi+1, yi+1)
Candidate Pixels
Midpoint
Current Pixel
( xi, yi)
( xi+1, yi)
If the midpoint is below the line, then the next pixel is (xi+1, yi+1).
If the midpoint is above the line, then the next pixel is (xi+1, yi).
Midpoint Algorithm..
y=mx+B = (dy/dx).x+B F(x,y) = ax+by+c = 0
dy.x - dx.y+ dx.B = 0
N
NE
W E M
S
(xi, yi) E
M dnew1- dold =a
E
(xi, yi)
Previous
Pixel
Choices Choices
for current for next
dnew1 =dold +a
pixel pixel
= dold + dy
Midpoint Algorithm..
d = F(xi+1, yi+1/2) = a(xi+1)+b(yi+1/2)+c = 0
If NE is chosen, next M is (xi+2, yi+3/2)
= dold + dy - dx
Midpoint Algorithm..
Since the first pixel is simply the first endpoint (x0, y0), we can directly
calculate initial value of d for choosing between E and NE. The first
midpoint is (x0+1, y0+1/2)
F(x0+1, y0+1/2) = a(x0+1)+b(y0+1/2)+c
= a.x0+ a + b.y0+ b/2+c
= (a.x0+b.y0+c)+ a+ b/2
= F(x0, y0) +a+ b/2
= a + b/2 (as point (x0, y0) is on the line)
dstart=a+b/2 = dy dx/2
Midpoint Algorithm..
dstart=a+b/2 = dy dx/2
F(x,y) = 2(ax+by+c)
This multiply each constant and the decision variable by 2, but
does not affect the sign of the decision variable.
dstart=a+b/2 = 2*dy dx
dnew1 (for selecting E) = dold + 2*dy
dnew2 (for selecting NE) = dold + 2(dy - dx)
Midpoint Algorithm..
void midpointline(int x1, int y1, int x2, int y2)
{
int dx, dy, d, incrE, incrNE;
dx=x2-x1; dy=y2-y1;
d=2*dy-dx;
incrE=2*dy;
incrNE=2*(dy-dx);
while(x1<=x2)
{
putpixel(x1, y1);
if(d<=0)
d+=incrE;
else
{
d+=incrNE;
y1++;
}
x1++;
}
}
Midpoint Algorithm..
N NE
N
NW
NE
W
E
W E
S
Midpoint Algorithm..
N
(-y, x) (y, x)
(-x, y) (x, y)
W E
(x, -y)
(-x, -y)
(-y, -x) (y, -x)
S
8-way Symmetry
The above Thank You is written in Raster
Graphics using line drawing algorithm