/**
沒(méi)有方向的相對(duì)坐標(biāo)轉(zhuǎn)換惫恼,x庸汗、y軸的方向與原點(diǎn)相同
@param origin 坐標(biāo)系原點(diǎn)
@param point 需要轉(zhuǎn)換的點(diǎn)
@return 沒(méi)有方向的相對(duì)坐標(biāo)轉(zhuǎn)換嗜逻,x洞拨、y軸的方向與原點(diǎn)相同
*? ? Y
*? ? |
*? ? |? ? ? CY
*? ? |? ? ? |
*? ? |? ? ? |? ? .point
*? ? |? ? ? |
*? ? |? ? ? |
*? ? |? origin----------------- CX
*? ? |
*? ? |
*? ? O--------------------------------------------------- X
*
*/
+ (CGPoint)absolute_to_relative:(CGPoint)origin point:(CGPoint)point {
return CGPointMake(point.x-origin.x, point.y-origin.y);
}
/**
簡(jiǎn)單極坐標(biāo)轉(zhuǎn)換(轉(zhuǎn)換后的x為斜邊养铸,y為角度),
減少數(shù)學(xué)函數(shù)調(diào)用(可以忽略舰攒,因?yàn)橥ǔG闆r下這幾種情況命中概率極低)
@param point 需要轉(zhuǎn)換的點(diǎn)
@return 簡(jiǎn)單極坐標(biāo)轉(zhuǎn)換(轉(zhuǎn)換后的x為斜邊,y為角度)悔醋,
減少數(shù)學(xué)函數(shù)調(diào)用(可以忽略摩窃,因?yàn)橥ǔG闆r下這幾種情況命中概率極低)
*/
+ (CGPoint)to_spolar_coordinate:(CGPoint)point {
CGPoint result;
result.x = 0;
result.y = -1;
if (0 == point.x == point.y) {
result.y = 0;
return result;
}
if (0 == point.y) {
result.x = point.x;
result.y = point.x > 0 ? 0 : 180;
return result;
}
if (0 == point.x) {
result.x = point.y;
result.y = point.y > 0 ? 90 : 270;
return result;
}
if (fabs(point.x) == fabs(point.y)) {
result.x = 1.41421 * fabs(point.x);
if (point.x > 0 && point.y > 0) {
result.y = 45;
} else if (point.x < 0 && point.y > 0) {
result.y = 135;
} else if (point.x < 0 && point.y < 0) {
result.y = 225;
} else if (point.x > 0 && point.y < 0) {
result.y = 315;
}
}
return result;
}
/**
轉(zhuǎn)換為極坐標(biāo)(轉(zhuǎn)換后的x為斜邊,y為角度)
@param point 需要轉(zhuǎn)換的點(diǎn)
@return 轉(zhuǎn)換為極坐標(biāo)(轉(zhuǎn)換后的x為斜邊芬骄,y為角度)
*/
+ (CGPoint)to_polar_coordinate:(CGPoint)point {
CGPoint result;
result.x = sqrt(point.x * point.x + point.y * point.y);
result.y = (180.0 / M_PI) * atan2(point.y , point.x); //弧度轉(zhuǎn)角度
result.y = result.y < .0 ? result.y + 360.0 : result.y;
return result;
}
/**
判斷一個(gè)點(diǎn)是否在扇形內(nèi)(相對(duì)中心點(diǎn))
@param center? 扇形的中心點(diǎn)
@param direction 中心線的方向坐標(biāo)
@param r 半徑
@param angle 角度(0 < angle < 360)
@param point 需要檢查的點(diǎn)
@return 判斷一個(gè)點(diǎn)是否在扇形內(nèi)(相對(duì)中心點(diǎn))
*/
+ (BOOL)in_circular_sector:(CGPoint)center direction:(CGPoint)direction r:(double)r angle:(float)angle point:(CGPoint)point {
//實(shí)際使用中猾愿,我們會(huì)把方向點(diǎn)的極坐標(biāo)放到外部進(jìn)行計(jì)算
CGPoint d_rpoint = [AlgorithmSystem absolute_to_relative:center point:direction]; //方向相對(duì)坐標(biāo)
CGPoint d_pc_point = [AlgorithmSystem to_spolar_coordinate:d_rpoint]; //方向極坐標(biāo)
if (-1 == d_pc_point.y) { //簡(jiǎn)單的如果轉(zhuǎn)換不出,則需要調(diào)用角度函數(shù)計(jì)算
d_pc_point = [AlgorithmSystem to_polar_coordinate:d_rpoint];
}
CGPoint rpoint = [AlgorithmSystem absolute_to_relative:center point:point]; //目標(biāo)相對(duì)坐標(biāo)
CGPoint pc_point = [AlgorithmSystem to_polar_coordinate:rpoint]; //目標(biāo)極坐標(biāo)
if (pc_point.x > r) return false;
bool result = false;
float half_angle = angle / 2;
float angle_counter = d_pc_point.y - half_angle; //中心線順時(shí)針?lè)较虻姆秶?/p>
float angle_clockwise = d_pc_point.y + half_angle; //中心線逆時(shí)針?lè)较虻姆秶?/p>
if (0 == d_pc_point.y || angle_counter < 0 || angle_clockwise > 360) {
angle_counter = angle_counter < 0 ? angle_counter + 360 : angle_counter;
angle_clockwise = angle_clockwise > 360 ? angle_counter - 360 : angle_counter;
if (pc_point.y >= 0 && pc_point.y <= angle_counter) {
result = true;
} else if (pc_point.y >= angle_clockwise && pc_point.y <= 360) {
result = true;
}
} else {
result = angle_counter <= pc_point.y && angle_clockwise >= pc_point.y;
}
return result;
}
將這三個(gè)方法添加到.m文件中調(diào)用+ (BOOL)in_circular_sector:(CGPoint)center direction:(CGPoint)direction r:(double)r angle:(float)angle point:(CGPoint)point 即可判斷