Anda di halaman 1dari 11

Prog1: Based on basic built in function

#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<conio.h>

void main()
{
int gdriver=DETECT;
int gmode,errorcode;
int a=0,b=0,c=0,d=0,maxx,maxy;

clrscr();
initgraph(&gdriver,&gmode,"c:/tc/bgi");
errorcode=graphresult();
if(errorcode!=grOk)
{

printf("Graphic Error");
printf("\n Driver not Found");
getch();
closegraph();
exit(1);
}
else
{
a=(getmaxx()+1)/2;
b=(getmaxy()+1)/2;
c= a+10;
d= b+20;

circle(a,b,30);
line(a,b,c,d);
rectangle(a,b,c,d);
putpixel(a,b,4);
printf("Pixel color at (%d,%d)== %d ",c,d,getpixel(c,d));
maxx = getmaxx();
maxy = getmaxy();

poly[0] = 20; /* 1st vertext */


poly[1] = maxy / 2;

poly[2] = maxx - 20; /* 2nd */


poly[3] = 20;
poly[4] = maxx - 50; /* 3rd */
poly[5] = maxy - 20;

poly[6] = maxx / 2; /* 4th */


poly[7] = maxy / 2;

/* drawpoly doesnt automatically close


the polygon so we close it
*/
poly[8] = poly[0];
poly[9] = poly[1];

/* draw the polygon */


drawpoly(5, poly);
getch();
closegraph();
}
}
Prog2: Bouncing Balls

#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<time.h>
#include<conio.h>

void main()
{
int gdriver=DETECT;
int gmode,errorcode;
int x,y,i;
int a=30;

clrscr();
initgraph(&gdriver,&gmode,"c:/tc/bgi");
errorcode=graphresult();
if(errorcode!=grOk)
{

printf("Graphic Error");
printf("\n Driver not Found");
getch();
closegraph();
exit(1);
}
else
{
x=getmaxx()/10;
y=getmaxy()/10;
for(i=0;i<=20;i++)
{
clrscr();
setcolor(4);
setfillstyle(1,4);
pieslice(x,y,0,360,5);
x=x+15;
y=y+a;
a=a*(-1);
delay(500);
}
x=getmaxx()/10;
x=x+50;
for(i=0;i<=20;i++)
{
clrscr();
setcolor(4);
circle(x,y,5);
circle(x+1,y+1,5);
floodfill(x,y,4);
y=y+a;
a=a*(-1);
delay(500);
}
getch();
}
}
Prog 3: Dashed Line

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
void main()
{
int gdriver=DETECT;
int gmode,errorcode;
int x,y,i;
clrscr();
initgraph(&gdriver,&gmode,"c:/tc/bgi");
errorcode=graphresult();
if(errorcode!=grOk)
{
printf("Graphic error\n\t\tDriver Not Found");
getch();
closegraph();
exit(1);
}
else
{
x=0;
y=200;
i=0;
while(i<=getmaxx())
{
for(x=i;x<=i+5;x++)
{
putpixel(x,y,5);
}
i=i+10;
}
getch();
closegraph();
exit(1);
}
}
Prog4: DDA

#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>

void main()
{
int gdriver=DETECT;
int gmode,errorcode;
int a=0,b=0,c=0,d=0,len=0,x=0,y=0;
int pk=0,pk1=0,dx=0,dy=0,i=1;

clrscr();
initgraph(&gdriver,&gmode,"c:/tc/bgi");
errorcode=graphresult();
if(errorcode!=grOk)
{

printf("Graphic Error");
printf("\n Driver not Found");
getch();
closegraph();
exit(1);
}
else
{
printf("\nEnter x1 ");
scanf("%d",&a);
printf("\nEnter y1 ");
scanf("%d",&b);
printf("\nEnter x2 ");
scanf("%d",&c);
printf("\nEnter y2 ");
scanf("%d",&d);
dx=c-a;
dy=d-b;
if(abs(dx)>=abs(dy))
len=abs(dx);
else
len=abs(dy);
x=dx/len;
y=dy/len;
clrscr();
putpixel(floor(a+.5f),floor(b+.5f),4);
for(i=1;i<=len;i++)
{
a=a+dx;
b=b+dy;
putpixel(floor(a+.5f),floor(b+.5f),4);
}
getch();
}
}
Prog5: Bresenham’s Circle algorithm

#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>

void main()
{
int gdriver=DETECT;
int gmode,errorcode;
int x=0,y=0,r;
int pk=0;

clrscr();
initgraph(&gdriver,&gmode,"c:/tc/bgi");
errorcode=graphresult();
if(errorcode!=grOk)
{

printf("Graphic Error");
printf("\n Driver not Found");
getch();
closegraph();
exit(1);
}
else
{
printf("\nEnter x ");
scanf("%d",&x);
printf("\nEnter radius ");
scanf("%d",&r);
y=r;
clrscr();

pk=3-2*r;
while(x<y)
{
if(pk<0)
{
pk=pk+4*x+6;
putpixel(x+1,y,4);
x=x+1;
}
else
{
pk=pk+4*(x-y)+10;
x=x+1;
y=y-1;
putpixel(x,y,4);
}
}
getch();
}
}
Prog 6: Bresenham’s Line algorithm

(Program for slope less than 1.)


#include<conio.h>
#include<graphics.h>
#include<math.h>

void main (void)


{
//Gaphics mode initialisation to 640x480 resolution
int gd=VGA,gm=VGAHI;
initgraph(&gd,&gm,"c:\\tc\\bgi");
//Variables type decleration
int dx, dy, x, y, xend, p;
int xa, xb, ya, yb;
//Points defined
xa = 100; ya = 100; xb = 500; yb = 105;
//Beginning of Bresenham Algorithm
dx = abs(xa - xb);
dy = abs(ya - yb);
p = 2 * dy - dx;
//determine which point to start end which point to end
if (xa > xb)
{
x = xb;
y = yb;
xend = xa;
}
else { x = xa;
y = ya;
xend = xb;
}
putpixel(x,y,7);
do
{
x ++;
if (p < 0) p += 2 * dy;
else {
y ++;
p += 2 * (dy - dx);
}
putpixel(x, y, 7);
}
while (x < xend);
getch(); //Wait for a key
restorecrtmode(); //Exit to previous mode
}

You also cover both version (x-version and y- version)

Anda mungkin juga menyukai