簡體   English   中英

如何在Objective-C中將點轉換為弧度?

[英]How to convert points to radians in Objective-C?

在我的應用程序中,我使用 UIBezierPath 將圓弧繪制成圓。 我試圖將一個數字與弧度相關聯。 所以假設用戶有一定數量的積分,積分上限為 100 分。 我希望 100 點是 360 度。 我希望圓圈的前 33% 是綠色,然后從圓圈的 34% 到接下來的 66% 用橙色描邊,然后從 67% 到 100% 用紅色描邊。

我在這里遇到的問題是將圓的百分比轉換為弧度。 創建 UIBezier 路徑時,我需要提供 startAngle 和 endAngle,並且在將這些點轉換為弧度值時遇到了一些麻煩。

我將如何解決這個問題?

謝謝

CGFloat radians = percent * 0.01 * 2 * M_PI;

簡單代數。

迅捷版

使其更通用,您可以編寫一個轉換函數:

func radiansFromPercent(_ percent: CGFloat) -> CGFloat {
    return percent * 0.01 * 2 * CGFloat.pi
}

我想你想要的是單位圓。 還記得使用單位圓時的三角學嗎? 同樣的事情也適用於此。 如果您需要在 Swift 中獲得 π - 只需說let π = CGFloat.pi (按住 alt+p 表示特殊字符)。 在 Objective-C 中 - 我認為是CGFloat π = M_PI; .

在此處輸入圖片說明

對於前 1/3,您可以從零到2π/3 ,然后從2π/34π/3 ,然后從4π/3 (整圓)。

我不應該說我沒有制作這個圖形——它來自 RayWenderlich.com 上的教程——但它完全適合 iOS 坐標系。

目標-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

迅速

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM