美文网首页
计算两条直线的交点

计算两条直线的交点

作者: 流沙3333 | 来源:发表于2017-03-10 14:57 被阅读83次

    + (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;

    }

    相关文章

      网友评论

          本文标题:计算两条直线的交点

          本文链接:https://www.haomeiwen.com/subject/dhzngttx.html