How to convert points to radians in Objective-C?

北慕城南 提交于 2019-12-01 13:27:51

问题


In my app I'm using UIBezierPath to draw an arc into a circle. I'm trying to correlate a number to radians. So let's say a user has a certain number of points, and the points are capped at 100 points. I want 100 points to be 360 degrees. I want the first 33% of the circle to be green, and then from 34% to the next 66% of the circle to be stroked in orange, and then from 67% to 100% in red.

The issue I'm having here is converting percents of a circle to radians. When creating a UIBezier path, I need to provide a startAngle and endAngle, and I'm having a bit of trouble converting these points to radian values.

How would I go about solving this?

Thanks


回答1:


CGFloat radians = percent * 0.01 * 2 * M_PI;

Simple algebra.




回答2:


I think what you want is the unit circle. Remember back to trigonometry when you used the unit circle? Same thing will apply here. If you need to get π - in Swift just say let π = CGFloat.pi (hold alt+p for the special character). In Objective-C - I think it's CGFloat π = M_PI;.

You could go from zero to 2π/3 for the first 1/3, then from 2π/3 to 4π/3, then from 4π/3 to (full circle).

I should not that I didn't make this graphic - it's from a tutorial on RayWenderlich.com - but it's oriented perfectly for the iOS coordinate system.




回答3:


Objective-C

CGFloat fullCircle = 2 * M_PI ;             // M_PI Pi number which is half of the circle in radian
CGFloat startPoint = 0.0      ;
CGFloat endPoint   = fullCircle * 0.33 ;

// Assuming circling clockwise

// .... Draw first step UIBezierPath

startPoint = endPoint        ;
endPoint = startPoint + fullCircle * 0.33 ;
// .... Draw second step UIBezierPath

startPoint = endPoint        ;
endPoint = fullCircle - startPoint ;       // This to make sure the whole circle will be covered
// .... Draw the last step UIBezierPath

Swift

let fullCircle = 2 * M_PI             // M_PI Pi number which is half of the circle in radian
var startPoint: Float = 0.0
var endPoint: Float = fullCircle * 0.33

// Assuming circling clockwise
// .... Draw first step UIBezierPath

startPoint = endPoint
endPoint = startPoint + fullCircle * 0.33
// .... Draw second step UIBezierPath

startPoint = endPoint
endPoint = fullCircle - startPoint       // This to make sure the whole circle will be covered
// .... Draw the last step UIBezierPath


来源:https://stackoverflow.com/questions/41905131/how-to-convert-points-to-radians-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!