#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 h=0;
int k=0;
int r=150;
int rs=70;
int hs=r-rs;
int ks=0;
int rk=30;
int hk=rs-rk;
int kk=0;
int rl=10;
int hl=rk-rl;
int kl=0;
int angs=0;
int angk=0;
int angl=0;
char ch;
init_graph();
do
{
bresenham_circle(h,k,r,GREEN);
bresenham_circle(hs,ks,rs,BLACK);
bresenham_circle(hk,kk,rk,BLACK);
bresenham_circle(hl,kl,rl,BLACK);
hs = h + ((r-rs)*cos(pie*angs/180));
ks = k + ((r-rs)*sin(pie*angs/180));
hk = hs + ((rs-rk)*cos(pie*angk/180));
kk = ks + ((rs-rk)*sin(pie*angk/180));
hl = hk + ((rk-rl)*cos(pie*angl/180));
kl = kk + ((rk-rl)*sin(pie*angl/180));
bresenham_circle(hs,ks,rs,RED);
bresenham_circle(hk,kk,rk,BLUE);
bresenham_circle(hl,kl,rl,YELLOW);
angs = (angs+1)%360;
angk = (angk+2)%360;
angl = (angl+4)%360;
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((getmaxx()/2)+(h+x),(getmaxy()/2)-(k+y),col);
putpixel((getmaxx()/2)+(h+y),(getmaxy()/2)-(k+x),col);
putpixel((getmaxx()/2)+(h+y),(getmaxy()/2)-(k-x),col);
putpixel((getmaxx()/2)+(h+x),(getmaxy()/2)-(k-y),col);
putpixel((getmaxx()/2)+(h-x),(getmaxy()/2)-(k-y),col);
putpixel((getmaxx()/2)+(h-y),(getmaxy()/2)-(k-x),col);
putpixel((getmaxx()/2)+(h-y),(getmaxy()/2)-(k+x),col);
putpixel((getmaxx()/2)+(h-x),(getmaxy()/2)-(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