How can I convert from degrees to radians?

2019-01-03 02:46发布

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?

8条回答
我想做一个坏孩纸
2楼-- · 2019-01-03 02:47

Apple provides these GLKit functions for conversion:

func GLKMathDegreesToRadians(_ degrees: Float) -> Float
func GLKMathRadiansToDegrees(_ radians: Float) -> Float
查看更多
神经病院院长
3楼-- · 2019-01-03 02:49

Also, to convert fron radians to degrees (if anyone stumbles upon this on google):

var degrees = radians * (180.0 / M_PI)
查看更多
狗以群分
4楼-- · 2019-01-03 03:01
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.

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.

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
查看更多
倾城 Initia
5楼-- · 2019-01-03 03:06

swift 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
} 
查看更多
Animai°情兽
6楼-- · 2019-01-03 03:07

You're no longer limited to ASCII characters when creating variable names, so how about this using π (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
查看更多
Root(大扎)
7楼-- · 2019-01-03 03:11

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
查看更多
登录 后发表回答