主要计算公式:
直线的一般方程 y = kx + b;
圆的一般方程 x^2 + y^2 + Dx + Ey + F = 0;
圆的基本系数关系 r = (根号(D^2 + E^2 - 4F))/2
一元二次方程求根公式 x = -b (+/-) (根号(b^2 - 4ac)) / 2a
/**
计算圆环上取色点,解任意点过圆心的直线与圆的交点取其一
@param pointOne 任意点
@param circleCenterPoint 圆心
*/
- (CGPoint)calculateColorPointWithPoint:(CGPoint)pointOne circleCenterPoint:(CGPoint)circleCenterPoint
{
//由于OC坐标系与数学坐标系的差异,y值取反,转成数学坐标系
CGPointtemp1 = pointOne;
CGPointtemp2 = circleCenterPoint;
pointOne =CGPointMake(temp1.x, -temp1.y);
circleCenterPoint =CGPointMake(temp2.x, -temp2.y);
//计算过圆心直线斜率和常数b
lineK= (CGFloat)(pointOne.y- circleCenterPoint.y) / (CGFloat)(pointOne.x- circleCenterPoint.x);
lineB= circleCenterPoint.y-lineK*circleCenterPoint.x;
//计算圆的方程常数 其圆心坐标(-D/2, -E/2) 半径公式 r = (根号(D^2 + E^2 - 4F))/2
CGFloatcircleR = circleCenterPoint.x-4;//圆的半径,取圆环的中间值
circleD= -2* circleCenterPoint.x;
circleE= -2* circleCenterPoint.y;
circleF= (powf(circleD,2) +powf(circleE,2) -4*powf(circleR,2))/4.0;
//一元二次方程求根公式 x = -b (+/-) (根号(b^2 - 4ac)) / 2a
CGFloata = (1+powf(lineK,2));
CGFloat b = (2*lineK*lineB + circleD + circleE*lineK);
CGFloat c = powf(lineB, 2) + circleE*lineB + circleF;
//直线与圆的两个交点
CGFloatx1 = ((-b) +sqrtf(powf(b,2) -4*a*c)) / (2*a);
CGFloaty1 =lineK*x1 +lineB;
CGFloatx2 = ((-b) -sqrtf(powf(b,2) -4*a*c)) / (2*a);
CGFloaty2 =lineK*x2 +lineB;
//当过圆心直线斜率不存在时
if(pointOne.x== circleCenterPoint.x) {
x1 = circleCenterPoint.x;
y1 = -4;
x2 = circleCenterPoint.x;
y2 = -(2*circleR +4);
}
//两个交点点到触发点的距离
CGFloatdistance1 =sqrtf(powf(x1 - pointOne.x,2) +powf(y1 - pointOne.y,2));
CGFloatdistance2 =sqrtf(powf(x2 - pointOne.x,2) +powf(y2 - pointOne.y,2));
//选择距离近的, 由于OC坐标系与数学坐标系的差异,y值取反转成OC坐标系
CGPointintersectPoint1 =CGPointMake(x1, -y1);
CGPointintersectPoint2 =CGPointMake(x2, -y2);
if(distance1 < distance2) {
returnintersectPoint1;
}
else{
returnintersectPoint2;
}
}
网友评论