uncleFedEx uncleFedEx - 3 months ago 11
Swift Question

How to check if CGPoints in array have the identical coordinates?

Is it possible to check if CGPoints in array have the identical x/y coordinates?

Here is the code.

enum Three {
case pointOne(point: CGPoint)
case pointTwo(point: CGPoint)
case pointThree(point: CGPoint)

func coordinatesXY() -> [CGPoint] {
switch self {
case .pointOne(let point): return [point]
case .pointTwo(let point): return [point]
case .pointThree(let point): return [point]
}
}
}

Answer

Simply ==

enum Three {
 case pointOne(point: CGPoint)
 case pointTwo(point: CGPoint)
 case pointThree(point: CGPoint)

   func coordinatesXY() -> CGPoint {
      switch self {
       case .pointOne(let point): return point
       case .pointTwo(let point): return point
       case .pointThree(let point): return point
    }
  }
}


let point1 = CGPoint(x: 100, y: 100)
let point2 = CGPoint(x: 100, y: 100)

if point1 == point2{
   print("yes")
  }

 Three.pointOne(point: point1).coordinatesXY() //Gives you a CGPoint
 Three.pointTwo(point: point2)

func checkEquality(pointA : CGPoint, pointB : CGPoint){

       if Three.pointOne(point: pointA).coordinatesXY() == Three.pointTwo(point: pointB).coordinatesXY(){
         print("yes") //Prints yes
  }


}

//Prototype of comparing two coordinates

var arrOne = [Three]()
var arrTwo = [Three]()

 arrOne.append(Three.pointOne(point: point1))
 arrOne.append(Three.pointOne(point: point1))
 arrOne.append(Three.pointOne(point: point1))
 arrOne.append(Three.pointOne(point: point1))
 arrOne.append(Three.pointOne(point: point1))
 arrOne.append(Three.pointOne(point: point1))

 arrTwo.append(Three.pointTwo(point: point2))
 arrTwo.append(Three.pointTwo(point: point2))
 arrTwo.append(Three.pointTwo(point: point2))
 arrTwo.append(Three.pointTwo(point: point2))
 arrTwo.append(Three.pointTwo(point: point2))
 arrTwo.append(Three.pointTwo(point: point2))



for eachA in arrOne{

  let arrayOneElement = eachA.coordinatesXY()
    for eachB in arrTwo{
       let arrayTwoElement = eachB.coordinatesXY()
        checkEquality(arrayOneElement, pointB: arrayTwoElement)
   }
}