Symmetry in using structures for conversion. What is best practice? - swift

I've been playing with Swift and I encoded an obvious conversion structure:
struct MutableAngle {
var degrees : CGFloat
var radians : CGFloat {
return degrees * CGFloat(M_PI) / 180.0
}
init(inRadians : CGFloat) {
degrees = inRadians * 180.0 / CGFloat(M_PI)
}
init(inDegrees : CGFloat) {
degrees = inDegrees
}
}
Now this is fine but inelegant since it doesn't treat degrees and radians symmetrically although it does give mutability. This is really a structure which should be called Degrees and which can provide radians. For instance, I can write:
var angle : MutableAngle
angle.degrees = 45.0
but not
var angle : MutableAngle
angle.radians = 0.75
Here's a final version:
struct Angle {
let degrees : CGFloat
let radians : CGFloat
init(inRadians : CGFloat ) {
radians = inRadians
degrees = radians * CGFloat (180 / M_PI)
}
init(inDegrees : Float ) {
degrees = inDegrees
radians = degrees * CGFloat (M_PI / 180)
}
}
Use as follows:
var alpha = Angle(inDegrees: 45)
alpha.degrees // returns 45
alpha.radians // returns 0.7853982
// alpha.radians = 0.9 ... is now illegal with let constants
// must use constructor ... provided alpha was defined using 'var'
// i.e. the struct itself is mutable
alpha = Angle(inRadians: 0.9)
alpha.radians // returns 0.7853982
alpha.degrees // returns 45
Switching from var to let makes it mutable/immutable depending on how alpha is defined and I'm now obliged to use the constructors which is good. So its symmetric. It also has the merit that a calculation is not required every time I want to use the radians.

Two things here:
In Swift you don't need a separate mutable type for value types - that's handled by whoever is instantiating the type by using let or var.
Your radians computed property only has a getter - you can do what you want with both a setter and a getter.
My implementation:
struct Angle {
var degrees : CGFloat = 0
var radians : CGFloat {
get {
return degrees * CGFloat(M_PI) / 180.0
}
set {
degrees = newValue * 180.0 / CGFloat(M_PI)
}
}
init(inRadians : CGFloat) {
radians = inRadians
}
init(inDegrees : CGFloat) {
degrees = inDegrees
}
}
Useage:
// immutable
let angle = Angle(inDegrees: 180)
println(angle.radians)
// next line gives an error: can't assign to an immutable instance
angle.radians = angle.radians * 2
// mutable copy
var mutableAngle = angle
mutableAngle.degrees = 10
println(mutableAngle.radians)
// 0.1745...
mutableAngle.radians = CGFloat(M_PI)
println(mutableAngle.degrees)
// 180.0

A possible solution is to use enum with associated value:
enum MutableAngle {
case Radian(CGFloat)
case Degree(CGFloat)
init(radians:CGFloat) {
self = .Radian(radians)
}
init(degrees:CGFloat) {
self = .Degree(degrees)
}
var radians:CGFloat {
get {
switch self {
case .Radian(let val): return val
case .Degree(let val): return val * CGFloat(M_PI) / 180.0
}
}
set {
self = .Radian(newValue)
}
}
var degrees:CGFloat {
get {
switch self {
case .Degree(let val): return val
case .Radian(let val): return val * 180.0 / CGFloat(M_PI)
}
}
set {
self = .Degree(newValue)
}
}
}
var angle = MutableAngle(radians: 1)
angle.degrees // -> 57.2957795130823
angle.degrees = 180
angle.radians // -> 3.14159265358979

Related

Is a variable or a function?

I see some code on github.
private static var allMemes:[MemeModel]{
return getMemeStorage().memes
}
Is this a variable or a function or other?
Thanks
Computed Properties
In addition to stored properties, classes, structures, and
enumerations can define computed properties, which do not actually
store a value. Instead, they provide a getter and an optional setter
to retrieve and set other properties and values indirectly.
struct Point {
var x = 0.0, y = 0.0
}
struct Size {
var width = 0.0, height = 0.0
}
struct Rect {
var origin = Point()
var size = Size()
var center: Point {
get {
let centerX = origin.x + (size.width / 2)
let centerY = origin.y + (size.height / 2)
return Point(x: centerX, y: centerY)
}
set(newCenter) {
origin.x = newCenter.x - (size.width / 2)
origin.y = newCenter.y - (size.height / 2)
}
}
}
Ref :
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Properties.html

SpriteKit action like "Skew" or "Distort"

Is it possible to create an SKAction for SKSpriteNode in SpriteKit that generates the same effect as "Photoshop" with the Edit->Transform->Distort option?
Example:
I solve with this implementation:
Swift 5
extension SKSpriteNode {
func addSkew(value: CGFloat = -1){
var effectNode = SKEffectNode()
effectNode.shouldRasterize = true
effectNode.shouldEnableEffects = true
effectNode.addChild(SKSpriteNode(texture: texture))
effectNode.zPosition = 1
let transform = CGAffineTransform(a: 1 , b: 0,
c: value, d: 1,
tx: 0 , ty: 0)
let transformFilter = CIFilter(name: "CIAffineTransform")!
transformFilter.setValue(transform, forKey: "inputTransform")
effectNode.filter = transformFilter
addChild(effectNode)
texture = nil
}
}
You can create a skew using a 1x1 warp mesh. This is supported in iOS10.0+.
This extension receives the skew angle in degrees, and distorts around the anchor point of the given sprite.
Swift 4.2
extension SKWarpGeometryGrid {
public static var skewPosGridZero:[float2] {
get {
return [float2(0.0, 0.0), float2(1.0, 0.0),
float2(0.0, 1.0), float2(1.0, 1.0)]
}
}
public static func skewXPosGrid(_ skewX: CGFloat, node:SKSpriteNode? = nil) -> [float2] {
let anchorY:Float = Float(node?.anchorPoint.y ?? 0.5)
var skewPosGrid = skewPosGridZero
let offsetX = Float(tan(skewX.degToRad()) * (node == nil ? 1.0 : (node!.size.height/node!.size.width)) )
skewPosGrid[2][0] += offsetX * (1.0 - anchorY)
skewPosGrid[3][0] += offsetX * (1.0 - anchorY)
skewPosGrid[0][0] -= offsetX * anchorY
skewPosGrid[1][0] -= offsetX * anchorY
return skewPosGrid
}
public static func skewYPosGrid(_ skewY: CGFloat, node:SKSpriteNode? = nil) -> [float2] {
let anchorX:Float = Float(node?.anchorPoint.x ?? 0.5)
var skewPosGrid = skewPosGridZero
let offsetY = Float(tan(skewY.degToRad()) * (node == nil ? 1.0 : (node!.size.width/node!.size.height)) )
skewPosGrid[1][1] += offsetY * (1.0 - anchorX)
skewPosGrid[3][1] += offsetY * (1.0 - anchorX)
skewPosGrid[0][1] -= offsetY * anchorX
skewPosGrid[2][1] -= offsetY * anchorX
return skewPosGrid
}
public static func skewX(_ angle: CGFloat, node:SKSpriteNode? = nil) -> SKWarpGeometryGrid {
return SKWarpGeometryGrid(columns: 1, rows: 1, sourcePositions: skewPosGridZero, destinationPositions: skewXPosGrid(angle, node:node))
}
public static func skewY(_ angle: CGFloat, node:SKSpriteNode? = nil) -> SKWarpGeometryGrid {
return SKWarpGeometryGrid(columns: 1, rows: 1, sourcePositions: skewPosGridZero, destinationPositions: skewYPosGrid(angle, node:node))
}
public static func skewZero() -> SKWarpGeometryGrid {
return SKWarpGeometryGrid(columns: 1, rows: 1)
}
}
Example animation:
let spriteNode = SKSpriteNode(imageNamed: "tex")
spriteNode.anchorPoint = CGPoint(x:0.25, y:1.0)
let skewA = SKWarpGeometryGrid.skewX(-45.0, node: spriteNode)
let skewB = SKWarpGeometryGrid.skewX(45.0, node: spriteNode)
spriteNode.warpGeometry = skewB
if let skewActionA = SKAction.warp(to: skewA, duration: 3.0),
let skewActionB = SKAction.warp(to: skewB, duration: 3.0){
// Individual easing
skewActionA.timingMode = .easeInEaseOut
skewActionB.timingMode = .easeInEaseOut
spriteNode.run(SKAction.repeatForever(SKAction.sequence([skewActionA,skewActionB])))
}
The list of available SKAction's is here: https://developer.apple.com/reference/spritekit/skaction
There is none to do exactly what you describe. Instead, you can export multiple sprite images from a photo editing tool like Photoshop, and use an animation action like class func animate(with: [SKTexture], timePerFrame: TimeInterval).
This is a little more work, but should achieve the desired effect.

Swift - keeping two properties in sync

In Swift, I have two related properties of a structure that I want to keep in sync.
I'm currently doing this with property observers but I've had to add an extra flag to prevent them playing an infinite game of ping-pong with each other.
Is there a more elegant and/or transparent way to achieve this?
A simplified example:
import Foundation
struct Angle {
var blockPropertyObservers = false
var degrees: Double {
willSet(degrees) {
print("will set degrees to \(degrees)")
if !blockPropertyObservers {
blockPropertyObservers = true
radians = (degrees / 360) * 2 * M_PI
} else {
blockPropertyObservers = false
}
}
}
var radians: Double {
willSet(radians) {
print("will set radians to \(radians)")
if !blockPropertyObservers {
blockPropertyObservers = true
degrees = (radians / (2 * M_PI)) * 360
} else {
blockPropertyObservers = false
}
}
}
init(inDegrees degrees: Double) {
self.degrees = degrees
self.radians = (degrees / 360) * 2 * M_PI
}
init(inRadians radians: Double) {
self.radians = radians
self.degrees = (radians / (2 * M_PI)) * 360
}
}
Ideally, I'd also like to find a way to avoid having to replicate the code for the conversions in the init() routines...
You could use a computed property for one of those two, let's say for degrees. This will reduce the boiler plate code, without losing functionality for your struct.
struct Angle {
var degrees: Double {
get { return radians / (2 * M_PI) * 360 }
set { radians = (newValue / 360) * 2 * M_PI}
}
var radians: Double = 0.0
init(inDegrees degrees: Double) {
self.degrees = degrees
}
init(inRadians radians: Double) {
self.radians = radians
}
}

Calculate bearing between two locations

I want to calculate bearing between two location in Swift. I tried some code but it do not work. I searched about this problem but I didn't find any result about this.
func calculat(userlocation:CLLocation){
let latuserlocation: () = DegreesToRadians(userlocation.coordinate.latitude)
let lonuserlocatioc: () = DegreesToRadians(userlocation.coordinate.longitude)
latitude = NSString (string: places[activePlace]["lat"]!).doubleValue
longitude = NSString (string: places[activePlace]["lon"]!).doubleValue
let targetedPointLatitude: () = DegreesToRadians(latitude)
let targetedPointlongitude: () = DegreesToRadians(longitude)
let dlon = lonuserlocatioc - targetedPointlongitude
let y = sin(dLon) * cos(targetedPointLatitude);
let x = cos(latuserlocation) * sin(targetedPointLatitude) - sin(latuserlocation) * cos(targetedPointLatitude) * cos(dLon);
let radiansBearing = atan2(y, x);
return RadiansToDegrees(radiansBearing)
The error on let dlon = lonuserlocatioc - targetedPointlongitude is:
(cannot invoke '-' with an argument list of type '((), ())')
Compared to CLLocation Category for Calculating Bearing w/ Haversine function, your dlon is different. That answer has
let dlon = targetedPointlongitude - lonuserlocatioc
I don't know if that's your problem but it looks odd.
Swift function like this;
func radians(n: Double) -> Double{
return n * (M_PI / 180);
}
func degrees(n: Double) -> Double{
return n * (180 / M_PI);
}
func logC(val:Double,forBase base:Double) -> Double {
return log(val)/log(base);
}
func getBearing(startLat: Double,startLon:Double,endLat: Double,endLon: Double) -> Double{
var s_LAT: Double , s_LON: Double, e_LAT: Double, e_LON: Double, d_LONG: Double, d_PHI: Double;
s_LAT = startLat;
s_LON = startLon;
e_LAT = endLat;
e_LON = endLon;
d_LONG = e_LON - s_LON;
d_PHI = logC(tan(e_LAT/2.0+M_PI/4.0)/tan(s_LAT/2.0+M_PI/4.0),forBase :M_E);
if (abs(d_LONG)>M_PI){
if(d_LONG>0.0){
d_LONG = -(2.0 * M_PI - d_LONG);
}else{
d_LONG = (2.0 * M_PI - d_LONG);
}
}
return degrees(atan2(d_LONG, d_PHI)+360.0)%360.0;
}

Swift Rope Verlets

I'm trying to implement a verlet rope in swift. I'm running into the problem where when trying to fix the position of the point masses from constraints, they very rapidly grow apart and then the coordinates become NaNs. Any idea what could be wrong in my code?
import Foundation
private let DEF_POINT_COUNT = 10
private let CONST_ITERATIONS = 5
#objc class PointMass: DebugPrintable {
var point: NSPoint
private var oldPoint: NSPoint
var x: Double {
get { return Double(point.x) }
}
var y: Double {
get { return Double(point.y) }
}
var debugDescription: String {
get { return "\(point)" }
}
init(_ point: NSPoint) {
self.point = point
self.oldPoint = point
}
func updatePosition() {
let dx = point.x - oldPoint.x
let dy = (point.y - oldPoint.y)
oldPoint = point
point = NSPoint(x: point.x + dx, y: point.y + dy)
}
func updatePosition(point: NSPoint) {
let dx = point.x - self.point.x
let dy = point.y - self.point.y
self.oldPoint = NSPoint(x: oldPoint.x + dx, y: oldPoint.y + dy)
self.point = point
}
}
struct Constraint {
var p1: PointMass
var p2: PointMass
var len: Double
func fixPoints() {
let dx = p2.x - p1.x
let dy = p2.y - p1.y
let dist = sqrt(dx*dx + dy*dy)
let diff = (dist - len)/len
p2.updatePosition(NSPoint(x: p2.x - diff*dx*0.5, y: p2.y - diff*dy*0.5))
p1.updatePosition(NSPoint(x: p1.x + diff*dx*0.5, y: p1.y + diff*dy*0.5))
}
}
#objc class Rope: NSObject {
let points: [PointMass]
let constraints: [Constraint]
init(anchor: NSPoint, end: NSPoint, length: Double, count: Int = DEF_POINT_COUNT) {
let anchorPoint = PointMass(anchor)
let endPoint = PointMass(end)
let dx = (anchorPoint.x - endPoint.x)/Double(count)
let dy = (anchorPoint.y - endPoint.y)/Double(count)
let constraintLength = length/Double(count)
var points = [endPoint]
var constraints: [Constraint] = []
for i in 1...count {
let prevPoint = points[i-1]
let newPoint = PointMass(NSPoint(x: prevPoint.x + dx, y: prevPoint.y + dy))
let constraint = Constraint(p1: prevPoint, p2: newPoint, len: constraintLength)
points.append(newPoint)
constraints.append(constraint)
}
self.points = points
self.constraints = constraints
}
func update(anchor: NSPoint, endPoint: NSPoint) {
points.first?.updatePosition(endPoint)
points.last?.updatePosition(anchor)
for point in points {
point.updatePosition()
}
for i in 0...CONST_ITERATIONS {
for constraint in constraints {
constraint.fixPoints()
}
}
}
}
Found it. The problem was in the fixPoints() method, as I suspected.
The line
let diff = (dist - len)/len
should be instead
let diff = (len - dist)/dist