在Objective-C中绘制具有起始和结束角度的椭圆 [英] Draw ellipse with start and end angle in Objective-C
问题描述
我正在编写一个iPad应用程序,我在其中呈现XML对象,这些对象将形状表示为屏幕上的图形。我试图渲染的对象之一是弧。基本上这些弧为我提供了一个边界矩形以及开始和结束角度。
I am writing an iPad app in which I am rendering XML objects that represent shapes into graphics on the screen. One of the objects I am trying to render is arcs. Essentially these arcs provide me with a bounding rectangle as well as a start and end angle.
给定属性:
- x
- y
- width
- height
- startAngle
- endAngle
- x
- y
- width
- height
- startAngle
- endAngle
使用这些值我需要绘制弧(基本上是椭圆的一部分)。我不能使用以下内容:
With these values I need to draw the arc (which is essentially part of an ellipse). I can not use the following:
UIBezierPath *arc = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(x, y, width, height)];
[UIColor blackColor] setStroke];
[arc stroke];
因为它绘制了一个完整的椭圆。基本上我需要上面但是它需要考虑开始和结束角度,因此只显示椭圆的一部分。我认为这将涉及绘制三次贝塞尔曲线或二次贝塞尔曲线。问题是我不知道如何用我给出的信息计算起点,终点或控制点。
because it draws a full ellipse. Basically I need the above but it needs to take into account the start and end angles so only part of the ellipse is displayed. I am thinking this will involve either drawing a cubic Bezier curve or a quadratic Bezier curve. The problem is I have no clue how to calculate the start point, end point, or control points with the information I am given.
推荐答案
你可以通过在椭圆的绘图周围设置一个剪辑路径来实现你想要的效果。
You can probably achieve what you want by setting up a clip path around the drawing of the ellipse.
CGContextSaveGState(theCGContext);
CGPoint center = CGPointMake(x + width / 2.0, y + height / 2.0);
UIBezierPath* clip = [UIBezierPath bezierPathWithArcCenter:center
radius:max(width, height)
startAngle:startAngle
endAngle:endAngle
clockwise:YES];
[clip addLineToPoint:center];
[clip closePath];
[clip addClip];
UIBezierPath *arc = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(x, y, width, height)];
[[UIColor blackColor] setStroke];
[arc stroke];
CGContextRestoreGState(theCGContext);
裁剪的确切半径并不重要。它需要足够大,以便它只在末端剪切椭圆,而不是通过所需的弧。
The exact radius for the clipping isn't important. It needs to be big enough so it only clips the ellipse at the ends, not through the desired arc.
这篇关于在Objective-C中绘制具有起始和结束角度的椭圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!