-
-
Notifications
You must be signed in to change notification settings - Fork 6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replaced ChartUtils.Math
in favour of an extension on FloatingPoint
#2993
Changes from 5 commits
ad296bc
2d297f4
2136715
82f54e4
569bf6a
cc742a5
846494a
3aba32d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,9 +16,26 @@ import CoreGraphics | |
import UIKit | ||
#endif | ||
|
||
extension FloatingPoint { | ||
var deg2rad: Self { | ||
return self * .pi / 180 | ||
} | ||
|
||
var rad2deg: Self { | ||
return self * 180 / .pi | ||
} | ||
|
||
/// - returns: An angle between 0.0 < 360.0 (not less than zero, less than 360) | ||
/// NOTE: Value must be in degrees | ||
var normalizedAngle: Self { | ||
let angle = truncatingRemainder(dividingBy: 360) | ||
return (sign == .minus) ? angle + 360 : angle | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the order is flipped. I need to refresh my mind first to see if they are equivalent. Or you already did that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They’re equivalent. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. made some stupid equations :P |
||
} | ||
} | ||
|
||
extension CGSize { | ||
func rotatedBy(degrees: CGFloat) -> CGSize { | ||
let radians = ChartUtils.Math.FDEG2RAD * degrees | ||
let radians = degrees.deg2rad | ||
return rotatedBy(radians: radians) | ||
} | ||
|
||
|
@@ -33,15 +50,7 @@ extension CGSize { | |
open class ChartUtils | ||
{ | ||
fileprivate static var _defaultValueFormatter: IValueFormatter = ChartUtils.generateDefaultValueFormatter() | ||
|
||
internal struct Math | ||
{ | ||
internal static let FDEG2RAD = CGFloat(Double.pi / 180.0) | ||
internal static let FRAD2DEG = CGFloat(180.0 / Double.pi) | ||
internal static let DEG2RAD = Double.pi / 180.0 | ||
internal static let RAD2DEG = 180.0 / Double.pi | ||
} | ||
|
||
|
||
internal class func roundToNextSignificant(number: Double) -> Double | ||
{ | ||
if number.isInfinite || number.isNaN || number == 0 | ||
|
@@ -89,8 +98,8 @@ open class ChartUtils | |
internal class func getPosition(center: CGPoint, dist: CGFloat, angle: CGFloat) -> CGPoint | ||
{ | ||
return CGPoint( | ||
x: center.x + dist * cos(angle * Math.FDEG2RAD), | ||
y: center.y + dist * sin(angle * Math.FDEG2RAD) | ||
x: center.x + dist * cos(angle.deg2rad), | ||
y: center.y + dist * sin(angle.deg2rad) | ||
) | ||
} | ||
|
||
|
@@ -262,20 +271,7 @@ open class ChartUtils | |
let rect = text.boundingRect(with: constrainedToSize, options: .usesLineFragmentOrigin, attributes: attributes, context: nil) | ||
drawMultilineText(context: context, text: text, knownTextSize: rect.size, point: point, attributes: attributes, constrainedToSize: constrainedToSize, anchor: anchor, angleRadians: angleRadians) | ||
} | ||
|
||
/// - returns: An angle between 0.0 < 360.0 (not less than zero, less than 360) | ||
internal class func normalizedAngleFromAngle(_ angle: CGFloat) -> CGFloat | ||
{ | ||
var angle = angle | ||
|
||
while (angle < 0.0) | ||
{ | ||
angle += 360.0 | ||
} | ||
|
||
return angle.truncatingRemainder(dividingBy: 360.0) | ||
} | ||
|
||
|
||
fileprivate class func generateDefaultValueFormatter() -> IValueFormatter | ||
{ | ||
let formatter = DefaultValueFormatter(decimals: 1) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally it's fine. But do you think we should follow the Camel-Case naming? like deg2Rad
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Up to you. I tried
deg2Rad
anddeg2RAD
and they both look silly to me. I also consideredinRadians
andinDegrees
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. How about we keep old
FDEG2RAD
to keep consitent?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll drop the F since it's implied as it's an extension on
FloatingPoint