DDA Technique To Draw A Straightline.

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

DDA Technique to draw a Straightline..

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,steps,i;
float x1,x2,y1,y2,dx,dy,xi,yi,x,y;
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("Enter the starting point:");
scanf("%f%f",&x1,&y1);
printf("Enter the ending point:");
scanf("%f%f",&x2,&y2);
setcolor(GREEN);
settextstyle(3,0,3);
outtextxy(75,450,"DDA TEchnique to draw a Straight line");
dx = x2-x1;
dy = y2-y1;
if(abs(dx)> abs(dy))
{
steps=abs(dx);
}
else
{
steps=abs(dy);
}
xi=dx/steps;
yi=dy/steps;
x=x1;
y=y1;
putpixel(x,y,GREEN);
for(i=1;i<=steps;i++)
{
x=x+xi;
y=y+yi;
putpixel(x,y,GREEN);
}
getch();
}

DDA Technique to draw Circle..


#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
#include<stdlib.h>
#include<graphics.h>
#define PI 3.1415
void main()
{
int gd=DETECT,gm,xc,yc,i,r;
float x,y,theta;
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("Enter radius of the circle: ");
scanf("%d",&r);
setcolor(GREEN);
settextstyle(1,0,1);
outtextxy(200,400,"DDA Technique to draw a circle");
xc = getmaxx()/2.0;
yc = getmaxy()/2.0;
for(theta=0;theta<PI*2;theta=theta+0.01)
{
x=xc+r*cos(theta);
y=yc+r*sin(theta);
putpixel((int)x,(int)y,GREEN);
delay(2);
}
getch();
}

Bersenham's technique for Straight line...


#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#include<dos.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,xend,p;
float x1,y1,x2,y2,dx,dy,x,y;
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("\n Enter the starting point: ");
scanf("%f%f",&x1,&y1);
printf("\n Enter the ending point : ");
scanf("%f%f",&x2,&y2);
setcolor(GREEN);
settextstyle(3,0,3);
outtextxy(50,450,"Bersenham's Technique to draw a straight line");
dx = abs(x2-x1);
dy = abs(y2-y1);
if(x1>x2)
{
x=x2; y=y2;
xend=x1;
}
else
{
x=x1; y=y1;
xend=x2;
}
p=2*dy-dx;
putpixel((int)x,(int)y,WHITE);
while(x<xend)
{
delay(10);
x=x+1;
if(p>0)
{
y++;
p=p+2*(dy-dx);
}
else
{
p=p+2*dy;
}
putpixel((int)x,(int)y,WHITE);
}
getch();
}

Bersenham's Technique for Circle...


#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
#include<stdlib.h>
#include<graphics.h>
plotpoints(int,int,int,int);
void main()
{
int gd=DETECT,gm;
int x,y,xc,yc,p,r;
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("enter the center coordinates of the circle(300,300) : ");
scanf("%d%d",&xc,&yc);
printf("Enter the radius of the circle(100) ");
scanf("%d",&r);
setcolor(GREEN);
settextstyle(1,0,1);
outtextxy(50,450,"Bresenham's Technique to draw a circle");
x=0; y=r;
plotpoints(xc,yc,x,y);
p=1-x;
while(x<y)
{
if(p<0)
p=p+2*x+1;
else
{
y--;
p=p+2*(x-y)+1;
}
x++;
plotpoints(xc,yc,x,y);
delay(100);
}
getch();
}
plotpoints(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,YELLOW);
putpixel(xc-x,yc+y,YELLOW);
putpixel(xc+x,yc-y,YELLOW);
putpixel(xc-x,yc-y,YELLOW);
putpixel(xc+y,yc+x,YELLOW);
putpixel(xc-y,yc+x,YELLOW);
putpixel(xc+y,yc-x,YELLOW);
putpixel(xc-y,yc-x,YELLOW);
return;
}

National Flag
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
flag1();
flag2();
flag3();
void main()
{
int gd=DETECT,gm;
int i,stangle,endangle;
initgraph(&gd,&gm,"c:\\tc\\bgi");
while(!kbhit())
{
cleardevice();
flag1();
setcolor(2);
settextstyle(GOTHIC_FONT,HORIZ_DIR,2);
outtextxy(150,300,"NATIONAL FLAG");
delay(250);
cleardevice();
flag3();
setcolor(15);
settextstyle(TRIPLEX_FONT,HORIZ_DIR,4);
outtextxy(150,300,"National Flag");
delay(250);
cleardevice();
flag2();
setcolor(2);
settextstyle(TRIPLEX_FONT,HORIZ_DIR,7);
outtextxy(150,300,"NATIONAL FLAG");
delay(250);
}
getch();
closegraph();
return;
}

flag1()
{
int i;
setfillstyle(SOLID_FILL,6);
bar(50,50,300,100);
setfillstyle(SOLID_FILL,WHITE);
bar(50,100,300,150);
setfillstyle(SOLID_FILL,GREEN);
bar(50,150,300,200);
setfillstyle(SOLID_FILL,8);
bar(40,20,50,460);
setcolor(BLUE);
setfillstyle(SOLID_FILL,WHITE);
for(i=0;i<=360;i=i+15)
pieslice(175,125,i,i+15,25);
return;
}
flag2()
{
int i;

flag3()
{
int i;

int
r1[8]={50,50,300,75,300,125,50,100};
int
r2[8]={50,100,300,125,300,175,50,150}
;
int
r3[8]={50,150,300,175,300,225,50,200}
;

int
r1[8]={50,50,300,25,300,75,50,100};
int
r2[8]={50,100,300,75,300,125,50,150};
int
r3[8]={50,150,300,125,300,175,50,200}
;

setcolor(0);
setfillstyle(SOLID_FILL,6);
fillpoly(4,r1);

setcolor(0);
setfillstyle(SOLID_FILL,6);
fillpoly(4,r1);
setfillstyle(SOLID_FILL,WHITE);
fillpoly(4,r2);
setfillstyle(SOLID_FILL,GREEN);
fillpoly(4,r3);
setfillstyle(SOLID_FILL,8);
bar(40,20,50,460);
setcolor(BLUE);
setfillstyle(SOLID_FILL,WHITE);
for(i=0;i<=360;i=i+5)
pieslice(175,137,i,i+15,25);
return;
}

setfillstyle(SOLID_FILL,WHITE);
fillpoly(4,r2);
setfillstyle(SOLID_FILL,GREEN);
fillpoly(4,r3);
setfillstyle(SOLID_FILL,8);
bar(40,20,50,460);
setcolor(BLUE);
setfillstyle(SOLID_FILL,WHITE);
for(i=0;i<=360;i=i+15)
pieslice(175,113,i,i+15,25);
return;
}

Shadow of a pole as sun moves


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int i=200,gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
bar(315,150,325,250);
setfillstyle(1,8);
while(!kbhit())
{
for(i=100;i>0;i=i-1)
{
setfillstyle(1,8);
bar(315-i,250,315,252);
circle(300+i*3,50+i,20);
delay(50);
setcolor(0);
circle(300+i*3,50+i,20);
setcolor(15);
setfillstyle(1,0);
bar(315-i,250,315,252);
}
for(i=0;i<100;i+1)
{
setfillstyle(1,8);
bar(325,250,325+i,252);
circle(300-i*3,50+i,20);
delay(50);
setcolor(0);
circle(300-i*3,50+i,20);
setcolor(15);
setfillstyle(1,0);
bar(325,250,325+i,252);
}
sleep(1);
}
getch();
closegraph();
}

A Man Walking With an umbrella


#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
#include<stdlib.h>
void main()
{
int driver=DETECT,mode,i,j;
int x1,x2,x3,x4,x5;
int y1,y2,y3,y4,y5;
initgraph(&driver,&mode,"c:\\tc\\bgi");
cleardevice();
x1=100; y1=330;
x2=150; y2=330;
x3=125; y3=280;
x4=0;
y4=330;
x5=600; y5=332;
for(i=0;i<500;i+=10)
for(x1=100,x2=150;x1<150,x2>100;x1+=2,x2-=2)
{
delay(15);
cleardevice();
line(x3+i,250,150+i,240);
line(x3+i,240,x1+i,270);
line(150+i,249,150+i,180);
ellipse(150+i,185,0,2350,50,5);
line(x1+i,y1,x3+i,y3);
line(x2+i,y2,x3+i,y3);
ellipse(125+i,255,0,360,5,25);
arc(150+i,225,40,140,65);
circle(125+i,220,10);
line(x4,y4,x5,y5);
}
}

Horizontal HISTOGRAM

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<math.h>
horizontalbar(int qty[]);
bargraph(int p[]);
void main()
{
int i,qty[4],j=1990;
clrscr();
printf("\n Histogram of sales for 4 years");
for(i=0;i<4;i++,j++)
{
printf("\n Enter the qty of cars produced in the year %d : ",j);
scanf("%d",&qty[i]);
}
horizontalbar(qty);
}
horizontalbar(int data[])
{
int driver=DETECT,mode,tot;
int i,left,right;
static char*label[5]={"1990","1991","1992","1993"};
initgraph(&driver,&mode,"c:\\tc\\bgi");
clrscr();
cleardevice();
setfillstyle(SOLID_FILL,MAGENTA);
settextstyle(4,0,4);
outtextxy(50,50,"HORIZONTAL HISTOGRAM");
line(75,400,300,400);
settextstyle(0,0,0);
outtextxy(150,410,"SALES--->");
line(75,220,75,400);
settextstyle(0,1,1);
outtextxy(35,280,"YEAR--->");
settextstyle(0,0,0);
left=75;
for(i=0;i<4;i++)
tot += data[i];
for(i=0;i<4;i++)
{
if(tot != 0)
right=left+data[i]/2;
else
left=right;
bar3d(left,365-i*40,right,380-i*40,8,1);
outtextxy(left-35,370-i*40,label[i]);
}
getch();
closegraph();
return(0);
}

Vertical HISTOGRAM

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<math.h>
verticalbar(int qty[]);
bargraph(int p[]);
void main()
{
int i,qty[4],j=1990;
clrscr();
printf("\n Histogram of sales for 4 years");
for(i=0;i<4;i++,j++)
{
printf("\n Enter the qty of cars produced in the year %d : ",j);
scanf("%d",&qty[i]);
}
verticalbar(qty);
}
verticalbar(int data[])
{
int driver=DETECT,mode,tot;
int i,left,top,wide,bottom,deep;
static char*label[5]={"1990","1991","1992","1993"};
initgraph(&driver,&mode,"c:\\tc\\bgi");
clrscr();
cleardevice();
setfillstyle(SOLID_FILL,MAGENTA);
setfillstyle(1,5);
settextstyle(4,0,4);
outtextxy(50,50,"VERTICAL HISTOGRAM");
line(60,400,300,400);
settextstyle(0,0,0);
outtextxy(150,430,"YEAR--->");
line(60,220,60,400);
settextstyle(0,1,0);
outtextxy(55,250,"SALES--->");
settextstyle(0,0,0);
bottom=getmaxy()-80;
for(i=0;i<4;i++)
tot += data[i];
for(i=0;i<4;i++)
{
if(tot != 0)
top=((1-(float)data[i]/tot)*bottom);
else
top=bottom;
bar3d(i*60+75,top,i*60+90,bottom,4,1);
outtextxy(i*60+65,bottom+10,label[i]);
}
getch();
closegraph();
return(0);
}

Program to stimulate a Progress bar


#include<graphics.h>
#include<conio.h>
#include<alloc.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm;
int x=232,y=336,x1=236,y1=344;
int i,m;
unsigned int size;
void *buffer;
initgraph(&gd,&gm,"c:\\tc\\bgi");
setbkcolor(BLACK);
line(230,330,370,330);
line(230,350,370,350);
line(226,335,226,345);
line(226,335,230,330);
line(226,345,230,350);
line(374,335,374,345);
line(374,335,370,330);
line(374,345,370,350);
outtextxy(275,365,"LOADING...");
for(i=1;i<5;i++)
{
setfillstyle(1,GREEN);
bar(x,y,x1,y1);
x=x1+2;
x1=x1+6;
}
size = imagesize(232,336,256,344);
buffer=malloc(size);
getimage(232,336,256,344,buffer);
x=232;
m=0;
while(!kbhit())
{
putimage(x,336,buffer,XOR_PUT);
x=x+2;
if(x>=350)
{
m++;
x=232;
if(m==5)
return;
}
putimage(x,336,buffer,XOR_PUT);
delay(30);
}
getch();
}

Pie chart depiction of the results of an election between


four parties.
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
int i,total=0,xc,yc,r,data[4];
float x,y,sangle=0,endangle;
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(GREEN);
xc=getmaxx()/2.0;
yc=getmaxy()/2.0;
for(i=0;i<4;i++)
{
printf("\n Enter Number of votes for party %d : ",i+1);
scanf("%d",&data[i]);
}
printf("\n Enter the radius of the pie chart : ");
scanf("%d",&r);
setcolor(GREEN);
settextstyle(1,0,1);
outtextxy(100,400,"Pie chart Depiction of the result ");
outtextxy(100,420,"of an election b/w four parties ");
setcolor(YELLOW);
settextstyle(0,0,0);
for(i=0;i<4;i++)
total=total+data[i];
for(i=0;i<4;i++)
{
endangle=360.00*data[i]/total+sangle;
setfillstyle(i+3,CYAN);
pieslice(xc,yc,sangle,endangle,r);
sangle=endangle;
getch();
}
}

Program to implement the Cohen-Sutherland line


clipping algorithm
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int outcode(int x,int y);
void clipping(int x1,int y1,int x2,int y2);
int xleft,xright,ytop,ybottom;
float m;
int gd=DETECT,gm;
void main()
{
int x1,x2,x3,x4,y1,y2,y3,y4;
clrscr();
printf("Enter the starting point & Ending point of Line -> \n");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
xleft=150;
xright=300;
ytop=150;
ybottom=300;
initgraph(&gd,&gm,"c:\\tc\\bgi");
rectangle(xleft,ytop,xright,ybottom);
line(x1,y1,x2,y2);
getch();
cleardevice();
clipping(x1,y1,x2,y2);
}
void clipping(int x1,int y1,int x2,int y2)
{
int x,y,code1,code2,check;
setcolor(GREEN);
code1=outcode(x1,y1);
code2=outcode(x2,y2);
while((code1 != 0) || (code2 != 0))
{
check = code1 & code2;
if(check != 0)
return;
check = code1;
if(code1 == 0)
check = code2;
m = (float)(y2-y1)/(x2-x1);
if((check & 1) != 0)
{
y = y1+m*(xleft-x1);
x = xleft;
}
if((check & 2) != 0)
{
y=y1+m*(xright-x1);
x=xright;
}

if((check & 4) != 0)
{
x=x1+(ybottom-y1)/m;
y=ybottom;
}
if((check & 8) != 0)
{
x=x1+(ytop-y1)/m;
y=ytop;
}
if(check == code1)
{
x1=x;
y1=y;
code1=outcode(x,y);
}
else if(check == code2)
{
x2=x;
y2=y;
code2=outcode(x,y);
}
}
printf("\n Clipped Line is ");
rectangle(xleft,ytop,xright,ybottom);
line(x1,y1,x2,y2);
getch();
}
int outcode(int x,int y)
{
int temp = 0;
if(x < xleft)
temp=temp|1;
else if(x > xright)
temp=temp|2;
else if(y > ybottom)
temp=temp|4;
else if(y < ytop)
temp=temp|8;
return temp;
}

Progress bar(short and simple)


#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(13);
settextstyle(TRIPLEX_FONT,HORIZ_DIR,0);
setusercharsize(2,1,1,1);
outtextxy(138,198,"LOADING");
setcolor(13);
settextstyle(TRIPLEX_FONT,HORIZ_DIR,0);
setusercharsize(2,1,1,1);
setcolor(BLUE);
rectangle(119,199,421,231);
rectangle(118,198,422,232);
setcolor(10);
for(int i=120;i<=420;i++)
{
line(i,200,i,230);
delay(30);
}
settextstyle(TRIPLEX_FONT,HORIZ_DIR,0);
setusercharsize(2,1,1,1);
outtextxy(150,300,"LOADED!");
getch();
closegraph();
}

You might also like