Computer Graphics Comp565: Chapter - 2 Simple Drawing Algorithms
Computer Graphics Comp565: Chapter - 2 Simple Drawing Algorithms
Computer Graphics Comp565: Chapter - 2 Simple Drawing Algorithms
Comp565
Chapter – 2
Simple Drawing Algorithms
Introduction
2
Line Drawing Algorithms…
3
Line Drawing Algorithms…
4
Line Drawing Algorithms…
5
Line Drawing Algorithms…
6
Line Drawing Algorithms…
7
Line Drawing Algorithms…
5. x = xs = 1
y = ys = 1
6. plot a point at (x, y) position
putpixel(round(x), round(y), 15)
7. increment the values of x and y
x = x + xinc = 1 + 1= 2
y = y+ yinc = 1 + 2/3 = 5/3
8. putpixel(round(x), round(y),15)
x = x + xinc = 2 + 1= 3
y = y+ yinc = 5/3+ 2/3 = 7/3
8
Line Drawing Algorithms…
9
Line Drawing Algorithms…
Procedure:
void lineDDA(float xs, float ys, float xe, float ye)
{
float dx, dy, x, y, xinc, yinc, step;
dx = xe – xs;
dy = ye –Ys;
if (abs(dx) > abs(dy))
step = abs(dx);
else
step = abs(dy);
xinc = dx /step;
yinc = dy /step;
10
Line Drawing Algorithms…
x = xs;
y = ys;
putpixel(Round(x),Round(y),15);
for(int i=0; i<=step; i++)
{
x = x+xinc;
y = y+yinc;
putpixel(Round(x),Round(y),15);
}
}
11
Line Drawing Algorithms…
12
Line Drawing Algorithms…
13
Line Drawing Algorithms…
4. If p < 0
If xs < xe
xs = xs + 1
Else
xs = xs – 1
p = p + 2∆y
Otherwise //If p >= 0
If xs < xe, then
xs = xs + 1
Else
xs = xs – 1
If ys < ye, then
ys = ys + 1
14
Line Drawing Algorithms…
Else
ys = ys – 1
p = p + 2(∆y -∆x)
5. Plot the point at the position(xs,ys)
putpixel(xs, ys, color)
6. Repeat step 4 and 5 until xs = xe
15
Line Drawing Algorithms…
16
Line Drawing Algorithms…
4. If p < 0, then
If ys < ye, then
ys = ys + 1
Else
ys = ys – 1
p = p + 2∆x
Otherwise //If p >= 0
If xs < xe, then
xs = xs + 1
Else
xs = xs – 1
17
Line Drawing Algorithms…
18
Line Drawing Algorithms…
xs = xs +1 = 1 + 1=2
ys = ys +1 = 1 + 1=2
p = p + 2(∆y -∆x)
= 1 + 2(4 – 7) = -5 < 0
putpixel(xs,ys,15);
xs = xs +1 = 2 + 1=3
ys = ys = 2
p = p + 2∆y = -5+2*4=3 > 0
putpixel(xs,ys,15);
20
Line Drawing Algorithms…
xs = xs +1 = 3 + 1=4
ys = ys +1 = 2 + 1=3
p = p + 2(∆y -∆x) =3+2(4-7)= -3<0
putpixel(xs,ys,15);
xs = xs +1 = 4 + 1=5
ys = ys = 3
p = p + 2∆y = -3 + 2(4) = 5 > 0
putpixel(xs,ys,15);
21
Line Drawing Algorithms…
xs = xs +1 = 5+ 1=6
ys = ys = 3+1=4
p = p + 2(∆y -∆x) = 5 + 2(4-7) = -1< 0
putpixel(xs,ys,15);
xs = xs +1 = 6 + 1=7
ys = ys = 4
p = p + 2∆y = -1 + 2(4) = 7 > 0
putpixel(xs,ys,15);
22
Line Drawing Algorithms…
xs = xs +1 = 7+ 1=8
ys = ys = 4+1=5
p = p + 2(∆y -∆x) = 7 + 2(4-7) = 1>0
putpixel(xs,ys,15);
Since xs = 8 = xe (stop)
23
Line Drawing Algorithms…
Graphically:
24
Line Drawing Algorithms…
Procedure:
void lineBresenham(int xs, int ys, int xe, int ye)
{
int dx, dy,p, p1, p2,x,y,xend;
dx =abs(xe –xs);
dy =abs(ye –ys);
p = 2*dy - dx;
p1 = 2*dy;
p2 = 2*(dy - dx);
25
Line Drawing Algorithms…
putpixel(x,y,15);
while(x<xend)
{
x++;
if(p<0)
p+=p1;
else
{
y++;
p+=p2;
}
putpixel(x, y,15);
}} 27
Circle Drawing Algorithm
28
Circle Drawing Algorithm…
29
Circle Drawing Algorithm…
30
Circle Drawing Algorithm
Algorithm: Bresenham’s Circle Drawing Algorithm
Steps:
1. Read the radius of a circle
2. Initialize the variables
x=0
y=r
p = 3 – 2 * r (Decision parameter)
3. If p < 0 then
x = x+1
p=p+4*x+6
Otherwise//p>=0
y=y-1
x=x+1
p = p + 4(x - y) + 10
4. Plot point at (x,y) position
putpixel(x,y,color)
5. Repeat steps 3 & 4 until x<=y
32
Circle Drawing Algorithm…
Procedure
void bresenCircle(int xc, yc,int r)
{
int x,y,p;
x=0;
y=r;
p=3-2*r;
while(x<=y)
{
if(p<0)
p=p+(4*x+6);
33
Circle Drawing Algorithm…
else
{
y=y-1;
p=p+4*(x-y)+10;
}
plot(xc,yc,x,y);
x=x+1;
}
}
34
Circle Drawing Algorithm…
36
Ellipse Drawing Algorithm…
Algorithm
Steps:
1. Get the centre point as (xc, yc)
2. Get the lengths of semi-major and semi-minor
axes as r1 & r2
3. Calculate the value of t
t=PI/180
4. Initialize i to zero (i=0)
5. Compute the value for d
d=i*t
37
Ellipse Drawing Algorithm…
38
Ellipse Drawing Algorithm…
Procedure:
void ellipseDraw(float xc,float yc,float r1,float r2)
{
float x,y,t,d, i;
t=3.14/180;
for(i=0;i<360;i++)
{
d=i*t;
x=xc+ceil(r1*sin(d));
y=yc+ceil(r2*cos(d));
putpixel(x,y,15);
}}
39
Character Generation and Attributes
40
Character Generation and Attributes…
41
Character Generation and Attributes…
42
END
THE END!
43