#include<stdio.h>
#include<graphics.h>
#include<dos.h>
#include<conio.h>
#include<math.h>
#define pie 3.14
void init_graph();
void bresenham_circle(const int,const int,const int,int);
void main( )
{
int ho = 95;
int ko = 55;
int ro = 25;
int hi = 445;
int ki = 145;
int ri = 25;
int hdo = 2;
int hdi = -2;
int kdo = 0;
int kdi = 0;
init_graph();
do
{
rectangle(40,10,600,470);
rectangle(150,100,490,380);
bresenham_circle(hi,ki,ri,BLACK);
bresenham_circle(ho,ko,ro,BLACK);
ho += hdo;
ko += kdo;
hi += hdi;
ki += kdi;
if(ho == 95 && ko == 55)
{ hdo = 2; kdo = 0; }
if(hi == 445 && ki == 145)
{ hdi = -2; kdi = 0; }
if(ho == 545 && ko == 55)
{ hdo = 0; kdo = 2; }
if(hi == 195 && ki == 145)
{ hdi = 0; kdi = 2; }
if(ho == 95 && ko == 425)
{ hdo = 0; kdo = -2; }
if(hi == 445 && ki == 335)
{ hdi = 0; kdi = -2; }
if(ho == 545 && ko == 425)
{ hdo = -2; kdo = 0; }
if(hi == 195 && ki == 335)
{ hdi = 2; kdi = 0; }
bresenham_circle(hi,ki,ri,GREEN);
bresenham_circle(ho,ko,ro,RED);
delay(2);
}while(!kbhit());
getch();
}
void init_graph()
{
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
}
}
void bresenham_circle(const int h,const int k,const int r,int col)
{
int x=0;
int y=r;
int p=(3-(2*r));
do
{
putpixel(h+x,k+y,col);
putpixel(h+y,k+x,col);
putpixel(h+y,k-x,col);
putpixel(h+x,k-y,col);
putpixel(h-x,k-y,col);
putpixel(h-y,k-x,col);
putpixel(h-y,k+x,col);
putpixel(h-x,k+y,col);
x++;
if(p<0)
p+=((4*x)+6);
else
{
y--;
p+=((4*(x-y))+10);
}
}while(x<=y);
}
No comments:
Post a Comment