-
Notifications
You must be signed in to change notification settings - Fork 0
/
Point.swift
113 lines (87 loc) · 2.99 KB
/
Point.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
// Point.swift
// Freehand
//
// Created by John Knowles on 9/9/22.
//
import Foundation
extension PerfectFreehand {
struct Point {
// horizontal coordinate
let x: Double
// vertical coordinate
let y: Double
// pressure component
let p: Double
init(x: Double, y: Double, p: Double = 0.5) {
self.x = x
self.y = y
self.p = p
}
}
}
extension PerfectFreehand.Point {
func neg() -> PerfectFreehand.Point {
PerfectFreehand.Point (x: -self.x, y: -self.y, p: self.p)
}
func add(_ this: PerfectFreehand.Point ) -> PerfectFreehand.Point {
PerfectFreehand.Point (x: self.x + this.x, y: self.y + this.y, p: this.p)
}
func sub(_ this: PerfectFreehand.Point ) -> PerfectFreehand.Point {
PerfectFreehand.Point (x: self.x - this.x, y: self.y - this.y, p: this.p)
}
func mulV(_ this: PerfectFreehand.Point ) -> PerfectFreehand.Point {
PerfectFreehand.Point (x: self.x * this.x, y: self.y * this.y, p: this.p)
}
func mul(_ this: Double) -> PerfectFreehand.Point {
PerfectFreehand.Point (x: self.x * this, y: self.y * this, p: self.p)
}
func divV(_ this: PerfectFreehand.Point ) -> PerfectFreehand.Point {
PerfectFreehand.Point (x: self.x / this.x, y: self.y / this.y, p: this.p)
}
func div(_ this: Double) -> PerfectFreehand.Point {
PerfectFreehand.Point (x: self.x / this, y: self.y / this, p: self.p)
}
func per() -> PerfectFreehand.Point {
PerfectFreehand.Point (x: self.y, y: -self.x, p: self.p)
}
func uni() -> PerfectFreehand.Point {
self.div(self.len())
}
func lrp(_ this: PerfectFreehand.Point , t: Double) -> PerfectFreehand.Point {
self.add(this.sub(self).mul(t))
}
func med(_ this: PerfectFreehand.Point ) -> PerfectFreehand.Point {
self.lrp(this, t: 0.5)
}
func len() -> Double {
sqrt(self.len2())
}
func len2() -> Double {
self.x * self.x + self.y * self.y
}
func isEqual(_ this: PerfectFreehand.Point ) -> Bool {
self.x == this.x && self.y == this.y
}
func dist2(_ this: PerfectFreehand.Point ) -> Double {
self.sub(this).len2()
}
func dist(_ this: PerfectFreehand.Point ) -> Double {
self.sub(this).len()
}
func dpr(_ this: PerfectFreehand.Point ) -> Double {
self.x * this.x + self.y * this.y
}
func prj(_ this: PerfectFreehand.Point , d: Double) -> PerfectFreehand.Point {
self.add(this.mul(d))
}
func rotAround(_ this: PerfectFreehand.Point , r: Double) -> PerfectFreehand.Point {
let s = sin(r)
let c = cos(r)
let px = self.x - this.x
let py = self.y - this.y
let nx = px * c - py * s
let ny = py * s + py * c
return PerfectFreehand.Point (x: nx + this.x, y: ny + this.y, p: this.p)
}
}