计算两条直线的交点

+ (CGPoint)getCrossPointWithPoint:(CGPoint)p1 p2:(CGPoint)p2 p3:(CGPoint)p3 p4:(CGPoint)p4 {

float K1,K2,N1,N2;

float x,y;

K1 = [self getLineK:p1Point2:p2ConstN:&N1];

K2 = [self getLineK:p3Point2:p4ConstN:&N2];

if(K1 == K2){//两条直线平行

return CGPointZero;

}

x = (N2 - N1 )/(K1 - K2);

y = K1 * x + N1;

NSLog(@"x:%f ,y:%f",x,y);

return CGPointMake(x, y);

}

+ (float)getLineK:(CGPoint)point1 Point2:(CGPoint)point2 ConstN:(float*)N{

float K = (point1.y- point2.y)/(point1.x- point2.x);

*N = point1.y- K * point1.x;

return K;

}

你可能感兴趣的:(计算两条直线的交点)