绘制部分圆 [英] Drawing a partial circle

查看:35
本文介绍了绘制部分圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,它会取一个 0 到 1 之间的数字,然后吐出一个圆(或我猜是圆弧),它完成了那么多.

I'm writing a program that will take a number between 0 and 1, and then spits out a circle (or arc I guess) that is completed by that much.

比如输入0.5,程序会输出一个半圆

So for example, if 0.5 was inputted, the program would output a semicircle

如果为 0.1,程序将输出一个小圆弧,最终占整个圆的 10%.

if 0.1, the program would output a tiny little arc that would ultimately be 10% of the whole circle.

我可以通过使角度起点为 0,角度终点为 2*M_PI*decimalInput

I can get this to work by making the angle starting point 0, and the angle ending point 2*M_PI*decimalInput

但是,我需要在圆的顶部有起点,所以起点是3*M_PI_2,终点是7*M_PI_2

However, I need to have the starting point at the top of the circle, so the starting point is 3*M_PI_2 and the ending point would be 7*M_PI_2

我只是在用这些新的起点/终点绘制部分完整的圆时遇到了问题.我承认,我的数学不是最好的,所以任何建议/意见都值得赞赏

I'm just having trouble drawing a circle partially complete with these new starting/ending points. And I'll admit, my math is not the best so any advice/input is appreciated

这是我目前所拥有的

var decimalInput = 0.75 //this number can be any number between 0 and 1
let start = CGFloat(3*M_PI_2)
let end = CGFloat(7*M_PI_2*decimalInput)

let circlePath = UIBezierPath(arcCenter: circleCenter, radius: circleRadius, startAngle: start, endAngle: end, clockwise: true)

circlePath.stroke()

不管我怎么努力,我似乎​​都做对了.我认为结束角度是罪魁祸首,除非我以错误的方式进行处理

I just cannot seem to get it right despite what I try. I reckon the end angle is culprit, unless I'm going about this the wrong way

推荐答案

弧长为 2 * M_PI * decimalInput.您需要将弧长添加到起始角度,如下所示:

The arc length is 2 * M_PI * decimalInput. You need to add the arc length to the starting angle, like this:

let circleCenter = CGPointMake(100, 100)
let circleRadius = CGFloat(80)
var decimalInput = 0.75
let start = CGFloat(3 * M_PI_2)
let end = start + CGFloat(2 * M_PI * decimalInput)
let circlePath = UIBezierPath(arcCenter: circleCenter, radius: circleRadius, startAngle: start, endAngle: end, clockwise: true)
XCPCaptureValue("path", circlePath)

结果:

注意在UIView中绘制时,路径会垂直翻转.

Note that the path will be flipped vertically when used to draw in a UIView.

这篇关于绘制部分圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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