-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.swift
More file actions
58 lines (44 loc) · 1.61 KB
/
Solution.swift
File metadata and controls
58 lines (44 loc) · 1.61 KB
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
struct Rectangle: CustomStringConvertible {
// coordinates of bottom left corner
let leftX: Int
let bottomY: Int
// dimensions
let width: Int
let height: Int
init(leftX: Int, bottomY: Int, width: Int, height: Int) {
self.leftX = leftX
self.bottomY = bottomY
self.width = width
self.height = height
}
init() {
self.init(leftX: 0, bottomY: 0, width: 0, height: 0)
}
var description: String {
return "\(leftX), \(bottomY), \(width), \(height)"
}
struct Point {
var a: Int
var b: Int
}
func intersection(_ other: Rectangle) -> Rectangle? {
// horizontal
guard let horizontal = lineIntersection(leftX, leftX + width, other.leftX, other.leftX + other.width) else { return nil }
//vertical
guard let vertical = lineIntersection(bottomY, bottomY + height, other.bottomY, other.bottomY + other.height) else { return nil }
return Rectangle(leftX: horizontal.a, bottomY: vertical.a, width: horizontal.b - horizontal.a, height: vertical.b - vertical.a)
}
private func lineIntersection(_ a0: Int, _ a1: Int, _ b0: Int, _ b1: Int) -> Point? {
let maxStart = max(a0, b0)
let minEnd = min(a1, b1)
if maxStart >= minEnd {
return nil
}
return Point(a: maxStart, b: minEnd)
}
}
let a = Rectangle(leftX: 0, bottomY: 0, width: 10, height: 10)
let b = Rectangle(leftX: 5, bottomY: 5, width: 2, height: 2)
if let c = a.intersection(b) {
print(c)
}