-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathUtilities.swift
executable file
·79 lines (65 loc) · 2.05 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
71
72
73
74
75
76
77
78
79
//
// Utilities.swift
// DelaunayTriangulationSwift
//
// Created by Alex Littlejohn on 2016/01/08.
// Copyright © 2016 zero. All rights reserved.
//
#if os(iOS)
import UIKit
public typealias OSColor = UIColor
public typealias OSViewController = UIViewController
public typealias OSFont = UIFont
public typealias OSAffineTransform = CGAffineTransform
extension CGAffineTransform {
public init(translationByX x: CGFloat, byY y: CGFloat) {
self.init(translationX: x, y: y)
}
public init(scaleByX x: CGFloat, byY y: CGFloat) {
self.init(scaleX: x, y: y)
}
}
#elseif os(OSX)
import Cocoa
public typealias OSColor = NSColor
public typealias OSViewController = NSViewController
public typealias OSFont = NSFont
public typealias OSAffineTransform = AffineTransform
#endif
import Delaunay
extension Triangle {
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 Double {
static func random() -> Double {
return Double(arc4random()) / 0xFFFFffff
}
static func random(_ min: Double, _ max: Double) -> Double {
return Double.random() * (max - min) + min
}
}
extension CGFloat {
static func random(_ min: CGFloat, _ max: CGFloat) -> CGFloat {
return CGFloat(Double.random(Double(min), Double(max)))
}
}
extension OSColor {
static func randomColor() -> OSColor {
let hue = CGFloat( Double.random() ) // 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
}
}