get point of line intersection code example
Example: obj c get point of intersection of 2 lines
-(NSPoint)line:(NSPoint)p1 end:(NSPoint)p2 with:(NSPoint)p3 end:(NSPoint)p4{
float denom = (p4.y - p3.y)*(p2.x - p1.x) - (p4.x - p3.x)*(p2.y - p1.y);
if(denom == 0){
return NSPointFromString(@"0,0");
}
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){
return NSPointFromCGPoint(CGPointMake(p1.x + ua * (p2.x - p1.x), p1.y + ua * (p2.y - p1.y)));
}else{
return NSPointFromString(@"0,0");
}
}