0% found this document useful (0 votes)
111 views31 pages

Computer Graphics Combined

The document describes an algorithm to implement the Digital Differential Analyzer (DDA) algorithm for line drawing. It takes in two endpoints as input and calculates the step size. It then increments the x and y coordinates based on the step size to plot each pixel along the line until it reaches the endpoint. The code implementation includes taking input, calculating step size, incrementing x and y in a loop and plotting each pixel.

Uploaded by

verma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
111 views31 pages

Computer Graphics Combined

The document describes an algorithm to implement the Digital Differential Analyzer (DDA) algorithm for line drawing. It takes in two endpoints as input and calculates the step size. It then increments the x and y coordinates based on the step size to plot each pixel along the line until it reaches the endpoint. The code implementation includes taking input, calculating step size, incrementing x and y in a loop and plotting each pixel.

Uploaded by

verma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 31

Program-01(a)

Objective- To implement DDA algorithms for line drawing

Algorithm:

Step 1:
Read the input of the two end points of the line as (x1, y1) & (x2, y2) such that
x1 != x2 and y1 != y2

Step 2:
Calculate dx = x2 – x1 and dy = y2 – y1

Step 3:
if(dx>=dy)
step=dx
else
step=dy

Step 4: xin = dx / step & yin = dy / step

Step 5: x = x1 + 0.5 & y = y1 + 0.5

Step 6:
for(k = 0; k < step; k++)
{
x = x + xin
y = y + yin
putpixel(x, y)
}
Code:

#include <graphics.h>
#include <stdio.h>
#include<conio.h>
#include <math.h>
#include <dos.h>

void main( )
{
float x,y,x1,y1,x2,y2,dx,dy,step;
int i,gd=DETECT,gm;

initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("Enter the value of x1 and y1 : ");
scanf("%f%f",&x1,&y1);
printf("Enter the value of x2 and y2: ");
scanf("%f%f",&x2,&y2);

dx=abs(x2-x1);
dy=abs(y2-y1);

if(dx>=dy)
step=dx;
else
step=dy;

dx=dx/step;
dy=dy/step;

x=x1;
y=y1;

i=1;
while(i<=step)
{
putpixel(x,y,5);
x=x+dx;
y=y+dy;
i=i+1;
delay(100);
}
getch();
closegraph();
}
Output:
Program-01(b)

Objective- To implement DDA algorithms for circle drawing

Algorithm:

Step 1:
Read the input from keyboard for circle center and radius in (x c,yc) and r

Step 2:
Calculate the initial value of p=1-rc. Plot the first point on circle circumference
as x=0 and y=rc

Step 3:
while (x<y)
x=x+1
if(p<0)
then p=p+2*x+1
else
y=y-1
p=p+2*(x-y)+1

Step 4:
Plot the circle with calculated x and y value as plot(x c,yc,x,y)

Step5:
End the algorithm

Code:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
#include<math.h>

void circl(int xc,int yc,int rad)


{
int x=0,y=rad,p=1-rad;
void plot(int,int,int,int);
plot(xc,yc,x,y);
while(x<y)
{
x++;
if(p< 0)
p+=2*x+1;
else
{
y--;
p+=2*(x-y)+1;
}
plot(xc,yc,x,y);
}
}

void plot(int xc,int yc,int x,int y)


{
putpixel(xc+x,yc+y,1);
putpixel(xc-x,yc+y,1);
putpixel(xc+x,yc-y,1);
putpixel(xc-x,yc-y,1);
putpixel(xc+y,yc+x,1);
putpixel(xc-y,yc+x,1);
putpixel(xc+y,yc-x,1);
putpixel(xc-y,yc-x,1);
}

void main()
{
int xc,yc,r;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\TURBOC3\\BGI");
printf("Enter the center coordinates and radius\n");
scanf("%d%d%d",&xc,&yc,&r);
setbkcolor(BLACK);
setcolor(WHITE);
circl(xc,yc,r);
getch();
closegraph();
}
Output:
Program-02(a)

Objective- To implement Bresenham’s algorithms for line drawing

Algorithm:

Step1:
Input the two line endpoints and store the left endpoint in (x0,y0)

Step2:
Load (x0,y0) into frame buffer, that is, plot the first point.

Step3:
Calculate constants Δx, Δy,2Δy and 2Δy-2Δx, and obtain the starting value for
decision parameter as:
P0=2Δy-Δx

Step4:
At each xk along the line, starting at k=0, perform the following test:
If pk<0, the next point to plot is (xk+1,yk) and
Pk+1=pk+2Δy
Otherwise, the next point to plot is (xk+1,yk+1)
Pk+1=pk+2Δy-2 Δx

Step5:
Repeat step 4 Δx times
Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void drawline(int x0, int y0, int x1, int y1)


{
int dx, dy, p, x, y;

dx=x1-x0;
dy=y1-y0;

x=x0;
y=y0;

p=2*dy-dx;

while(x<x1)
{
if(p>=0)
{
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,7);
p=p+2*dy;
}
x=x+1;
}
}

int main()
{
int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");

printf("Enter co-ordinates of first point: ");


scanf("%d%d", &x0, &y0);

printf("Enter co-ordinates of second point: ");


scanf("%d%d", &x1, &y1);
drawline(x0, y0, x1, y1);
getch();
return 0;
}
Output:
Program-02(b)

Objective- To implement Bresenham’s algorithms for circle drawing.

Algorithm:

Step 1
Get the coordinates of the center of the circle and radius, and store them in x,
y, and R respectively. Set P=0 and Q=R.

Step 2
Set decision parameter D = 3 – 2R.

Step 3
Repeat through step-8 while P ≤ Q.

Step 4
Call Draw Circle (X, Y, P, Q).

Step 5
Increment the value of P.

Step 6
If D < 0 then D = D + 4P + 6.

Step 7
Else Set R = R - 1, D = D + 4(P-Q) + 10.

Step 8
Call Draw Circle (X, Y, P, Q).

Code:

#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <graphics.h>

void drawCircle(int xc, int yc, int x, int y)


{
putpixel(xc+x, yc+y, RED);
putpixel(xc-x, yc+y, RED);
putpixel(xc+x, yc-y, RED);
putpixel(xc-x, yc-y, RED);
putpixel(xc+y, yc+x, RED);
putpixel(xc-y, yc+x, RED);
putpixel(xc+y, yc-x, RED);
putpixel(xc-y, yc-x, RED);
}

void circleBres(int xc, int yc, int r)


{
int x = 0, y = r;
int d = 3 - 2 * r;
drawCircle(xc, yc, x, y);
while (y >= x)
{
x++;
if (d > 0)
{
y--;
d = d + 4 * (x - y) + 10;
}
else
d = d + 4 * x + 6;
drawCircle(xc, yc, x, y);
delay(50);
}
}

int main()
{
int xc, yc, r ;
printf("Enter the coordinate center of circle\n");
scanf("%d\n",&xc);
scanf("%d\n",&yc);
printf("Enter the radius of circle\n");
scanf("%d",&r);
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
circleBres(xc, yc, r);
getch();
return 0;
}
Output:
Program-03

Objective- To implement Mid Point Circle algorithm using C.

Algorithm:

Step1:
Input radius r and circle center (xc,yc), and obtain the first point on the
circumference of a circle centered on the origin as
(x0,y0)=(0,r)
Step2:
Calculate the initial value of decision parameter as
P0=5/4-r
Step3:
At each xk position,starting at k=0, perform the following test:
If pk<0, the next point along the circle centered on (0,0) is (x k+1,yk) and
pk+1=pk+2xk+1+1
Otherwise, the next point along the circle is (x k+1,yk-1) and
pk+1=pk+2xk+1+1-2yk+1
where 2xk+1=2xk+2 2yk+1=2yk-2

Step4:
Determine symmetry points in the other seven octants.

Step5:
Move each calculated pixel position (x,y) onto the circular path centered on
(xc,yc) and plot the coordinate values:
x=x+xc, y=y+yc
Step6:
Repeat steps 3 through 5 until x>=y

Code:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>

void drawcircle(int x0, int y0, int radius)


{
int x = radius;
int y = 0;
int err = 0;
while (x >= y)
{
putpixel(x0 + x, y0 + y, 7);
putpixel(x0 + y, y0 + x, 7);
putpixel(x0 - y, y0 + x, 7);
putpixel(x0 - x, y0 + y, 7);
putpixel(x0 - x, y0 - y, 7);
putpixel(x0 - y, y0 - x, 7);
putpixel(x0 + y, y0 - x, 7);
putpixel(x0 + x, y0 - y, 7);

if (err <= 0)
{
y += 1;
err += 2*y + 1;
}

if (err > 0)
{
x -= 1;
err -= 2*x + 1;
}
}
}

int main()
{
int gdriver=DETECT, gmode, error, x, y, r;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");

printf("Enter radius of circle: ");


scanf("%d", &r);

printf("Enter co-ordinates of center(x and y): ");


scanf("%d%d", &x, &y);
drawcircle(x, y, r);
getch();
return 0;
}
Output:
Program-4
Objective- To implement Mid Point Ellipse algorithm using C.

Algorithm:

Step1:
Input rx,ry, and ellipse centered (xc,yc) and obtain the first point on an ellipse
centered on the origin as
(x0,y0)=(0,ry)
Step2:
Calculate the initial value of the decision parameter in region 1 as
P10= ry2-rx2ry+0.25rx2
Step3:
At each xk position in region 1, starting at k=0, perform the following test:
If p1k<0, the next point along the ellipse centered on (0,0) is (x k+1,yk) and
P1k+1= P1k+2ry2xk+1+ry2
Otherwise, the next point along the circle is (x k+1,yk-1) and
P1k+1= P1k+2ry2xk+1-2rx2yk+1+ry2
with
2ry2xk+1= 2ry2xk+2ry2, 2ry2yk+1= 2ry2yk-2rx2
and continue until 2ry x>=2rx y
2 2

Step4:
Calculate the initial value of the decision parameter in region 2 using the
last point (x0,y0) calculated in region 1 as
P20= ry2(x0+1/2)2-rx2(y0-1)2-rx2ry2

Step5:
At each yk position in region 2, starting at k=0, perform the following test:
If p2k<0, the next point along the ellipse centered on (0,0) is (x k,yk-1)
P2k+1= P2k-2rx2yk+1+rx2
Otherwise, the next point along the circle is (x k+1,yk-1) and
P2k+1= P2k+2ry2xk+1-2rx2yk+1+rx2
using the same incremental calculations for x and y as in region 1.

Step6:
Determine symmetry points in the other three quadrants.

Step7:
Move each calculated pixel position (x,y) onto the elliptical path centered on
(xc,yc) and plot the coordinate values:
x=x+xc , y=y+yc
Step8:
Repeat the steps for region 1 until 2r y2x>=2rx2y
Code:

#include <stdio.h>
#include <conio.h>
#include<graphics.h>
void midptellipse(int rx, int ry, int xc, int yc)
{

float dx, dy, d1, d2, x, y;


x = 0;
y = ry;

// Initial decision parameter of region 1


d1 = (ry * ry)
- (rx * rx * ry)
+ (0.25 * rx * rx);
dx = 2 * ry * ry * x;
dy = 2 * rx * rx * y;

// For region 1
while (dx < dy) {

// Print points based on 4-way symmetry


putpixel( x + xc, y + yc,RED);
putpixel( -x + xc, y + yc,RED);
putpixel( x + xc, -y + yc,RED);
putpixel( -x + xc, -y + yc,RED);

// Checking and updating value of decision parameter based on


algorithm
if (d1 < 0) {
x++;
dx = dx + (2 * ry * ry);
d1 = d1 + dx + (ry * ry);
}
else {
x++;
y--;
dx = dx + (2 * ry * ry);
dy = dy - (2 * rx * rx);
d1 = d1 + dx - dy + (ry * ry);
}
}
// Decision parameter of region 2
d2 = ((ry * ry) * ((x + 0.5) * (x + 0.5)))
+ ((rx * rx) * ((y - 1) * (y - 1)))
- (rx * rx * ry * ry);

// Plotting points of region 2


while (y >= 0) {

// printing points based on 4-way symmetry


putpixel(x + xc, y + yc,RED);
putpixel(-x + xc, y + yc,RED);
putpixel(x + xc, -y + yc,RED);
putpixel(-x + xc, -y + yc,RED);

// Checking and updating parameter value based on algorithm


if (d2 > 0) {
y--;
dy = dy - (2 * rx * rx);
d2 = d2 + (rx * rx) - dy;
}
else {
y--;
x++;
dx = dx + (2 * ry * ry);
dy = dy - (2 * rx * rx);
d2 = d2 + dx - dy + (rx * rx);
}
}
}

int main()
{ int gdriver=DETECT, gmode;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
// To draw a ellipse of major and minor radius 15, 10 centred at (50, 50)
midptellipse(10, 15, 50, 50);
getch();
return 0;
}
Output:
Program-5

Objective- To perform 2D Transformations such as translation, rotation,


scaling, reflection and sharing.

Code:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<math.h>
void main()
{
int gm;
int gd=DETECT;
int x1,x2,x3,y1,y2,y3,nx1,nx2,nx3,ny1,ny2,ny3,c;
int sx,sy,xt,yt,r;
float t;
initgraph(&gd,&gm,"c:\turboc3\bgi:");
printf("\t Program for basic transactions");
printf("\n\t Enter the points of triangle");
setcolor(1);
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
printf("\n 1.Transaction\n 2.Rotation\n 3.Scalling\n 4.exit");
printf("Enter your choice:");
scanf("%d",&c);
switch(c)
{
case 1:
printf("\n Enter the translation factor");
scanf("%d%d",&xt,&yt);
nx1=x1+xt;
ny1=y1+yt;
nx2=x2+xt;
ny2=y2+yt;
nx3=x3+xt;
ny3=y3+yt;
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();
case 2:
printf("\n Enter the angle of rotation");
scanf("%d",&r);
t=3.14*r/180;
nx1=abs(x1*cos(t)-y1*sin(t));
ny1=abs(x1*sin(t)+y1*cos(t));
nx2=abs(x2*cos(t)-y2*sin(t));
ny2=abs(x2*sin(t)+y2*cos(t));
nx3=abs(x3*cos(t)-y3*sin(t));
ny3=abs(x3*sin(t)+y3*cos(t));
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();

case 3:
printf("\n Enter the scalling factor");
scanf("%d%d",&sx,&sy);
nx1=x1*sx;
ny1=y2*sy;
nx2=x2*sx;
ny2=y2*sy;
nx3=x3*sx;
ny3=y3*sy;
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();

case 4:
break;
default:
printf("Enter the correct choice");
}
getch();
closegraph();

}
Output:
Program-6

Objective- To implement Cohen–Sutherland 2D clipping and window–


viewport mapping.

Algorithm:

1. Read 2 end points of line as p1(x1,y1) and p2(x2,y2).

2. Read 2 corner points of the clipping window (left-top and right-bottom) as


(wx1,wy1) and (wx2,wy2)

3. Assign the region codes for 2 endpoints p1 and p2 using following steps:-
initialize code with 0000
a) Set bit 1 if x<wx1
b) Set bit 2 if x>wx2
c) Set bit 3 if y<wy2
d) Set bit 4 if y>wy1

4. Check for visibility of line


a. If region codes for both endpoints are zero then line is completely visible.
Draw the line go to step 9.
b. If region codes for endpoints are not zero and logical ANDing of them is
also nonzero then line is invisible. Discard the line and move to step 9.
c. If it does not satisfy 4.a and 4.b then line is partially visible.

5. Determine the intersecting edge of clipping window as follows:-


a. If region codes for both endpoints are nonzero find intersection points p1’
and p2’ with boundary edges.
b. If region codes for any one end point is non zero then find intersection
point p1’ or p2’.

6. Divide the line segments considering intersection points.

7. Reject line segment if any end point of line appears outside of any boundary.

8. Draw the clipped line segment.

9. Stop.

Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>

typedef struct coordinate


{
int x,y;
char code[4];
}PT;

void drawwindow();
void drawline(PT p1,PT p2);
PT setcode(PT p);
int visibility(PT p1,PT p2);
PT resetendpt(PT p1,PT p2);

void main()
{
int gd=DETECT,v,gm;
PT p1,p2,p3,p4,ptemp;

printf("\nEnter x1 and y1\n");


scanf("%d %d",&p1.x,&p1.y);
printf("\nEnter x2 and y2\n");
scanf("%d %d",&p2.x,&p2.y);

initgraph(&gd,&gm,"c:\\turboc3\\bgi");
drawwindow();
delay(500);

drawline(p1,p2);
delay(500);
cleardevice();

delay(500);
p1=setcode(p1);
p2=setcode(p2);
v=visibility(p1,p2);
delay(500);

switch(v)
{
case 0: drawwindow();
delay(500);
drawline(p1,p2);
break;
case 1: drawwindow();
delay(500);
break;
case 2: p3=resetendpt(p1,p2);
p4=resetendpt(p2,p1);
drawwindow();
delay(500);
drawline(p3,p4);
break;
}

delay(5000);
getch();
closegraph();
}

void drawwindow()
{
line(150,100,450,100);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
}

void drawline(PT p1,PT p2)


{
line(p1.x,p1.y,p2.x,p2.y);
}

PT setcode(PT p) //for setting the 4 bit code


{
PT ptemp;

if(p.y<100)
ptemp.code[0]='1'; //Top
else
ptemp.code[0]='0';

if(p.y>350)
ptemp.code[1]='1'; //Bottom
else
ptemp.code[1]='0';

if(p.x>450)
ptemp.code[2]='1'; //Right
else
ptemp.code[2]='0';

if(p.x<150)
ptemp.code[3]='1'; //Left
else
ptemp.code[3]='0';

ptemp.x=p.x;
ptemp.y=p.y;

return(ptemp);
}

int visibility(PT p1,PT p2)


{
int i,flag=0;

for(i=0;i<4;i++)
{
if((p1.code[i]!='0') || (p2.code[i]!='0'))
flag=1;
}

if(flag==0)
return(0);

for(i=0;i<4;i++)
{
if((p1.code[i]==p2.code[i]) && (p1.code[i]=='1'))
flag='0';
}

if(flag==0)
return(1);

return(2);
}

PT resetendpt(PT p1,PT p2)


{
PT temp;
int x,y,i;
float m,k;
if(p1.code[3]=='1')
x=150;

if(p1.code[2]=='1')
x=450;

if((p1.code[3]=='1') || (p1.code[2]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(p1.y+(m*(x-p1.x)));
temp.y=k;
temp.x=x;

for(i=0;i<4;i++)
temp.code[i]=p1.code[i];

if(temp.y<=350 && temp.y>=100)


return (temp);
}

if(p1.code[0]=='1')
y=100;

if(p1.code[1]=='1')
y=350;

if((p1.code[0]=='1') || (p1.code[1]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(float)p1.x+(float)(y-p1.y)/m;
temp.x=k;
temp.y=y;

for(i=0;i<4;i++)
temp.code[i]=p1.code[i];

return(temp);
}
else
return(p1);
}
Output:

Figure 1 Before Clipping

Figure 2 After Clipping


Program-7

Objective- To implement Liang Barksy Line Clipping Algorithm.

Algorithm:

1. Read 2 endpoints of line as p1 (x1, y1) & p2 (x2, y2).

2. Read 2 corners (left-top & right-bottom) of the clipping window as (xwmin,


ywmin, xwmax, ywmax).

3. Calculate values of parameters pi and qi for i = 1, 2, 3, 4 such that


p1 = -dx, q1 = x1 – xwmin
p2 = dx, q2 = xwmax – x1
p3 = -dy, q3 = y1 – ywmin
p4 = dy, q4 = ywmax – y1

4. if pi = 0 then line is parallel to ith boundary


if qi < 0 then line is completely outside boundary so discard line
else, check whether line is horizontal or vertical and then check the line
endpoints with the corresponding boundaries.

5. Initialize t1 & t2 as
t1 = 0 & t2 = 1

6. Calculate values for qi/pi for i = 1, 2, 3, 4.

7. Select values of qi/pi where pi < 0 and assign maximum out of them as t1.

8. Select values of qi/pi where pi > 0 and assign minimum out of them as t2.

9. if (t1 < t2)


{
xx1 = x1 + t1dx
xx2 = x1 + t2dx
yy1 = y1 + t1dy
yy2 = y1 + t2dy
line (xx1, yy1, xx2, yy2)
}

10. Stop.
Advantages
1. More efficient than other algorithms as line intersection with boundaries
calculations are reduced.
2. Intersections of line are computed only once.

Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>

void main()
{
int i,gd=DETECT,gm;
int x1,y1,x2,y2,xmin,xmax,ymin,ymax,xx1,xx2,yy1,yy2,dx,dy;
float t1,t2,p[4],q[4],temp;

x1=120;
y1=120;
x2=300;
y2=300;

xmin=100;
ymin=100;
xmax=250;
ymax=250;

initgraph(&gd,&gm,"c:\\turboc3\\bgi");
rectangle(xmin,ymin,xmax,ymax);
dx=x2-x1;
dy=y2-y1;

p[0]=-dx;
p[1]=dx;
p[2]=-dy;
p[3]=dy;

q[0]=x1-xmin;
q[1]=xmax-x1;
q[2]=y1-ymin;
q[3]=ymax-y1;

for(i=0;i<4;i++)
{
if(p[i]==0)
{
printf("line is parallel to one of the clipping boundary");
if(q[i]>=0)
{
if(i<2)
{
if(y1<ymin)
{
y1=ymin;
}

if(y2>ymax)
{
y2=ymax;
}

line(x1,y1,x2,y2);
}

if(i>1)
{
if(x1<xmin)
{
x1=xmin;
}

if(x2>xmax)
{
x2=xmax;
}

line(x1,y1,x2,y2);
}
}
}
}

t1=0;
t2=1;

for(i=0;i<4;i++)
{
temp=q[i]/p[i];
if(p[i]<0)
{
if(t1<=temp)
t1=temp;
}
else
{
if(t2>temp)
t2=temp;
}
}

if(t1<t2)
{
xx1 = x1 + t1 * p[1];
xx2 = x1 + t2 * p[1];
yy1 = y1 + t1 * p[3];
yy2 = y1 + t2 * p[3];
line(xx1,yy1,xx2,yy2);
}

delay(5000);
getch();
closegraph();
}
Output:

You might also like