IOS CoreGraphics: draw an arc, determine angle angles from intersecting chord theorem

I am trying to figure out how to draw an arc in CoreGraphics. I understand which method calls the call and how to calculate the angles in the following scenario.

---------- | | *--------* 

When the dots are at the bottom of the rectangle. However, when the two points are in other places, I do not know how to calculate the correct angle.

 ---------* | | *--------- 

See the bottom of my image.

enter image description here

Ray Wenderlich has an excellent tutorial on creating arcs only in the first points mentioned.

 // sample code for creating arc for path from bottom of rect CGMutablePathRef createArcPathFromBottomOfRect(CGRect rect, CGFloat arcHeight) { CGRect arcRect = CGRectMake(rect.origin.x, rect.origin.y + rect.size.height - arcHeight, rect.size.width, arcHeight); CGFloat arcRadius = (arcRect.size.height/2) + (pow(arcRect.size.width, 2) / (8 * arcRect.size.height)); CGPoint arcCenter = CGPointMake(arcRect.origin.x + arc.size.width/2, arcRect.origin.y + arcRadius); CGFloat angle = acos(arcRect.size.width/ (2*arcRadius)); CGFloat startAngle = radians(180) + angle; CGFloat endAngle = radians(360) - angle; CGMutablePathRef path = CGPathCreateMutable(); CGPathAddArc(path, NULL, arcCenter.x, arcCenter.y, arcRadius, startAngle, endAngle, 0); return path; } 

How to calculate the angle in other situations, as shown at the bottom of the image?

+6
source share
2 answers

I find it easier to use arcs:

 void CGContextAddArcToPoint ( CGContextRef c, CGFloat x1, CGFloat y1, CGFloat x2, CGFloat y2, CGFloat radius ); 

If you look at this image from the Ray Wenderlich website ( http://www.raywenderlich.com/2134/core-graphics-101-glossy-buttons/cgcontextaddarctopoint ), the point (x1, y1) is your starting point for the curve and the point (x2, y2) is your endpoint. Then just specify the radius of the corner and voila! It looks like this might be a simpler API to use for what you are looking for.

+9
source

To determine the circle you need at least 3 points.

In your first senario, where two points are at the bottom of the rectangle, the top midpoint is implicitly the third point when arcHeight is known. Therefore, three points define a circle, thus an arc. Thus, it is possible to calculate all angles, etc.

In your second Senario, however, the third point is not defined. Therefore, you can draw an infinite number of arcs passing through two points with different curvatures. You will need additional requirements for a fixed arc. For example, the radius or third point must be a single arc.

0
source

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


All Articles