程序:罗健军
程序中var16为有符号16位数字型,uvar16为无符号16位数字型。var32为有符号32位数字型。PutPixel为画点函数。
函数Sector入口参数x0,y0为圆心坐标,r为半径。stangle为起始角度,endangle为结束角度。
double SIN45=0.707106781186548;
uvar32 SINV[91]={0,
17452406, 34899496, 52335956, 69756473, 87155742, 104528463,
121869343, 139173100, 156434465, 173648177, 190808995, 207911690,
224951054, 241921895, 258819045, 275637355, 292371704, 309016994,
325568154, 342020143, 358367949, 374606593, 390731128, 406736643,
422618261, 438371146, 453990499, 469471562, 484809620, 500000000,
515038074, 529919264, 544639035, 559192903, 573576436, 587785252,
601815023, 615661475, 629320391, 642787609, 656059028, 669130606,
681998360, 694658370, 707106781, 719339800, 731353701, 743144825,
754709580, 766044443, 777145961, 788010753, 798635510, 809016994,
819152044, 829037572, 838670567, 848048096, 857167300, 866025403,
874619707, 882947592, 891006524, 898794046, 906307787, 913545457,
920504853, 927183854, 933580426, 939692620, 945518575, 951056516,
956304755, 961261695, 965925826, 970295726, 974370064, 978147600,
981627183, 984807753, 987688340, 990268068, 992546151, 994521895,
996194698, 997564050, 998629534, 999390827, 999847695, 1000000000
};
/////////////////////////////////////////////////////////////////////////////////
// 查找表式sin函数
/////////////////////////////////////////////////////////////////////////////////
double Lsin(var16 angle)
{
double v,f;
if(angle>360||angle<-360)angle=angle-(angle/360)*360;
if(angle<0)angle=360+angle;
if(angle>180)f=-1;else f=1;
if(angle>90&&angle<=180)angle=180-angle;
else if(angle>180&&angle<=270)angle=angle-180;
else if(angle>270)angle=360-angle;
v=f*SINV[angle]/10.0e8;
return(v);
}
/////////////////////////////////////////////////////////////////////////////////
// 查找表式cos函数
/////////////////////////////////////////////////////////////////////////////////
double Lcos(var16 angle)
{
double v,f;
if(angle>360||angle<-360)angle=angle-(angle/360)*360;
if(angle<0)angle=360+angle;
if(angle<270&&angle>90)f=-1;else f=1;
if(angle>90&&angle<=180)angle=180-angle;
else if(angle>180&&angle<=270)angle=angle-180;
else if(angle>270)angle=360-angle;
angle=90-angle;
v=f*SINV[angle]/10.0e8;
return(v);
}
/////////////////////////////////////////////////////////////////////////////////
// 扇形绘制函数
////////////////////////////////////////////////////////////////////////////////
void Sector(var16 x0,var16 y0,uvar16 r,uvar16 stangle,uvar16 endangle)
{
var16 i,j;
var16 *xy;
var16 bx,ex,bxd,exd,bxf,exf,ben;
var32 tn,x,y;
var32 xmax;
y=r; x=0;
xmax=(var32)(r*SIN45);
tn=(1-r*2);
xy=(var16 *)calloc(20,sizeof(var16));
xy[ 0]=x0+r;xy[1]=y0;
xy[ 2]=x0; xy[3]=y0-r;
xy[ 4]=x0; xy[5]=y0-r;
xy[ 6]=x0-r;xy[7]=y0;
xy[ 8]=x0-r;xy[9]=y0;
xy[10]=x0; xy[11]=y0+r;
xy[12]=x0; xy[13]=y0+r;
xy[14]=x0+r;xy[15]=y0;
bx=stangle/45;
ex=endangle/45;
ben=ex-bx-1;
xy[16]=(var16)(r*Lcos(stangle));
xy[17]=(var16)(r*Lsin(stangle));
xy[18]=(var16)(r*Lcos(endangle));
xy[19]=(var16)(r*Lsin(endangle));
Line(x0+xy[16],y0-xy[17],x0,y0);
Line(x0+xy[18],y0-xy[19],x0,y0);
if(bx==1||bx==2||bx==5||bx==6)bxd=abs(xy[16]);else bxd=abs(xy[17]);
if(ex==1||ex==2||ex==5||ex==6)exd=abs(xy[18]);else exd=abs(xy[19]);
if(bx==0||bx==2||bx==4||bx==6)bxf=0; else bxf=1;
if(ex==0||ex==2||ex==4||ex==6)exf=1; else exf=0;
while(x<=xmax)
{
if(tn>=0)
{
tn+=(6+((x-y)*4));
y--;
xy[0]--;
xy[3]++;
xy[5]++;
xy[6]++;
xy[8]++;
xy[11]--;
xy[13]--;
xy[14]--;
}
else tn+=((x*4)+2);
if(stangle<endangle)
{
j=(bx+1)*2;
for(i=0;i<ben;i++)
{
PutPixel(xy[j],xy[j+1]);
j+=2;
}
}
else if(stangle>endangle)
{
j=(bx+1)*2;
for(i=bx+1;i<8;i++)
{
PutPixel(xy[j],xy[j+1]);
j+=2;
}
j=0;
for(i=0;i<ex;i++)
{
PutPixel(xy[j],xy[j+1]);
j+=2;
}
}
i=bx*2;
if( (x>bxd)^bxf )PutPixel(xy[i],xy[i+1]);i=ex*2;
if( (x>exd)^exf )PutPixel(xy[i],xy[i+1]);x++;
xy[ 1]--;
xy[ 2]++;
xy[ 4]--;
xy[ 7]--;
xy[ 9]++;
xy[10]--;
xy[12]++;
xy[15]++;
}
free(xy);
}