> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/ios/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/ios/custom/package/geometrykit.md).

# GeometryKit

[swift](/ios/swift.md) ⟩ [type](/ios/swift/type.md) ⟩ [custom](/ios/custom.md) ⟩ [package](/ios/custom/package.md) ⟩ 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](/ios/custom/package/geometrykit/metricspace.md)：支援「<mark style="color:yellow;">距離</mark>」觀念
  * [Frame](/ios/custom/package/geometrykit/frame.md)：簡化 frame 與 CGRect 的相關計算
  * [Vector](/ios/custom/package/geometrykit/vector.md)：支援「<mark style="color:yellow;">向量運算</mark>」的底層邏輯
    * [Vector2D](/ios/custom/package/geometrykit/vector2d.md)：支援「<mark style="color:yellow;">平面幾何</mark>」的各種計算
      * [ComplexNumber](/ios/custom/package/geometrykit/complexnumber.md)：支援「<mark style="color:yellow;">複數</mark>」運算
        {% endtab %}

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

```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)
    }
}
```

做好這些[擴充](/ios/swift/type/ext.md)宣告後，其他檔案就不需要再宣告一次了。但使用到 <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](/ios/swift/type/category/protocol/associated-type.md)
  {% 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 的對齊方式](/ios/swiftui/view/measure/geometryreader/geometryreader-de-dui-qi-fang-shi.md)
  {% endtab %}
  {% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lochiwei.gitbook.io/ios/custom/package/geometrykit.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
