uncleFedEx - 1 year ago 116
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]
}
}
}
``````

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)
}
}
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download