MetricSpace๏ผๆฏๆดใ่ท้ขใ่งๅฟต
Frame๏ผ็ฐกๅ frame ่ CGRect ็็ธ้่จ็ฎ
Vector๏ผๆฏๆดใๅ้้็ฎใ็ๅบๅฑค้่ผฏ
Vector2D๏ผๆฏๆดใๅนณ้ขๅนพไฝใ็ๅ็จฎ่จ็ฎ
ๅฐ่ฆ้ตๅพช GeometryKit ็ธ้ๅๅฎ (Vector2D, ComplexNumber ็ญ) ็ๅ
ท้ซๅๅฅๅๅจไธๅ็จ็ซ็ๆชๆกไธญ๏ผไธฆๅ ๅ
ฅ่ฉฒ้ตๅพช็ๆขไปถ๏ผ
// ๐ +GeometryKit.swift
import SwiftUI
import GeometryKit
// โญ๏ธ CGPoint + Vector2D
extension CGPoint: Vector2D {
// ๅ ็บ CGFloat ๅทฒ็ถๆๆ่ฉฒ้ตๅพช็ๅฑฌๆงใๆนๆณ๏ผ
// ๆไปฅๅช่ฆๆๆ associated type ๅณๅฏใ
public typealias Scalar = CGFloat
}
// โญ๏ธ CGSize + Vector2D
extension CGSize: Vector2D {
public typealias Scalar = CGFloat
// CGSize ๆฒๆ x, y ๅฑฌๆง
public var x: CGFloat { width }
public var y: CGFloat { height }
// CGSize ๆฒๆ init(x:y:) initializer
public init(x: CGFloat, y: CGFloat) {
self.init(width: x, height: y)
}
}
ๅๅฅฝ้ไบๆดๅ
ๅฎฃๅๅพ๏ผๅ
ถไปๆชๆกๅฐฑไธ้่ฆๅๅฎฃๅไธๆฌกไบใไฝไฝฟ็จๅฐ GeometryKit ็ธ้ๅ่ฝๆ๏ผ้ๆฏ้่ฆ import GeometryKit
๏ผ่ซๆณจๆโ๏ธ
// ๐ anotherFile.swift
import SwiftUI
import GeometryKit // โญ๏ธ ๆ็จๅฐๆญคๅ่ฝ็ๆชๆก้่ฆๅ ๅ
ฅๆญค่ก
public func testGeometryKit() {
let p = CGPoint(1,2) // โญ๏ธ CGPoint ๅทฒ็ถ้ตๅพช Vector2D
let q = CGPoint(3, 4)
print("---------------------")
print("p =", p)
print("q =", q)
print("p + q =", p + q)
print("p.dot(q) =", p.dot(q))
print("p.cross(q) =", p.cross(q))
print(p.angle(to: q))
}