如何将度数转换为弧度? [英] How can I convert from degrees to radians?

查看:344
本文介绍了如何将度数转换为弧度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将此 Obj-C 代码转换为 Swift 代码,但我不知道是什么相当于这个代码应该是?

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 ?

#define DEGREES_TO_RADIANS(degrees)((M_PI * degrees)/180)

我用Google搜索并找到 this

I googled and found this

但我不明白如何在我的案例中将其转换为Swift ?

But I don't understand how to convert that in Swift in my case?

推荐答案

Xcode 9•Swift 4

extension BinaryInteger {
    var degreesToRadians: CGFloat { return CGFloat(Int(self)) * .pi / 180 }
}

extension FloatingPoint {
    var degreesToRadians: Self { return self * .pi / 180 }
    var radiansToDegrees: Self { return self * 180 / .pi }
}






游乐场

45.degreesToRadians         // 0.785398163397448

Int(45).degreesToRadians    // 0.785398163397448
Int8(45).degreesToRadians   // 0.785398163397448
Int16(45).degreesToRadians  // 0.785398163397448
Int32(45).degreesToRadians  // 0.785398163397448
Int64(45).degreesToRadians  // 0.785398163397448

UInt(45).degreesToRadians   // 0.785398163397448
UInt8(45).degreesToRadians  // 0.785398163397448
UInt16(45).degreesToRadians // 0.785398163397448
UInt32(45).degreesToRadians // 0.785398163397448
UInt64(45).degreesToRadians // 0.785398163397448

Double(45).degreesToRadians    // 0.7853981633974483
CGFloat(45).degreesToRadians   // 0.785398163397448
Float(45).degreesToRadians     // 0.7853981
Float80(45).degreesToRadians   // 0.785398163397448278999

这篇关于如何将度数转换为弧度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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