Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert from degrees to radians?

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) 

I googled and found this

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

like image 374
Dharmesh Kheni Avatar asked Mar 21 '15 05:03

Dharmesh Kheni


People also ask

How do you convert from degrees to radians?

How do we convert degrees to radians? To convert the value of the angle in degree, to its equivalent radians, we need to multiply the given value with π/180. For example, the value of 180 degrees is π in radians.

How do you convert degrees to radians in terms of pi?

Answer: 'Radians = Degrees × π/180' is the formula to convert degrees to radians in terms of pi.

What is the formula used to convert between degrees and radians?

☛Important Notes on Radians to Degrees 1° equals 0.017453 radians and 1 rad equals 57.2958°. To convert an angle from radians to degrees, we multiply it by 180°/π. To convert an angle from degrees to radians, we multiply it by π/180°.


2 Answers

Xcode 11 • Swift 5.1 or later

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:

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 
like image 121
Leo Dabus Avatar answered Sep 28 '22 02:09

Leo Dabus


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!

let result = Measurement(value: 45, unit: UnitAngle.degrees)     .converted(to: .radians).value 
like image 43
matt Avatar answered Sep 28 '22 01:09

matt