Answers for "obj c get point of intersection of 2 lines"

1

obj c get point of intersection of 2 lines

-(NSPoint)line:(NSPoint)p1 end:(NSPoint)p2 with:(NSPoint)p3 end:(NSPoint)p4{
    //(y4 - y3)*(x2 - x1) - (x4 - x3)*(y2 - y1);
    float denom = (p4.y - p3.y)*(p2.x - p1.x) - (p4.x - p3.x)*(p2.y - p1.y);
    if(denom == 0){
      //returns null because the lines never intersect
        return NSPointFromString(@"0,0");
    }
    //ua = ((x4 - x3)*(y1 - y3) - (y4 - y3)*(x1 - x3))/denom;
    //ub = ((x2 - x1)*(y1 - y3) - (y2 - y1)*(x1 - x3))/denom;
    float ua = ((p4.x - p3.x)*(p1.y - p3.y) - (p4.y - p3.y)*(p1.x - p3.x))/denom;
    float ub = ((p2.x - p1.x)*(p1.y - p3.y) - (p2.y - p1.y)*(p1.x - p3.x))/denom;
    bool seg1 = ua >= 0 && ua <= 1;
    bool seg2 = ub >= 0 && ub <= 1;
    if(seg1==true && seg2 == true){
      // returns a point if the point of intersection is on the line you may tweak this
        return NSPointFromCGPoint(CGPointMake(p1.x + ua * (p2.x - p1.x), p1.y + ua * (p2.y - p1.y)));
    }else{
        return NSPointFromString(@"0,0");
    }
    
    
}
Posted by: Guest on April-29-2020

Code answers related to "obj c get point of intersection of 2 lines"

Code answers related to "Objective-C"

Browse Popular Code Answers by Language