PaintCode: Does the circle support A / R?

In Paintcode 2, I have a circle inside the frame inside the canvas.

Circle restrictions are defined as follows:

enter image description here

To get the size of the circle and not become an ellipse, I have to

  • Know the expected aspect ratio
  • Enter the code in Objective-C

Is there any way around this type of code?

-(void)drawRect:(CGRect)rect { if (rect.size.width > rect.size.height) { rect.origin.x = (rect.size.width - rect.size.height) * .5f; rect.size.width = rect.size.height; } else { rect.origin.y = (rect.size.height - rect.size.width) * .5f; rect.size.height = rect.size.width; } NSLog(@"Frame=%@", NSStringFromCGRect(rect)); [CircleDraw drawCircleWithFrame:rect]; } 
+6
source share
1 answer
  • Create canvas, 150x120
  • Create an ellipse, 10, 10, 100, 100
  • Create a new variable frame , enter the rectangle: 10, 10, 150, 100
  • Create a new smallestSide expression: min(frame.height, frame.width)
  • Make an expression called position (below)
  • Drag smallestSide to the height and width of the ellipse
  • Drag position to ellipse position


position (expression)
  makePoint( frame.x+(frame.width-smallestSide)*0.5, frame.y+(frame.height-smallestSide)*0.5 ) 


Output
 - (void)drawCanvas1WithFrame: (CGRect)frame { //// Variable Declarations CGFloat smallestSide = MIN(frame.size.height, frame.size.width); CGPoint position = CGPointMake(frame.origin.x + (frame.size.width - smallestSide) * 0.5, frame.origin.y + (frame.size.height - smallestSide) * 0.5); //// Oval Drawing UIBezierPath* ovalPath = [UIBezierPath bezierPathWithOvalInRect: CGRectMake(position.x, position.y, smallestSide, smallestSide)]; [UIColor.grayColor setFill]; [ovalPath fill]; } 

NOTE. . I was helped by Matt Dunik at PaintCode to figure this out, but the solution is actually very simple.

+4
source

Source: https://habr.com/ru/post/982109/


All Articles