Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 7

1] Display line using DDA line drawing algorithm ?

#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
void main()
{
float x1,y1,x2,y2,dx,dy,len;
int x,y,i,gmode,gdriver=DETECT;
initgraph (&gdriver, &gmode,"c:\\tc\\bgi");
cleardevice();
printf("Enter the value of x1,y1,x2,y2: ");
scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
{
len=dx;
}
else
{
len=dy;
}
dx=(x2-x1)/len;
dy=(y2-y1)/len;
x=x1+0.5;
y=y1+0.5;
i=1;
while(i<=len)
{
putpixel(x,y,20);
x=x+dx;
y=y+dy;
i=i+1;
delay(200);
}
getch();
closegraph();
}
2] Draw line using Bresenhams Line drawing algorithm?
#include<stdio.h>
#include<math.h>
#include<graphics.h>
#include<conio.h>
void main()
{
float x1,y1,x2,y2,e,dx,dy;
int x,y,i,gmode,gdriver=DETECT;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
cleardevice();
printf("Enter the value of x1,y1,x2,y2: ");
scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
dx=abs(x2-x1);
dy=abs(y2-y1);
x=x1;
y=y1;
e=2*dy-dx;
i=1;
do
{
putpixel(x,y,15);
while(e>=0)
{
y=y+1;
e=e-2*dx;
}
x=x+1;
e=e+2*dy;
i=i+1;
}while(i<=dx);
getch();
closegraph();
}
3] Draw circle using Bresenhams circle drawing algorithm?
#include<stdio.h>
#include<math.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
void main()
{
float x,y,r,d;
int i,gmode, gdriver=DETECT;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
cleardevice();
printf("Enter the radius: ");
scanf("%f",&r);
d=3-2*i;
x=0;
y=r;
do
{
putpixel(200+x,200+y,13);
putpixel(200+x,200-y,31);
putpixel(200-x,200+y,39);
putpixel(200-x,200-y,93);
putpixel(200+y,200+x,65);
putpixel(200+y,200-x,56);
putpixel(200-y,200+x,91);
putpixel(200-y,200-x,19);
if(d<0)
{
d=d+4*x+6;
}
else
{
d=d+4*(x-y)+10;
y=y-1;
}
x=x+1;
delay(100);
}
while(x<y);
getch();
closegraph();
}
4] Display thickline using DDA line drawing algorithm?
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void main()
{
float x,y,x1,y1,x2,y2,dx,dy,e;
int gdriver = DETECT, gmode,i;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); cleardevice();
printf("\n enter x1,y1,x2,y2: ");
scanf("%f%f%f%f", &x1,&y1,&x2,&y2);
dx=abs(x2-x1);
dy=abs(y2-y1);
x=x1;
y=y1;
e=2*dy-dx;
i=1;
do
{
putpixel(x,y,35) ;
putpixel(x+1,y,35) ;
putpixel(x+2,y,35) ;
putpixel(x+3,y,35) ;
while(e>=0)
{
y=y+1;
e=e-2*dx;
}
x=x+1;
e=e+2*dy;
i=i+1;
delay(200);
}
while(i<=dx);
getch();
closegraph();
}
5] Display dotted line using DDA line drawing algorithm?
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void main()
{
float x,y,x1,y1,x2,y2,dx,dy,len;
int gdriver = DETECT, gmode,i;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
cleardevice();
printf("\n enter x1,y1,x2,y2: ");
scanf("%f%f%f%f", &x1,&y1,&x2,&y2);
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
len=dx;
else
len=dy;
dx=(x2-x1)/len;
dy=(y2-y1)/len;
x=x1+0.5;
y=y1+0.5;
i=1;
while(i<=len)
{
putpixel(x,y,35);
x=x+9;
y=y+9;
x=x+dx;
y=y+dy;
i=i+1;
delay(10);
}
getch();
closegraph();
}
6] Display Dashline using DDA line drawing algorithm ?
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void main()
{
float x,y,x1,y1,x2,y2,dx,dy,len;
int gdriver = DETECT, gmode,i,a;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
cleardevice();
printf("\n enter x1,y1,x2,y2: ");
scanf("%f%f%f%f", &x1,&y1,&x2,&y2);
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
len=dx;
else
len=dy;
dx=(x2-x1)/len;
dy=(y2-y1)/len;
x=x1+0.5;
y=y1+0.5;
i=1;
a=0;
while(i<=len)
{
putpixel(x,y,35);
if(a==5)
{
x=x+5;
y=y+5;
a=0;
}
a++;
x=x+dx;
y=y+dy;
i=i+1;
delay(10);
}
getch();
closegraph();
}
7] Write a c program to create a house?
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(55);
line(200,200,300,300);
setcolor(89);
circle(200,250,15);
setcolor(45);
line(100,300,200,200);
setcolor(14);
line(100,300,500,300);
setcolor(13);
line(200,200,500,200);
setcolor(25);
line(500,200,500,400);
setcolor(29);
line(500,400,100,400);
setcolor(70);
line(300,300,300,400);
setcolor(121);
line(100,300,100,400);
setcolor(19);
line(225,225,500,225);
setcolor(46);
line(275,275,500,275);
setcolor(100);
line(300,200,400,300);
setcolor(84);
line(400,200,500,300);
setcolor(1);
line(150,400,150,350);
setcolor(2);
line(150,350,250,350);
setcolor(3);
line(250,350,250,400);
getch();
}

You might also like