如何检查数组中的CGPoints是否具有相同的坐标?

时间:2016-09-03 20:44:17

标签: arrays swift cgpoint

是否可以检查数组中的CGPoints是否具有相同的x / y坐标?

这是代码。

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]
       }
   }
}

1 个答案:

答案 0 :(得分:0)

只需==

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)
   }
}