工作需要判定一个坐标(x,y)是否落在扇形区域内;
使用到数学函数库
#include
atan2(y,x)输出为弧度
返回角度:
double cacul_alpha(double x,double y)
{
return atan2(y,x)*180/PI;
}
判断一个点是否落在扇形区域内
char judge_coor_pos_fan(double x,double y,double alpha1,double alpha2)
{
double theta;
theta=cacul_alpha(x,y);
if(theta>=alpha1 && theta<=alpha2)
{
return true;
}
else
{
return false;
}
}
测试程序:
int main(void)
{
double alpha[100];
double coor_x[100];
double coor_y[100];
int i;
coor_x[0]=1;coor_y[0]=1;
coor_x[1]=1;coor_y[1]=0;
coor_x[2]=0;coor_y[2]=1;
alpha[0]=60;alpha[1]=120;
for(i=0;i
结果
使用查表法
正切表,这里的无穷大设定为1000
TAN[91]={0.000000,0.017455,0.034921,0.052408,0.069927,0.087489,0.105104,0.122785,0.140541,0.158384,
0.176327,0.194380,0.212557,0.230868,0.249328,0.267949,0.286745,0.305731,0.324920,0.344328,0.363970,
0.383864,0.404026,0.424475,0.445229,0.466308,0.487733,0.509525,0.531709,0.554309,0.577350,0.600861,
0.624869,0.649408,0.674509,0.700208,0.726543,0.753554,0.781286,0.809784,0.839100,0.869287,0.900404,
0.932515,0.965689,1.000000,1.035530,1.072369,1.110613,1.150369,1.191754,1.234897,1.279942,1.327045,
1.376382,1.428148,1.482561,1.539865,1.600334,1.664280,1.732051,1.804048,1.880727,1.962611,2.050304,
2.144507,2.246037,2.355853,2.475087,2.605089,2.747478,2.904211,3.077684,3.270853,3.487415,3.732052,
4.010781,4.331476,4.704631,5.144556,5.671284,6.313751,7.115370,8.144348,9.514368,11.430045,14.300659,
19.081174,28.636354,57.290038,1000};
函数实现
double cacul_scope(short x,short y)
{
if(abs(x) < X_MIN)
return SCOPE_MAX;
else
return y/x;
}
char judge_coor_pos_fan_quick(double x,double y,short alpha1,short alpha2)
{
double scope;
scope=cacul_scope(x,y);
if(scope>=TAN[alpha1] && scope<=TAN[alpha2])
{
return true;
}
else
{
return false;
}
}
结果