-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utility.swift
133 lines (89 loc) · 3.19 KB
/
Utility.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//
// Utility.swift
// 2024challenge
//
// Created by dasoya on 2/11/24.
//
import Foundation
import SwiftUI
struct Utility{
func clampPosition(_ position: CGPoint) -> CGPoint {
var x = position.x
var y = position.y
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height
x = max(0, min(x, screenWidth))
y = max(0, min(y, screenHeight))
return CGPoint(x: x, y: y)
}
func findSymmetricPoint(from pointA: CGPoint, to pointB: CGPoint) -> CGPoint {
_ = CGPoint(x: (pointA.x + pointB.x) / 2, y: (pointA.y + pointB.y) / 2)
let vectorAB = CGPoint(x: pointB.x - pointA.x, y: pointB.y - pointA.y)
let symmetricPoint = CGPoint(x: pointA.x + 2 * vectorAB.x, y: pointA.y + 2 * vectorAB.y)
return symmetricPoint
}
func getPointOnBezierCurve(points : [CGPoint], t: CGFloat) -> CGPoint {
var points = points
while points.count > 1 {
var newPoints = [CGPoint]()
for i in 0..<points.count - 1 {
let point1 = points[i]
let point2 = points[i + 1]
let newX = (1 - t) * point1.x + t * point2.x
let newY = (1 - t) * point1.y + t * point2.y
newPoints.append(CGPoint(x: newX, y: newY))
}
points = newPoints
}
return points[0]
}
func deCasteljau(points: [CGPoint], t: CGFloat) -> [[CGPoint]] {
var points = points
var result = [[CGPoint]]()
result.append(points)
while points.count > 1 {
var newPoints = [CGPoint]()
for i in 0..<points.count - 1 {
let point1 = points[i]
let point2 = points[i + 1]
let newX = (1 - t) * point1.x + t * point2.x
let newY = (1 - t) * point1.y + t * point2.y
newPoints.append(CGPoint(x: newX, y: newY))
}
points = newPoints
result.append(points)
}
return result
}
func getPointsOnBezierCurve(points: [CGPoint], numPoints: Int) -> [CGPoint] {
var cpoints = [CGPoint]()
for i in 0..<numPoints {
let t = CGFloat(i) / CGFloat(numPoints - 1)
cpoints.append(getPointOnBezierCurve(points: points, t: t))
}
return cpoints
}
}
struct PointWrapper: Hashable {
let x: CGFloat
let y: CGFloat
// let point: CGPoint
init(_ point: CGPoint) {
// self.point = point
self.x = point.x
self.y = point.y
}
}
extension Text {
func highlight() -> some View {
self
.font(.title)
.fontWeight(.semibold)
.foregroundColor(.white)
.padding(EdgeInsets(top: 3, leading: 9, bottom: 3, trailing: 9))
.background(
RoundedRectangle(cornerRadius: 20)
.fill(Color.blue)
)
}
}