-
Notifications
You must be signed in to change notification settings - Fork 26
/
Border.swift
93 lines (78 loc) · 1.94 KB
/
Border.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//
// Border.swift
// SwiftColorArt
//
// Created by Jan Gregor Triebel on 11.01.15.
// Copyright (c) 2015 Jan Gregor Triebel. All rights reserved.
//
import Foundation
import UIKit
import CoreGraphics
/// Border
///
/// A struct to keep trac of dimensions and border width to check a point's location
///
public struct Border {
public let rect: CGRect
public let width: CGFloat
let top: Bool
let right: Bool
let bottom: Bool
let left: Bool
public init (rect: CGRect, width: CGFloat, top: Bool, right: Bool, bottom: Bool, left: Bool ){
self.rect = rect
self.width = width
self.top = top
self.right = right
self.bottom = bottom
self.left = left
}
public func isPointInBorder(_ point: CGPoint) -> Bool {
if top && point.y <= width {
return true
} else if right && point.x >= rect.maxX - width {
return true
} else if bottom && point.y >= rect.maxY - width {
return true
} else if left && point.x <= width {
return true
}
return false
}
public func createBorderSet() -> [CGPoint] {
var borderSet: [CGPoint] = Array()
if top {
for x in 0...Int(rect.maxX) {
for y in 0...Int(width) {
let point = CGPoint(x: x, y: y)
borderSet.append(point)
}
}
}
if bottom {
for x in 0...Int(rect.maxX) {
for y in Int(rect.maxY - width)...Int(rect.maxY) {
let point = CGPoint(x: x, y: y)
borderSet.append(point)
}
}
}
if right {
for x in Int(rect.maxX - width)...Int(rect.maxX) {
for y in 0...Int(rect.maxY) {
let point = CGPoint(x: x, y: y)
borderSet.append(point)
}
}
}
if left {
for x in 0...Int(width) {
for y in 0...Int(rect.maxY) {
let point = CGPoint(x: x, y: y)
borderSet.append(point)
}
}
}
return borderSet
}
}