Skip to content

Instantly share code, notes, and snippets.

@ajonnet
Created March 4, 2016 06:52
Show Gist options
  • Save ajonnet/6fc0cf6e66684f4a54a9 to your computer and use it in GitHub Desktop.
Save ajonnet/6fc0cf6e66684f4a54a9 to your computer and use it in GitHub Desktop.
Function to print circle of Asterisk in Objective C
-(void) asteriskCircleWidthRadius:(CGFloat)rad filled:(BOOL) fill border:(NSUInteger) borderW{
borderW -= 1;
CGFloat dia = (rad + borderW) * 2;
CGFloat centerX,centerY;
centerX = centerY = dia/2.0; //-1 for
NSMutableString *op = [NSMutableString string];
NSMutableString *opCircle = [NSMutableString string];
[op appendString:@"\n"];
[opCircle appendString:@"\n"];
for (NSUInteger x = 0; x<=dia; x++) {
for (NSUInteger y = 0; y<=dia; y++) {
double dis = sqrt((x-centerX)*(x-centerX) + (y-centerY)*(y-centerY));
[op appendFormat:@"%5.02f ",dis];
CGFloat tolerance = borderW + 0.4;
if ((dis >= (rad - tolerance) || fill) && (dis <= (rad + tolerance))) {
[opCircle appendString:@"* "];
}else {
[opCircle appendString:@" "];
}
}
[op appendString:@"\n"];
[opCircle appendString:@"\n"];
}
//NSLog(@"%@",op);
NSLog(@"%@",opCircle);
}
@ajonnet
Copy link
Author

ajonnet commented Mar 4, 2016

Sample output

              * * * * *               
          * * * * * * * * *           
      * * * * * * * * * * * * *       
    * * * * *           * * * * *     
    * * *                   * * *     
  * * *                       * * *   
  * * *                       * * *   
* * *                           * * * 
* * *                           * * * 
* * *                           * * * 
* * *                           * * * 
* * *                           * * * 
  * * *                       * * *   
  * * *                       * * *   
    * * *                   * * *     
    * * * * *           * * * * *     
      * * * * * * * * * * * * *       
          * * * * * * * * *           
              * * * * *            

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment