Skip to content

Instantly share code, notes, and snippets.

@stan1y
Created March 29, 2011 08:55
Show Gist options
  • Select an option

  • Save stan1y/892028 to your computer and use it in GitHub Desktop.

Select an option

Save stan1y/892028 to your computer and use it in GitHub Desktop.
- (StellarPosition) getOribitOf:(StellarObject*)target;
{
//find intersection of line (position.x, position.y)..(target.x, target.y)
//with circle around (target.x, target.y) with radius 'orbitRadius'
//current angle to target
double angleRadians = [self angleTo:[target position]];
//get point with angle on circle with R=orbitRadius, center (target.x, target.y)
double pointX = [target position].x + orbitRadius * cos(angleRadians);
double pointY = [target position].y - orbitRadius * sin(angleRadians);
StellarPosition intersection = { pointX, pointY };
return intersection;
}
- (double) angleTo:(StellarPosition)pos
{
double a = pos.x - position.x;
double b = pos.y - position.y;
//the cot(angle) = a/b -> angle = acot(a/b) = Pi - atan(b/a)
return Pi - atan(b / a);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment