You are on page 1of 24

Department of Computer Science and Engineering,

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.

Rasterization Problem: Given only the two end points, how to


compute the intermediate pixels, so that the set of pixels closely
approximate the ideal line.
Line Drawing - Analytical Method

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 = ?

Assumes that the next pixel to be set is on the next column of


pixels (Incrementing the value of x !)
Not valid if slope of the line is large.
Formulating Incremental Method
y=mx+B
yi+1 =mxi+1+B
If, x= 1, then
=>yi+1 =m(xi+x)+B yi+1 = yi + m
=>yi+1 =mxi+ mx+ B
m= dy/dx = (y2-y1)/ (x2-x1)
=>yi+1 =(mxi+ B) + mx

=>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..

Does not involve any floating point multiplication


Involves floating point addition
Requires round-off function
Incremental Method (Example)
Compute which pixels should be turned on to represent the line from (6,9) to (11,12).

m = (12-9)/(11-6) = 3/5 = 0.6


x=6
y=9

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

Next pixel on next column Next pixel on next row


(when slope is small) (when slope is large)
Midpoint Algorithm
Midpoint algorithm is an incremental algorithm

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)

Coordinates of Midpoint = ( xi+1, yi+(1/2) )


Midpoint Algorithm..

Midpoint Below Line Midpoint Above Line

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

a= dy, b = -dx, c = dx.B


Function for midpoint

F(M) = F(xi+1, yi+1/2) = a(xi+1)+b(yi+1/2)+c = 0

Let d = F(xi+1, yi+1/2) = a(xi+1)+b(yi+1/2)+c = 0


Here, d is a decision variable
Midpoint Algorithm..
d = F(xi+1, yi+1/2) = a(xi+1)+b(yi+1/2)+c = 0

N
NE

W E M

S
(xi, yi) E

Previous Choices Choices


Pixel for current for next
pixel pixel

Condition Point to select Resulting y value


If d>0 NE is selected yi+1 = yi +1
If d<0 E is selected yi+1 = yi
If d=0 Chose any point (we select E here) yi+1 = yi
Midpoint Algorithm..
d = F(xi+1, yi+1/2) = a(xi+1)+b(yi+1/2)+c = 0
If E is chosen, next M is (xi+2, yi+1/2)

dnew1 = F(xi+2, yi+1/2) = a(xi+2)+b(yi+1/2)+c = 0

dold (or d) = F(xi+1, yi+1/2) = a(xi+1)+b(yi+1/2)+c = 0


NE

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)

dnew2 = F(xi+2, yi+3/2) = a(xi+2)+b(yi+3/2)+c = 0

dold (or d) = F(xi+1, yi+1/2) = a(xi+1)+b(yi+1/2)+c = 0


NE

M dnew2- dold =a+b


E
(xi, yi)
Previous
Pixel
Choices Choices
for current for next
dnew2 = dold +a + b
pixel pixel

= 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

To eliminate the fraction in dstart , we redefine our original F by


multiplying it with 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

You might also like