0% found this document useful (0 votes)
456 views

Computer Graphics Program 1. Write A C Program To Implement DDA Line Drawing Algorithm

This document contains 6 C programs that implement various computer graphics algorithms and transformations: 1. A DDA line drawing algorithm 2. A Bresenham line drawing algorithm 3. A DDA circle drawing algorithm 4. A 2D translation transformation 5. A 2D scaling transformation 6. A 2D rotation transformation Each program contains the necessary #include statements, function definitions, input/output statements and graphics calls to implement the given algorithm or transformation.

Uploaded by

DM STUDIO TUBE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
456 views

Computer Graphics Program 1. Write A C Program To Implement DDA Line Drawing Algorithm

This document contains 6 C programs that implement various computer graphics algorithms and transformations: 1. A DDA line drawing algorithm 2. A Bresenham line drawing algorithm 3. A DDA circle drawing algorithm 4. A 2D translation transformation 5. A 2D scaling transformation 6. A 2D rotation transformation Each program contains the necessary #include statements, function definitions, input/output statements and graphics calls to implement the given algorithm or transformation.

Uploaded by

DM STUDIO TUBE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Computer Graphics Program

1. Write a C program to implement DDA line drawing algorithm.


#include<graphics.h>
#include<stdio.h>
#include<math.h>
void main()
{
float x,y,x1,y1,x2,y2,dx,dy,steps;
int i,gd=DETECT,gm;
initgraph(&gd,&gm, "C:\\TurboC3\\BGI");
printf("\n Enter the value of x1:");
scanf("%f",&x1);
printf("\n Enter the value of y1:");
scanf("%f",&y1);
printf("\n Enter the value of x2:");
scanf("%f",&x2);
printf("\n Enter the value of y2:");
scanf("%f",&y2);
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
steps=dx;
else
steps=dy;
dx=dx/steps;
dy=dy/steps;
x=x1;
y=y1;
i=1;

while(i<=steps)
{
putpixel(x,y,3);
x=x+dx;
y=y+dy;
i=i+1;
delay(100);
}
getch();
closegraph();
}
2. Write a C program to implement Bresenham line drawing algorithm.
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int dx,dy,x,y,p,x1,x2,y1,y2;
int gd,gm;
clrscr();
printf("\n\n Enter the co=ordinates of first point : ");
scanf("%d %d",&x1,&y1);
printf("\n\n Enter the co=ordinates of second point : ");
scanf("%d %d",&x2,&y2);
dx=(x2-x1);
dy=(y2-y1);
p=2*(dy) - (dx);
x=x1;
y=y1;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
putpixel(x,y,WHITE);
setcolor(6);
setbkcolor(3);

while(x <= x2)


{
if(p<=0)
{
x=x+1;
y=y;
p=p+2*(dy);
}
else
{
x=x+1;
y=y+1;
p=p+2*(dy-dx);
}
putpixel(x,y,WHITE);
}
getch();
closegraph();
}
3. Write a C program to implement DDA circle drawing algorithm.
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm,i,val,r;
float x1,y1,x2,y2,startx,starty,e;
clrscr();
printf("Enter radius");
scanf("%d",&r);
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
x1=r*cos(0);
y1=r*sin(0);
startx=x1;
starty=y1;
i=0;
do
{
val=pow(2,i);
i++;
}
while(val<r);
e=1/pow(2,i-1);
do
{
x2=x1+y1*e;
y2=y1-e*x2;
putpixel(200+x2,200+y2,15);
x1=x2;
y1=y2;
delay(50);
}
while((y1-starty)<e||(startx-x1)>e);
getch();
}
4. Write a program to implement 2D Translation.

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int x1,y1,x2,y2,x3,y3,tx,ty,nx1,ny1,nx2,ny2,nx3,ny3,gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("Enter line values");
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);
getch();
cleardevice();
printf("enter the translation distance value");
scanf("%d%d",&tx,&ty);
nx1=x1+tx;
ny1=y1+ty;
nx2=x2+tx;
ny2=y2+ty;
nx3=x3+tx;
ny3=y3+ty;
cleardevice();
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();
}
5. Write a program to implement 2D Scaling.
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm;
int x1,x2,x3,y1,y2,y3,nx1,nx2,nx3,ny1,ny2,ny3,sx,sy;
clrscr();
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("\n Enter the points of triangle\n");
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);
getch();
cleardevice();
printf("Enter the scaling factor\n");
scanf("%d %d",&sx,&sy);
nx1=x1*sx;
ny1=y1*sy;
nx2=x2*sx;
ny2=y2*sy;
nx3=x3*sx;
ny3=y3*sy;
cleardevice();
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();
}
6. Write a program to implement 2D Rotation.
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
int x1,x2,x3,y1,y2,y3,nx1,nx2,nx3,ny1,ny2,ny3,r;
float t;
clrscr();
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("\t PROGRAM FOR BASIC TRANSFORMATION");
printf("\n Enter the points of triangle\n");
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);
getch();
cleardevice();
printf("\n Enter the angle of rotation\n");
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));
cleardevice();
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();
}

You might also like