# GeometryKit

[swift](https://lochiwei.gitbook.io/ios/swift) ⟩ [type](https://lochiwei.gitbook.io/ios/swift/type) ⟩ [custom](https://lochiwei.gitbook.io/ios/custom) ⟩ [package](https://lochiwei.gitbook.io/ios/custom/package) ⟩ GeometryKit&#x20;

{% hint style="success" %}
為了方便<mark style="color:yellow;">幾何計算</mark>而設計的模組，支援<mark style="color:yellow;">向量</mark>、<mark style="color:yellow;">複數</mark>運算。\
:link: <https://github.com/lochiwei/GeometryKit>
{% endhint %}

{% tabs %}
{% tab title="🔴 主題" %}

* [metricspace](https://lochiwei.gitbook.io/ios/custom/package/geometrykit/metricspace "mention")：支援「<mark style="color:yellow;">距離</mark>」觀念
  * [frame](https://lochiwei.gitbook.io/ios/custom/package/geometrykit/frame "mention")：簡化 frame 與 CGRect 的相關計算
  * [vector](https://lochiwei.gitbook.io/ios/custom/package/geometrykit/vector "mention")：支援「<mark style="color:yellow;">向量運算</mark>」的底層邏輯
    * [vector2d](https://lochiwei.gitbook.io/ios/custom/package/geometrykit/vector2d "mention")：支援「<mark style="color:yellow;">平面幾何</mark>」的各種計算
      * [complexnumber](https://lochiwei.gitbook.io/ios/custom/package/geometrykit/complexnumber "mention")：支援「<mark style="color:yellow;">複數</mark>」運算
        {% endtab %}

{% tab title="📕 用法" %}
將要遵循 <mark style="color:purple;">GeometryKit</mark> 相關協定 ([Vector2D](https://lochiwei.gitbook.io/ios/custom/package/geometrykit/vector2d), [ComplexNumber](https://lochiwei.gitbook.io/ios/custom/package/geometrykit/complexnumber) 等) 的具體型別列在一個獨立的檔案中，並加入該遵循的條件：

```swift
// 📁 +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)
    }
}
```

做好這些[擴充](https://lochiwei.gitbook.io/ios/swift/type/ext)宣告後，其他檔案就不需要再宣告一次了。但使用到 <mark style="color:purple;">GeometryKit</mark> 相關功能時，還是需要 <mark style="color:blue;">`import GeometryKit`</mark>，請注意❗️&#x20;

```swift
// 📁 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))
}
```

{% endtab %}

{% tab title="👥 相關" %}

* [associated-type](https://lochiwei.gitbook.io/ios/swift/type/category/protocol/associated-type "mention")
  {% endtab %}

{% tab title="📗 參考" %}

* ChatGPT ⟩&#x20;
  * [如何使用自製 Swift Package](https://chatgpt.com/share/67597791-e480-800e-8eef-6cc9d004bb18) &#x20;
  * [設計 MetricSpace 協定](https://chatgpt.com/share/675916e7-e2e4-800e-9dbb-b5ca5f1cca2e)&#x20;
  * [分享協定只要設定一次](https://chatgpt.com/share/67597d30-3e4c-800e-974a-8a6b5e74cdea)
    {% endtab %}

{% tab title="⬇️ 應用" %}

* [geometryreader-de-dui-qi-fang-shi](https://lochiwei.gitbook.io/ios/swiftui/view/measure/geometryreader/geometryreader-de-dui-qi-fang-shi "mention")
  {% endtab %}
  {% endtabs %}
