2016-09-25 2 views
1

배열의 두 개 이상의 CGPoint가 같은 좌표를 가지고 있는지 확인하는 Swift extension을 쓰고 있습니다. 이 코드를 가지고 배열의 모든 점을 검사 할 수 있습니다. 하지만 몇 가지 요소를 확인하는 방법 (전부가 아닌)은 무엇입니까? 두 개 (또는 그 이상)의 붉은 CGPoints들이 그린 사람으로 설정되어 있어야합니다 동일한 좌표가있는 경우여러 배열의 요소가 같은 좌표를 가지고 있는지 확인하십시오.

여기

import Foundation 

extension Array where Element : Equatable { 

    func equalCoordinates() -> Bool { 

     if let firstElement = first { 

      return dropFirst().contains { $0 == firstElement } 

     } 

     return true 
    } 
} 

... 확장입니다.

... 그리고 equalCoordinates() 방법 사용의 ViewController의 코드 (즉, 데이터의 크기에 따라 개선 될 수있다) 효율을 주어 전혀 우려

func drawn() { 

    let colorArray = array.map { $0.pointCoord()[0] } 

    for dot in array { 

     for cPoint in dot.pointCoord() { 

      if colorArray.equalCoordinates() { 

       let altColor = dot.alternativePointColour() 
       draw(cPoint, color: altColor) 

      } else { 

       let color = dot.pointColour() 
       draw(cPoint, color: color) 
      } 
     } 
    } 
} 

........... 

Swift.print(colorArray.equalCoordinates()) 

........... 
+0

여러 무엇을 의미합니까? 그들 범위? 그들 색인의 집합? 한번에 한? – ColGraff

+0

@ColGraff 범위 내의 둘 이상의 CGPoints입니다. 인덱스가있는 경우 가능합니다. – andy

+0

그래서 동일한 원소의 인덱스를 반환하고 싶습니까? – ColGraff

답변

2

, 이것은 어떻게 좋겠입니다 아마 그것에 대해 이동하십시오. 각 조각은 매우 간단하므로 다양한 출력물에 적용 할 수 있어야합니다 (예를 들어 IndexSet 이외의 것을 선호하는 경우).

import Foundation 
import CoreGraphics 

// We could put this on Collection rather than Array, but then we'd have to rewrite 
// IndexSet on generic indices or use [Index]. 
extension Array where Element : Equatable { 

    func uniqueElements() -> [Element] { 
     // This is O(n^2), but it's hard to beat that without adding either 
     // Hashable (for Set) or Comparable (to pre-sort) to the requirements, 
     // neither of which CGPoints have by default. 
     var uniqueElements: [Element] = [] 

     for element in self { 
      if !uniqueElements.contains(element) { 
       uniqueElements.append(element) 
      } 
     } 
     return uniqueElements 
    } 

    func indexSet(of element: Element) -> IndexSet { 
     var indices = IndexSet() 
     for (index, member) in enumerated() { 
      if element == member { 
       indices.insert(index) 
      } 
     } 
     return indices 
    } 

    func indexSetsGroupedByEquality() -> [(element: Element, indexSet: IndexSet)] { 
     return uniqueElements().map { element in (element, indexSet(of: element)) } 
    } 

    func indexSetsOfCollidingElements() -> [IndexSet] { 
     func hasCollisions(_: Element, indexSet: IndexSet) -> Bool { return indexSet.count > 1 } 

     return indexSetsGroupedByEquality() 
      .filter(hasCollisions) 
      .map { $0.indexSet } 
    } 
} 

let points = [ 
    CGPoint(x:1,y:1), 
    CGPoint(x:2,y:1), 
    CGPoint(x:1,y:1), 
    CGPoint(x:3,y:1), 
    CGPoint(x:2,y:1), 
] 

print(points.indexSetsOfCollidingElements().map(Array.init)) 

// [[0, 2], [1, 4]] 
+0

고맙습니다. Rob. 그러나 나는 오류가있다. 나는'colorArray.indexSetsOfCollidingElements(). map (Array.init)'을 타이프하고 Xcode에서 메시지를 얻었습니다.''init "'멤버에 대한 모호한 참조. 해결 방법이 있습니까? – andy

+1

그지도는 단지 IndexSet에서 배열로 도착하므로 더 쉽게 인쇄 할 수 있습니다. 당신은 일반적으로 그것을 필요로하지 않습니다. 인덱스 세트가 아닌 인덱스 배열이 필요한 경우에는 먼저 배열을 생성하도록 코드를 다시 작성하면됩니다. 다시 작성하는 방법을 잘 모르는 경우 IMO는 공부할 수있는 좋은 코드입니다. 그것이하는 일을 정확하게 수행 할 수 있어야합니다. 여기에는 아무 것도 까다로운 내용이 아닙니다. (여전히 문제가 있다면 문제가있는 방법에 대해 물어보십시오. 답변에 더 많은 의견을 추가하겠습니다.) –

+0

Xcode에서 'Type'[NSIndexSet] '프로토콜'BooleanType '을 준수하지 않습니다. . – andy

0

스위프트 2.2 버전

extension Array where Element : Equatable { 


    func uniqueElements() -> [Element] { 

     var uniqueElements: [Element] = [] 

     for element in self { 

      if !uniqueElements.contains(element) { 

       uniqueElements.append(element) 
      } 
     } 
     return uniqueElements 
    } 



    func indexSet(of element: Element) -> NSIndexSet { 

     let indices = NSIndexSet() 

     for (index, member) in enumerate() { 

      if element == member { 

       indices.insertValue(index, inPropertyWithKey: "") 
      } 
     } 
     return indices 
    } 



    func indexSetsGroupedByEquality() -> [(element: Element, indexSet: NSIndexSet)] { 

     return uniqueElements().map { element in (element, indexSet(of: element)) } 
    } 



    func indexSetsOfCollidingElements() -> [NSIndexSet] { 

     func hasCollisions(_: Element, indexSet: NSIndexSet) -> Bool { 

      return indexSet.count > 0 
     } 

     return indexSetsGroupedByEquality() 

     .filter(hasCollisions) 

     .map { $0.indexSet } 
    } 
} 
관련 문제