Skip to content

Instantly share code, notes, and snippets.

@stevebrun
Created May 7, 2014 20:04
Show Gist options
  • Save stevebrun/2231e870dc41f3288afc to your computer and use it in GitHub Desktop.
Save stevebrun/2231e870dc41f3288afc to your computer and use it in GitHub Desktop.
Here is the code for getting points around a circle.
static CGPoint convertRadianToCGPoint( double angle, double length, CGPoint center ) {
double xCoord = length * cos( angle );
if( angle == M_PI ) {
xCoord = -length;
}
double yCoord = length * sin( angle );
if( angle == (3 * M_PI)/2 ){
yCoord = -length;
}
return CGPointMake( center.x - xCoord, center.y - yCoord );
}
typedef void (^pointPositionSetter)(int pointID, CGPoint position);
void arrangeNPointsInACircle(int n, CGRect bounds, pointPositionSetter setPosition, BOOL asLetters)
{
int _vertexCount = n;
CGRect _vertexBounds = bounds;
BOOL _verticesAsLetters = asLetters;
double width = _vertexBounds.width / 2.;
double height = _vertexBounds.height / 2.;
CGPoint center = CGPointMake( width, height );
double radius = width / 1.21;
double startCoord = M_PI / 2;
double increment = ( 2 * M_PI ) / (double)_vertexCount;
if (_vertexCount == 12 && !_verticesAsLetters) {
startCoord += increment;
} else if (_vertexCount == 1) {
radius = 0;
}
for ( int vertex=0; vertex < _vertexCount; vertex++ ) {
double radianCoord = startCoord + ( increment * vertex );
CGPoint position = convertRadianToCGPoint( radianCoord, radius, center );
setPosition(vertex+1, position);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment