DDA Technique To Draw A Straightline.
DDA Technique To Draw A Straightline.
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();
}
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;
}
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);
}
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;
}