在Objective C中绘制饼图

3
我用以下代码在Objective C中画了一个弧形:
    [newPath appendBezierPathWithArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle];

现在我想从圆弧的中心画两条直线,一条连接起点,另一条连接终点。我该怎么做?其中一侧可以使用以下代码绘制:
    [newPath lineToPoint:center];

但是其他方面呢?

当光标位于中心时,您可以使用三角函数来计算弧的起始点,然后使用[newPath lineToPoint:startingPoint] - user529758
2个回答

4

最简单的绘制饼图的方法是利用appendBezierPathWithArcWithCenter:...方法绘制从当前点到弧线起点的线:

// Start at the center of the circle:
[newPath moveToPoint:center];
// This draws a line from the center to the starting point of the arc AND the arc:
[newPath appendBezierPathWithArcWithCenter:center radius:radius startAngle:startAngle endAngle:endAngle];
// Closing the path draws a line from the end point of the arc back to the center:
[newPath closePath];

在iOS SDK中,等效的方法是addArcWithCenter:。 - Abduliam Rehmanius

0

目前我没有机会测试它,但你可以尝试类似这样的方法。

#import "math.h"

[newPath moveToPoint:center];
[newPath addLineToPoint:CGPointMake(center.x + radius * cos(endAngle * M_PI / 180),
                                    center.y + radius * sin(endAngle * M_PI / 180))];

[newPath moveToPoint:center];
[newPath addLineToPoint:CGPointMake(center.x + radius * cos(startAngle * M_PI / 180),
                                    center.y + radius * sin(startAngle * M_PI / 180))];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接