#include"graphics.h"
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#define PI 3.1415926
void myinitgraph()
{
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
exit(1);
}
}
void Bresenham_Line(float x0,float y0,float x1,float y1,float color)
{
float x,y,dx,dy,i;
float k,e,m;
x0=x0+250;y0=y0+250;x1=x1+250;y1=y1+250;
x=x0;y=y0;e=0;
dx=x1-x0;dy=y1-y0;
k=dy/dx;m=fabs(k);
if(dx==0)
{
for(i=0;i<=fabs(dx);i++)
{
putpixel(x,y,color);
y++;
}
}
else
{
k=dy/dx;m=fabs(k);
if(k>=-1.0&&k<=1.0)
{
for(i=0;i<=fabs(dx);i++)
{
putpixel(x,y,color);
x=x+1;e=e+m;
if(k<0&&k>=-1.0)
{
if(e>=0.5)
{
y=y-1;e=e-1;
}
}
else if(k>=0&&k<=1.0)
{
if(e>=0.5)
{
y=y+1;e=e-1;
}
}
}
}
else if(k>1.0)
{
for(i=0;i<=fabs(dy);i++)
{
putpixel(x,y,color);
y++;e=e+1/m;
if(e>=0.5)
{
x=x+1;
e=e-1;
}
}
}
else
{
for(i=0;i<=fabs(dy);i++)
{
putpixel(x,y,color);
y--;e=e+1/m;
if(e>=0.5)
{
x=x+1;
e=e-1;
}
}
}
}
}
void MidpointCircle(r,color)
int r,color;
{
int x1,y1,x2,y2,x,y,deltax,deltay,d;
x=0;y=r;
deltax=3;deltay=2-r-r;d=1-r;
putpixel(x+250,y+250,color);
putpixel(x+250,-y+250,color);
putpixel(y+250,x+250,color);
putpixel(y+250,-x+250,color);
putpixel(-x+250,y+250,color);
putpixel(-x+250,-y+250,color);
putpixel(-y+250,x+250,color);
putpixel(-y+250,-x+250,color);
while(x<y)
{
if(d<0)
{
d+=deltax;
deltax+=2;
x++;
}
else
{
d+=(deltax+deltay);
deltax+=2;deltay+=2;
x++;y--;
}
x1=x+250;y1=y+250;x2=-x+250;y2=-y+250;
putpixel(x1,y1,color);
putpixel(y1,x1,color);
putpixel(x1,y2,color);
putpixel(y1,x2,color);
putpixel(y2,x1,color);
putpixel(y2,x2,color);
putpixel(x2,y1,color);
putpixel(x2,y2,color);
}
}
void fill(int x,int y,int fillcolor,int oldcolor)
{
if(getpixel(x,y)!=fillcolor&&getpixel(x,y)!=oldcolor)
{
putpixel(x,y,fillcolor);
fill(x+1,y,fillcolor,oldcolor);
fill(x,y+1,fillcolor,oldcolor);
fill(x-1,y,fillcolor,oldcolor);
fill(x,y-1,fillcolor,oldcolor);
}
}
void AXB(float A[][100],float B[][100],float C[][100],int m,int n,int k)
{ int t,i,j;
for(i=0;i<m;i++)
for(j=0;j<k;j++)
{
C[i][j]=0;
for(t=0;t<n;t++)
C[i][j]+=A[i][t]*B[t][j];
}
}
main()
{
int i,j;
float aa[10][2],bb[2][2],C[10][2];
float a,b,m,n,x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8,x9,y9,x10,y10,c,d;
int r;
scanf("%d",&r);
c=sin(60*PI/180);d=cos(60*PI/180);
m=sin(18*PI/180);n=cos(18*PI/180);
a=sin(54*PI/180);b=cos(54*PI/180);
x1=0;y1=r;x2=-r*n;y2=r*m;x3=-r*b;y3=-r*a;x4=r*b;y4=-r*a;x5=r*n;y5=r*m;x6=-0.25*r*n;y6=r*m;x7=0.25*r*n;y7=r*m;
x8=(-r+r*m-r*n*(r*m+r*a)/(r*n+r*b))/((r+r*a)/(r*b)+(r*m+r*a)/(r*n+r*b));y8=((r+r*a)/(r*b))*x8+r;
x9=-(-r+r*m-r*n*(r*m+r*a)/(r*n+r*b))/((r+r*a)/(r*b)+(r*m+r*a)/(r*n+r*b));y9=((r+r*a)/(r*b))*x8+r;
x10=0;y10=(r*m+r*a)/(r*n+r*b)*r*b-r*a;
aa[0][0]=x1;aa[0][1]=y1;aa[1][0]=x2;aa[1][1]=y2;aa[2][0]=x3;aa[2][1]=y3;aa[3][0]=x4;aa[3][1]=y4;aa[4][0]=x5;aa[4][1]=y5;
aa[5][0]=0.25*x2;aa[5][1]=y2;aa[6][0]=0.25*x5;aa[6][1]=y5;aa[7][0]=x8;aa[7][1]=y8;aa[8][0]=x9;aa[8][1]=y9;aa[9][0]=x10;aa[9][1]=y10;
bb[0][0]=d;bb[0][1]=-c;bb[1][0]=c; bb[1][1]=d;
AXB(aa,bb,C,10,2,2);
for(i=0;i<10;i++)
{
for(j=0;j<2;j++)
aa[i][j]=C[i][j];
}
myinitgraph();
MidpointCircle(r,4);
Bresenham_Line(aa[1][0],aa[1][1],aa[5][0],aa[5][1],2);
Bresenham_Line(aa[6][0],aa[6][1],aa[4][0],aa[4][1],2);
Bresenham_Line(aa[0][0],aa[0][1],aa[6][0],aa[6][1],2);
Bresenham_Line(aa[8][0],aa[8][1],aa[3][0],aa[3][1],2);
Bresenham_Line(aa[1][0],aa[1][1],aa[7][0],aa[7][1],2);
Bresenham_Line(aa[9][0],aa[9][1],aa[3][0],aa[3][1],2);
Bresenham_Line(aa[8][0],aa[8][1],aa[4][0],aa[4][1],2);
Bresenham_Line(aa[2][0],aa[2][1],aa[9][0],aa[9][1],2);
Bresenham_Line(aa[5][0],aa[5][1],aa[0][0],aa[0][1],2);
Bresenham_Line(aa[2][0],aa[2][1],aa[7][0],aa[7][1],2);
fill(250,250,4,2);
}
运行时出现Floating point error: Overflow.