obj c get point of intersection of 2 lines

Solutions on MaxInterview for obj c get point of intersection of 2 lines by the best coders in the world

showing results for - "obj c get point of intersection of 2 lines"
Gayle
01 Nov 2019
1-(NSPoint)line:(NSPoint)p1 end:(NSPoint)p2 with:(NSPoint)p3 end:(NSPoint)p4{
2    //(y4 - y3)*(x2 - x1) - (x4 - x3)*(y2 - y1);
3    float denom = (p4.y - p3.y)*(p2.x - p1.x) - (p4.x - p3.x)*(p2.y - p1.y);
4    if(denom == 0){
5      //returns null because the lines never intersect
6        return NSPointFromString(@"0,0");
7    }
8    //ua = ((x4 - x3)*(y1 - y3) - (y4 - y3)*(x1 - x3))/denom;
9    //ub = ((x2 - x1)*(y1 - y3) - (y2 - y1)*(x1 - x3))/denom;
10    float ua = ((p4.x - p3.x)*(p1.y - p3.y) - (p4.y - p3.y)*(p1.x - p3.x))/denom;
11    float ub = ((p2.x - p1.x)*(p1.y - p3.y) - (p2.y - p1.y)*(p1.x - p3.x))/denom;
12    bool seg1 = ua >= 0 && ua <= 1;
13    bool seg2 = ub >= 0 && ub <= 1;
14    if(seg1==true && seg2 == true){
15      // returns a point if the point of intersection is on the line you may tweak this
16        return NSPointFromCGPoint(CGPointMake(p1.x + ua * (p2.x - p1.x), p1.y + ua * (p2.y - p1.y)));
17    }else{
18        return NSPointFromString(@"0,0");
19    }
20    
21    
22}
similar questions
queries leading to this page
obj c get point of intersection of 2 lines