CIRectangleFeature+Utils.swift 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //
  2. // CIRectangleFeature+Utils.swift
  3. // WeScan
  4. //
  5. // Created by Boris Emorine on 2/8/18.
  6. // Copyright © 2018 WeTransfer. All rights reserved.
  7. //
  8. import Foundation
  9. import AVFoundation
  10. extension CIRectangleFeature {
  11. /// The perimeter of the quadrilateral.
  12. func perimeter() -> CGFloat {
  13. return (topRight.x - topLeft.x) + (topRight.y - bottomRight.y) + (bottomRight.x - bottomLeft.x) + (topLeft.y - bottomLeft.y)
  14. }
  15. /// Checks whether the quadrilateral is withing a given distance of another quadrilateral.
  16. ///
  17. /// - Parameters:
  18. /// - distance: The distance (threshold) to use for the condition to be met.
  19. /// - rectangleFeature: The other rectangle to compare this instance with.
  20. /// - Returns: True if the given rectangle is within the given distance of this rectangle instance.
  21. func isWithin(_ distance: CGFloat, ofRectangleFeature rectangleFeature: CIRectangleFeature) -> Bool {
  22. let topLeftRect = topLeft.surroundingSquare(withSize: distance)
  23. if !topLeftRect.contains(rectangleFeature.topLeft) {
  24. return false
  25. }
  26. let topRightRect = topRight.surroundingSquare(withSize: distance)
  27. if !topRightRect.contains(rectangleFeature.topRight) {
  28. return false
  29. }
  30. let bottomRightRect = bottomRight.surroundingSquare(withSize: distance)
  31. if !bottomRightRect.contains(rectangleFeature.bottomRight) {
  32. return false
  33. }
  34. let bottomLeftRect = bottomLeft.surroundingSquare(withSize: distance)
  35. if !bottomLeftRect.contains(rectangleFeature.bottomLeft) {
  36. return false
  37. }
  38. return true
  39. }
  40. override open var description: String {
  41. return "topLeft: \(topLeft), topRight: \(topRight), bottomRight: \(bottomRight), bottomLeft: \(bottomLeft)"
  42. }
  43. }