Skip to content

Instantly share code, notes, and snippets.

@densa
Created April 10, 2015 09:13
Show Gist options
  • Save densa/3fb0fea6f135e5412aab to your computer and use it in GitHub Desktop.
Save densa/3fb0fea6f135e5412aab to your computer and use it in GitHub Desktop.
findPointOnTangentOnCircleWithCenter
+(double)distanceBetweenPoint:(CGPoint)pointA andAnotherPoint:(CGPoint)pointB
{
return sqrt((pointA.x - pointB.x)*(pointA.x - pointB.x) + (pointA.y - pointB.y)*(pointA.y - pointB.y));
}
+(NSArray *)findPointOnTangentOnCircleWithCenter:(CGPoint)center Radius:(double)radius pointOnTangent:(CGPoint)point
{
double dx = center.x - point.x;
double dy = center.y - point.y;
double distance = [EXMathBrain distanceBetweenPoint:center andAnotherPoint:point];
double alfa = atan2(dy, dx);
double beta = asin(radius/distance);
double segmentLen = sqrt(distance*distance - radius*radius);
NSMutableArray *results = [NSMutableArray arrayWithCapacity:2];
dx = cos(alfa+beta)*segmentLen;
dy = sin(alfa+beta)*segmentLen;
[results addObject:[NSValue valueWithCGPoint:CGPointMake(point.x+dx, point.y+dy)]];
dx = cos(alfa-beta)*segmentLen;
dy = sin(alfa-beta)*segmentLen;
[results addObject:[NSValue valueWithCGPoint:CGPointMake(point.x+dx, point.y+dy)]];
return results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment