-
Notifications
You must be signed in to change notification settings - Fork 2
/
Utilities.swift
executable file
·70 lines (56 loc) · 1.66 KB
/
Utilities.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
//
// Utilities.swift
// DelaunayTriangulationSwift
//
// Created by Alex Littlejohn on 2016/01/08.
// Copyright © 2016 zero. All rights reserved.
//
// Improved and Extended by Volodymyr Boichentsov on 14/11/2017
#if os(iOS)
import UIKit
public typealias OSColor = UIColor
public typealias OSViewController = UIViewController
#elseif os(OSX)
import Cocoa
public typealias OSColor = NSColor
public typealias OSViewController = NSViewController
#endif
import Delaunay
extension Point {
public func pointValue() -> CGPoint {
return CGPoint(x: x, y: y)
}
}
extension Triangle {
func v1() -> CGPoint {
return point1.pointValue()
}
func v2() -> CGPoint {
return point2.pointValue()
}
func v3() -> CGPoint {
return point3.pointValue()
}
func toPath() -> CGPath {
let path = CGMutablePath()
let point1 = self.v1()
let point2 = self.v2()
let point3 = self.v3()
path.move(to: point1)
path.addLine(to: point2)
path.addLine(to: point3)
path.addLine(to: point1)
path.closeSubpath()
return path
}
}
extension OSColor {
static func randomColor() -> OSColor {
let range:Range<CGFloat> = Range<CGFloat>(uncheckedBounds: (lower: 0.0, upper: 1.0))
let hue = CGFloat.random(in: range) // 0.0 to 1.0
let saturation: CGFloat = 0.5 // 0.5 to 1.0, away from white
let brightness: CGFloat = 1.0 // 0.5 to 1.0, away from black
let color = OSColor(hue: hue, saturation: saturation, brightness: brightness, alpha: 1)
return color
}
}