Search code examples
iosswiftcore-graphics

Comparing two CGPoints for equality: returning not equal for two objects that output same point?


According to this question, using == and != should let you check for equality between two CGPoint objects.

However, the code below fails to consider two CGPoint objects as equal even though they output the same value.

What is the right way to check equality among CGPoint objects?

Code:

    let boardTilePos = boardLayer.convert(boardTile.position, from: boardTile.parent!)
    let shapeTilePos = boardLayer.convert(tile.position, from: tile.parent!)   
    print("board tile pos: \(boardTilePos). active tile pos: \(shapeTilePos). true/false: \(shapeTilePos == boardTilePos)")

Output:

board tile pos: (175.0, 70.0). active tile pos: (175.0, 70.0). true/false: false

Solution

  • Unfortunately, what you see in the console is not what your real value is.

    import UIKit
    
    var x = CGPoint(x:175.0,y:70.0)
    var y = CGPoint(x:175.0,y:70.00000000000001)
    
    print("\(x.equalTo(y)), \(x == y),\(x),\(y)")
    

    The problem is, the console only allows for 10-16 but in reality your CGFloat can go even lower than that because on 64bit architecture, CGFloat is Double.

    This means you have to cast your CGPoint values to a Float if you want to get equality that will appear on the console, so you need to do something like:

    if Float(boxA.x) == Float(boxB.x) && Float(boxA.y) == Float(boxB.y)
    {
      //We have equality
    }
    

    Now I like to take it one step further.

    In most cases, we are using CGPoint to determine points on the scene. Rarely do we ever want to be dealing with 1/2 points, they make our lives just confusing.

    So instead of Float, I like to cast to Int. This will guarantee if two points are lying on the same CGPoint in scene space

    if Int(boxA.x) == Int(boxB.x) && Int(boxA.y) == Int(boxB.y)
    {
      //We have equality
    }