Skip to content

Instantly share code, notes, and snippets.

@osteslag
Last active October 9, 2019 20:17
Show Gist options
  • Save osteslag/560f942ba4ce144315ceaeea9c2bba7c to your computer and use it in GitHub Desktop.
Save osteslag/560f942ba4ce144315ceaeea9c2bba7c to your computer and use it in GitHub Desktop.
A composable type to suplement the read-only CIRectangleFeature.
//
// Quadrilateral.swift
// CPR
//
// Created by Joachim Bondo on 16/08/16.
// Copyright © 2016 Joachim Bondo. All rights reserved.
//
import UIKit
import CoreImage
protocol Quadrilateral: CustomStringConvertible {
var bounds: CGRect { get }
var topLeft: CGPoint { get }
var topRight: CGPoint { get }
var bottomRight: CGPoint { get }
var bottomLeft: CGPoint { get }
}
extension Quadrilateral {
var description: String {
return "tl {\(String(format: "%.01f", self.topLeft.x)), \(String(format: "%.01f", self.topLeft.y))}, tr {\(String(format: "%.01f", self.topRight.x)), \(String(format: "%.01f", self.topRight.y))}, br {\(String(format: "%.01f", self.bottomRight.x)), \(String(format: "%.01f", self.bottomRight.y))}, bl {\(String(format: "%.01f", self.bottomLeft.x)), \(String(format: "%.01f", self.bottomLeft.y))}"
}
}
/// Because we cannot instantiate a `CIRectangleFeature` object.
struct QuadrilateralRegion: Quadrilateral {
let topLeft: CGPoint
let topRight: CGPoint
let bottomRight: CGPoint
let bottomLeft: CGPoint
var bounds: CGRect {
let top = max(topLeft.y, topRight.y, bottomRight.y, bottomLeft.y)
let bottom = min(topLeft.y, topRight.y, bottomRight.y, bottomLeft.y)
let left = min(topLeft.x, topRight.x, bottomRight.x, bottomLeft.x)
let right = max(topLeft.x, topRight.x, bottomRight.x, bottomLeft.x)
let origin = CGPoint(x: left, y: bottom)
let size = CGSize(width: right - left, height: top - bottom)
return CGRect(origin: origin, size: size)
}
init(topLeft: CGPoint, topRight: CGPoint, bottomRight: CGPoint, bottomLeft: CGPoint) {
self.topLeft = topLeft
self.topRight = topRight
self.bottomRight = bottomRight
self.bottomLeft = bottomLeft
}
}
// MARK: - UIKit
extension Quadrilateral {
var path: UIBezierPath {
let path = UIBezierPath()
path.move(to: self.topLeft)
path.addLine(to: self.topRight)
path.addLine(to: self.bottomRight)
path.addLine(to: self.bottomLeft)
path.close()
return path
}
}
extension QuadrilateralRegion {
init(rect: CGRect) {
let top = rect.maxY
let bottom = rect.minY
let left = rect.minX
let right = rect.maxX
self.topLeft = CGPoint(x: left, y: top)
self.topRight = CGPoint(x: right, y: top)
self.bottomRight = CGPoint(x: right, y: bottom)
self.bottomLeft = CGPoint(x: left, y: bottom)
}
}
// MARK: - Core Image
extension CIRectangleFeature: Quadrilateral {}
extension QuadrilateralRegion {
init(rectangleFeature feature: CIRectangleFeature) {
self.topLeft = feature.topLeft
self.topRight = feature.topRight
self.bottomRight = feature.bottomRight
self.bottomLeft = feature.bottomLeft
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment