iOS CoreGraphics:绘制圆弧,根据相交的和弦定理确定圆弧角度

dev*_*oug 6 core-graphics objective-c ios

我正在试图弄清楚如何在CoreGraphics中绘制弧线.我理解在下面的场景中调用哪个方法以及如何计算角度.

----------
|        |
*--------*
Run Code Online (Sandbox Code Playgroud)

当点都在矩形的底部.但是当两个点位于其他位置时,我不知道如何计算正确的角度.

---------*
|        |
*---------
Run Code Online (Sandbox Code Playgroud)

查看我图片的底部.

在此输入图像描述

Ray Wenderlich有一个很好的教程,关于仅在第一个提到的点位置创建弧.

// 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;
}
Run Code Online (Sandbox Code Playgroud)

如何在我的图像底部描绘的其他情况下计算角度?

小智 9

我发现更简单的方法是使用弧线:

void CGContextAddArcToPoint (
   CGContextRef c,
   CGFloat x1,
   CGFloat y1,
   CGFloat x2,
   CGFloat y2,
   CGFloat radius
);
Run Code Online (Sandbox Code Playgroud)

如果您从Ray Wenderlich的网站(https://www.raywenderlich.com/33330/core-graphics-tutorial-glossy-buttons)查看此图像,则点(x1,y1)是曲线和点的起点( x2,y2)是你的终点.然后只需指定角半径和瞧!看起来这可能是一个更容易使用的API,用于您想要做的事情.