ios 如何将度数转换为弧度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29179692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I convert from degrees to radians?
提问by Dharmesh Kheni
I am trying to convert this Obj-C
code to Swift
code but I don't know what the equivalent of this code should be ?
我正在尝试将此Obj-C
代码转换为Swift
代码,但我不知道此代码的等价物应该是什么?
#define DEGREES_TO_RADIANS(degrees)((M_PI * degrees)/180)
I googled and found this
我用谷歌搜索并找到了这个
But I don't understand how to convert that in Swift in my case?
但我不明白在我的情况下如何在 Swift 中转换它?
回答by Leo Dabus
Xcode 11 ? Swift 5.1 or later
Xcode 11?Swift 5.1 或更高版本
extension BinaryInteger {
var degreesToRadians: CGFloat { CGFloat(self) * .pi / 180 }
}
extension FloatingPoint {
var degreesToRadians: Self { self * .pi / 180 }
var radiansToDegrees: Self { self * 180 / .pi }
}
Playground
操场
45.degreesToRadians // 0.7853981633974483
Int(45).degreesToRadians // 0.7853981633974483
Int8(45).degreesToRadians // 0.7853981633974483
Int16(45).degreesToRadians // 0.7853981633974483
Int32(45).degreesToRadians // 0.7853981633974483
Int64(45).degreesToRadians // 0.7853981633974483
UInt(45).degreesToRadians // 0.7853981633974483
UInt8(45).degreesToRadians // 0.7853981633974483
UInt16(45).degreesToRadians // 0.7853981633974483
UInt32(45).degreesToRadians // 0.7853981633974483
UInt64(45).degreesToRadians // 0.7853981633974483
Double(45).degreesToRadians // 0.7853981633974483
CGFloat(45).degreesToRadians // 0.7853981633974483
Float(45).degreesToRadians // 0.7853981
Float80(45).degreesToRadians // 0.78539816339744830963
If you would like to make the binary integer return a floating point type instead of always returning a CGFloat you can make a generic method instead of a computed property:
如果你想让二进制整数返回一个浮点类型而不是总是返回一个 CGFloat 你可以创建一个泛型方法而不是一个计算属性:
extension BinaryInteger {
func degreesToRadians<F: FloatingPoint>() -> F { F(self) * .pi / 180 }
}
let radiansDouble: Double = 45.degreesToRadians() // 0.7853981633974483
let radiansCGFloat: CGFloat = 45.degreesToRadians() // 0.7853981633974483
let radiansFloat: Float = 45.degreesToRadians() // 0.7853981
let radiansFloat80: Float80 = 45.degreesToRadians() // 0.78539816339744830963
回答by matt
This is not identically what you asked, but in Swift 3 / iOS 10, you can use the Measurement type and do the conversion without knowing the formula!
这与您所要求的不同,但是在 Swift 3 / iOS 10 中,您可以使用测量类型并在不知道公式的情况下进行转换!
let result = Measurement(value: 45, unit: UnitAngle.degrees)
.converted(to: .radians).value
回答by Crashalot
Apple provides these GLKit functionsfor conversion:
Apple 提供了以下GLKit 函数进行转换:
func GLKMathDegreesToRadians(_ degrees: Float) -> Float
func GLKMathRadiansToDegrees(_ radians: Float) -> Float
回答by t1ser
let angle = 45° // angle will be in radians, 45 is in degrees
Compiles under Swift 3. Still keep all values, do all calculations in radians with CGFloats..., but make the code more readable with the constants in degrees. For example: 90°The ° sign will magically do the degrees to radians conversion.
在Swift 3下编译。仍然保留所有值,使用 CGFloats 以弧度进行所有计算...,但使用以度为单位的常量使代码更具可读性。例如:90°° 符号会神奇地将度数转换为弧度。
How to set this up:
如何设置:
Define and use a postfix operator for the °sign. This operator will do the conversion from degrees to radians. This example is for Ints, extend these also for the Float types if you have the need.
为°符号定义并使用后缀运算符。此运算符将进行从度数到弧度的转换。此示例适用于 Int,如果需要,也可以将它们扩展为 Float 类型。
postfix operator °
protocol IntegerInitializable: ExpressibleByIntegerLiteral {
init (_: Int)
}
extension Int: IntegerInitializable {
postfix public static func °(lhs: Int) -> CGFloat {
return CGFloat(lhs) * .pi / 180
}
}
Some examples of usage:
一些用法示例:
let angle = 45°
contentView.transform = CGAffineTransform(rotationAngle: 45°)
let angle = 45
contentView.transform = CGAffineTransform(rotationAngle: angle°)
Warning!
警告!
It is too easy to use this conversion twice (on a value already in radians by mistake), you will get a very small number as the result, and seemingly the resulting angle will be always zero... DO NOT use ° on the same value twice (do not convert twice)!!:
使用这种转换两次太容易了(错误地在一个已经以弧度表示的值上),结果你会得到一个非常小的数字,而且看起来结果角度将始终为零......不要在相同的情况下使用°值两次(不要转换两次)!!:
// OBVIOUSLY WRONG!
let angle = 45°° // ° used twice here
// WRONG! BUT EASY TO MISS
let angle = 45° // ° used here
contentView.transform = CGAffineTransform(rotationAngle: angle°) // ° also used here
回答by nikans
Also, to convert fron radians to degrees (if anyone stumbles upon this on google):
另外,要将 fron 弧度转换为度数(如果有人在 google 上偶然发现了这个):
var degrees = radians * (180.0 / M_PI)
回答by Ashley Mills
You're no longer limited to ASCII characters when creating variable names, so how about this using π (alt-p):
创建变量名时不再局限于 ASCII 字符,那么使用 π (alt-p) 怎么样:
typealias RadianAngle = CGFloat
let π = RadianAngle(M_PI)
let π_x_2 = RadianAngle(M_PI * 2)
let π_2 = RadianAngle(M_PI_2)
let π_4 = RadianAngle(M_PI_4)
extension RadianAngle {
var degrees: CGFloat {
return self * 180 / π
}
init(degrees: Int) {
self = CGFloat(degrees) * π / 180
}
}
Example usage:
用法示例:
let quarterCircle = RadianAngle(degrees: 90)
print("quarter circle = \(quarterCircle) radians")
// quarter circle = 1.5707963267949 radians
let halfCircle = π
print("half circle = \(halfCircle.degrees) degrees")
// half circle = 180.0 degrees
回答by Tikhonov Alexander
Swift 4.2
斯威夫特 4.2
You can write global generic functions:
您可以编写全局通用函数:
public func radians<T: FloatingPoint>(degrees: T) -> T {
return .pi * degrees / 180
}
public func degrees<T: FloatingPoint>(radians: T) -> T {
return radians * 180 / .pi
}
Example:
例子:
let cgFloat: CGFloat = 23.0
let double: Double = 23.0
let float: Float = 23.0
let int: Int = 23
let cgf = radians(degrees: cgFloat) // 0.4014 CGFloat
let d = radians(degrees: double) // 0.4014 Double
let f = radians(degrees: float) // 0.4014 Float
let i = radians(degrees: int) // compile error
In case when you don't want to extension all your floating types like this
如果您不想像这样扩展所有浮动类型
extension FloatingPoint { ... }
回答by Septronic
I'd use the same principle @t1ser mentioned above, but create an extension of CGFloat
to make it easier to use decimals for the degree as well (so you could have a 23.4 degrees, for example):
我会使用上面提到的相同原则@t1ser,但创建一个扩展名,CGFloat
以便更容易地使用小数作为度数(例如,您可以使用 23.4 度):
extension CGFloat {
func degrees() -> CGFloat {
return self * .pi / 180
}
init(degrees: CGFloat) {
self = degrees.degrees()
}
}
Using it would be pretty easy as well (mainly because I personally didn't know how to type ° - in case you didn't either, it's option+shift+8
btw):
使用它也很容易(主要是因为我个人不知道如何输入° - 如果你也不知道,option+shift+8
顺便说一句):
let degreesToConvert: CGFloat = 45.7
.
.
.
let convertedDegrees = degreesToConvert.degrees()
Or to use the initialiser:
或者使用初始化程序:
let convertedDegrees = CGFloat(degrees: 45.3)
回答by HardikParmar
swift 2.3
快速 2.3
func kilometersBetween(Place1:CLLocationCoordinate2D , Place2:CLLocationCoordinate2D) -> Double{
var start = MKMapPointForCoordinate(Place1)
var finish = MKMapPointForCoordinate(Place2)
print(MKMetersBetweenMapPoints(start, finish) / 1000)
return MKMetersBetweenMapPoints(start, finish) / 1000
}