Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Write A Program To Draw A Line Using DDA Algorithm.: Output

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

PROGRAME-1

Write a Program to draw a line using DDA Algorithm.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\tc\bgi");
int x,y,x1,x2,y1,y2,dx,dy,step,m, xinc,yinc;
x=x1;
y=y1;
printf("Enter Ist end point of line");
scanf("%d%d",&x1,&y1);
printf("Enter 2nd end point of line");
scanf("%d%d",&x2,&y2);
dx=x2-x1;
dy=y2-y1;
m=(y2-y1)\(x2-x1);
if(abs(dx)>abs(dy))
step=abs(dx);
else
step=abs(dy);
xinc=dx/float(steps);
yinc=dy/float(steps);
putpixel(x,y,2);
for(k=0;k<=step;k++)
{
x=x+xinc;
y=y+yinc;
putpixel(x,y,2);
}
getch();
closegraph();
}

Output:
PROGRAME-2
Write a Program to draw a line using Bresenham’s Algorithm
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x,y,x1,x2,y1,y2,dx,dy,p;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"c:\tc\bgi");
printf("Enter Ist end point of line");
scanf("%d%d",&x1,&y1);
printf("Enter 2nd end point of line");
scanf("%d%d",&x2,&y2);
x=x1;
y=y1;
putpixel(x,y,4);
dx=x2-x1;
dy=y2-y1;
p=2*dy-dx;
while(x<x2)
{
if(p<0)
{
x+=1;
p+=2*dy;
}
else
{
x+=1;
y+=1;
p+=2*dy-2*dx;
}
putpixel(x,y,4);
}
getch();
closegraph();
}

Output:
Enter 1st end point of line: 100,200
Enter 2nd end point of line: 200,300
PROGRAM -3
Write a Program for bresanham circle algorithm
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
int r,x,y,p,xc=320,yc=420;
initgraph(&gd,&gm,"C://tc//bgi");
cleardevice();
printf("enter radius");
scanf("%d",&r);
x=0;
y=r;
putpixel(xc+x,yc-y,1);
p=3-(2*r);
for(x=0;x<=y;x++)
{
if(p<0)
{
y=y;
p=(p+4*x)+6;
}
else
{
y=y-1;
p=p+((4*(x-y)+10));
}
putpixel(xc+x,yc-y,1);
putpixel(xc-x,yc-y,2);
putpixel(xc+x,yc+y,3);
putpixel(xc-x,yc+y,4);
putpixel(xc+y,yc-x,5);
putpixel(xc-y,yc-x,6);
putpixel(xc+y,yc+x,7);
putpixel(xc-y,yc+x,8);
}
getch();
closegraph();
}
Output: Ente r radius 100
PROGRAM-4
Write a program for boundary fill algorithm
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void boundaryFill(int x,int y)
{
int interiorColor;
interiorColor=getpixel(x,y);
if((interiorColor !=WHITE)&&(interiorColor !=RED))
{
putpixel(x,y,RED);
boundaryFill(x+1,y);
boundaryFill(x,y+1);
boundaryFill(x-1,y);
boundaryFill(x,y-1);
}
delay(1);
}
void main()
{
void boundaryFill(int,int);
int x,y,n,i;
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"C:\\TC\\BGI");
line(50,50,100,50);
line(100,50,100,100);
line(100,100,50,100);
line(50,100,50,50);
x=78,y=78;
boundaryFill(x,y);
getch();
closegraph();
}
Output:
PROGRAME -5
Write a program for flood fill algorithm
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void floodFill(int x,int y,int ncolor,int ocolor)
{
if(getpixel(x,y)==ocolor)
{
putpixel(x,y,ncolor);
floodFill(x+1,y,ncolor,ocolor);
floodFill(x-1,y,ncolor,ocolor);
floodFill(x,y+1,ncolor,ocolor);
floodFill(x,y-1,ncolor,ocolor);
}
delay(1);
}
void main()
{
int x,y,ncolor=BLUE,ocolor=WHITE;
int midx,midy;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C://tc//bgi");
cleardevice();
printf("enter seed point:");
scanf("%d%d",&x,&y);
midx=getmaxx()/2;
midy=getmaxy()/2;
setbkcolor(RED);
setpalette(ocolor,GREEN);
fillellipse(midx,midy,50,25);
fillellipse(midx+100,midy+100,50,25);
floodFill(x,y,ncolor,ocolor);
getch();
closegraph();
}

Output:
enter seed point:
320
240

PROGRAM -6
Write a program for mid point circle algorithm
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
int r,x,y,p,xc=320,yc=420;
initgraph(&gd,&gm,"C://tc//bgi");
cleardevice();
printf("enter radius");
scanf("%d",&r);
x=0;
y=r;
putpixel(xc+x,yc-y,1);
p=1-r;
for(x=0;x<=y;x++)
{
if(p<0)
{
y=y;
p=(p+2*x)+3;
}
else
{
y=y-1;
p=p+((2*(x-y)+5));
}
putpixel(xc+x,yc-y,1);
putpixel(xc-x,yc-y,2);
putpixel(xc+x,yc+y,3);
putpixel(xc-x,yc+y,4);
putpixel(xc+y,yc-x,5);
putpixel(xc-y,yc-x,6);
putpixel(xc+y,yc+x,7);
putpixel(xc-y,yc+x,8);
}
getch();
closegraph();
}
Output: enter radius 200

PROGRAM-7
Write a program for translation of a triangle
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int x1,y1,x2,y2,x3,y3,tx,ty;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C://tc//bgi");
printf("enter x1 and y1");
scanf("%d%d",&x1,&y1);
printf("enter x2 and y2");
scanf("%d%d",&x2,&y2);
printf("enter x3 and y3");
scanf("%d%d",&x3,&y3);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x1,y1,x3,y3);
printf("enter translation vector along x-axis");
scanf("%d",&tx);
printf("enter translation vector along y-axis");
scanf("%d",&ty);
line(x1+tx,y1+ty,x2+tx,y2+ty);
line(x2+tx,y2+ty,x3+tx,y3+ty);
line(x1+tx,y1+ty,x3+tx,y3+ty);
getch();
closegraph();
}

Output:
enter x1 and y1
100
250
enter x2and y2
200
250
enter x3 and y3
150
150
enter translation vector along x-axis
100
enter translation vector along y-axis
100

PROGRAM -8

Write a program to make a kite


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main(){
int a,b,gd =DETECT,gm,i;
clrscr();
init graph(&gd,&gm,"c:\tc\bgi ");
line(100,100,50,180);
line(100,100,150,180);
line(50,180,100,250);
line(150,180,100,250);
line(100,100,100,250);
line(50,180,150,180);
line(100,250,70,300);
line(100,250,130,300);
line(70,300,130,300);
line(100,300,120,320);
line(120,320,80,340);
line(80,340,120,360);
line(120,360,80,380);
set color(4);
getch();
closegraph();
}
Output:

PROGRAME -9

Write a program to make a fish


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd =DETECT,gm;
clrscr();
init graph(&gd,&gm,"c:\tc\bgi");
set color(6);
ellipse(200,200,0,360,50,30);
line(250,200,280,170);
line(280,170,280,230);
line(280,230,250,200);
circle(160,190,3);
getch();
closegraph();
}

Output:

PROGRAM -10

Write a program to make a hut


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd =DETECT,gm;
clrscr();
init graph(&gd,&gm,"c:\tc\bgi");
set color(6);
rectangle(50,180,150,300);
rectangle(150,180,320,300);
rectangle(80,250,120,300);
line(100,100,50,180,);
line(100,100,300,100);
line(100,100,300,100);
line(300,100,320,180);
getch();
closegraph();
}

Output:

PROGRAME -11

Animination Of fish programe


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,i;
initgraph(&gd,&gm,"c:/tc/bgi");
clrscr();
setcolor(2);
for(i=500;i>=0;i--)
{
clearviewport();
ellipse(200+i,200,0,360,50,30);
line(250+i,200,280+i,170);
line(280+i,170,280+i,230);
line(280+i,230,250+i,200);
circle(160+i,190,3);
}
setcolor(1);
getch();
closegraph();
}

Output:

PROGRAME -12

Write A Program For Translation Of A Circle


#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,r,tx,ty ;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\tc");
printf("enter center coordinates of circle");
scanf("%d%d",&x,&y);
printf("enter radius of circle");
scanf("%d",&r);
circle(x,y,r);
printf("enter translation vector along x-axis");
scanf ("%d",&tx);
printf("enter translation vector along y-axis");
scanf ("%d",&ty);
circle(x+tx,y+ty,r);
getch();
closegraph();
}
Output:

Enter center coordinates of circle


300
300
Enter the radius of circle
30
Enter translation vector along x-axis
100
Enter translation vector along y-axis
100

You might also like