Intersection between two lines in coordinates

后端 未结 6 1674
臣服心动
臣服心动 2020-12-31 19:42

I can detect the intersection point of two lines, but if my line don\'t has the length of my screen, it detects the point, where it shouldn\'t be.

Here a preview:

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-31 19:59

    That's the correct equation:

    +(CGPoint) intersection2:(CGPoint)u1 u2:(CGPoint)u2 v1:(CGPoint)v1 v2:(CGPoint)v2 {  
        CGPoint ret=u1;  
        double t=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))  
        /((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));  
        ret.x+=(u2.x-u1.x)*t;  
        ret.y+=(u2.y-u1.y)*t;  
        return ret;  
    }  
    

    Also, you can check this library to calculate line intersections: http://www.cprogramdevelop.com/5045485/

提交回复
热议问题