iOS CoreGraphics:绘制圆弧,根据相交的和弦定理确定圆弧角度 [英] iOS CoreGraphics: Draw arc, determine arc angles from intersecting chord theorem

查看:489
本文介绍了iOS CoreGraphics:绘制圆弧,根据相交的和弦定理确定圆弧角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to figure out how to draw an arc in CoreGraphics. I understand which method calls to make and how to compute the angles in the following scenario.

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

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

When the points are both in the bottom of the rect. However when two points are in other locations, I don't know how to calculate the correct angle.

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

查看我图片的下半部分。

See bottom portion of my image.

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

Ray Wenderlich has a great tutorial about creating arcs for only in the first mentioned point positions.

// 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 do I calculate the angle when in other situations as depicted at the bottom of my image?

推荐答案

我找到一种更简单的方法来制作弧线:

I find an easier way to make arcs is to use:

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

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

If you look at this image from Ray Wenderlich's site (https://www.raywenderlich.com/33330/core-graphics-tutorial-glossy-buttons), point (x1,y1) is your start point for the curve and point (x2,y2) is your end point. Then just specify the corner radius and voila! It looks like this may be an easier API to use for what you are looking to do.

这篇关于iOS CoreGraphics:绘制圆弧,根据相交的和弦定理确定圆弧角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆