|
1 | 1 | # VectorKit
|
2 | 2 |
|
3 |
| -[](https://travis-ci.org/David Lee/VectorKit) |
4 |
| -[](http://cocoapods.org/pods/VectorKit) |
5 |
| -[](http://cocoapods.org/pods/VectorKit) |
6 |
| -[](http://cocoapods.org/pods/VectorKit) |
| 3 | +[](https://travis-ci.org/David Lee/Vector) |
| 4 | +[](http://cocoapods.org/pods/Vector) |
| 5 | +[](http://cocoapods.org/pods/Vector) |
| 6 | +[](http://cocoapods.org/pods/Vector) |
7 | 7 |
|
8 | 8 | ## Usage
|
9 | 9 |
|
10 |
| -To run the example project, clone the repo, and run `pod install` from the Example directory first. |
| 10 | +VectorKit provides a `Vector` protocol which can give any type the power to perform vector operations. |
11 | 11 |
|
12 |
| -## Requirements |
| 12 | +Here is a `CGPoint` extension, adopting the `Vector` protocol. |
| 13 | + |
| 14 | +```swift |
| 15 | +extension CGPoint: Vector { |
| 16 | + // We need to specify the number of dimensions, so that the vector operations can be configured to fit the dimension. |
| 17 | + public var numberOfDimensions: Int { return 2 } |
| 18 | + |
| 19 | + // Every `Vector` can be initialized from a collection. This allows easy conversion among similar vector types. |
| 20 | + public init<T where T: CollectionType, T.Generator.Element == CGFloat>(collection: T) { |
| 21 | + var g = collection.generate() |
| 22 | + guard let x = g.next(), let y = g.next() else { |
| 23 | + fatalError() |
| 24 | + } |
| 25 | + self.init(x: x, y: y) |
| 26 | + } |
| 27 | + |
| 28 | + // `Vector` inherits from `CollectionType`, and internally leverages `CollectionType`'s methods for support |
| 29 | + // for multi-dimensional vectors. |
| 30 | + public subscript(index: Int) -> CGFloat { |
| 31 | + switch index { |
| 32 | + case 0: |
| 33 | + return x |
| 34 | + case 1: |
| 35 | + return y |
| 36 | + default: |
| 37 | + fatalError() |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +Once we define this adoption, we can use all of `Vector`'s operations within `CGPoint`. |
| 44 | + |
| 45 | +```swift |
| 46 | +let pt1 = CGPoint(x: 3, y: 4) |
| 47 | +let pt2 = CGPoint(collection: [1, -2]) |
| 48 | + |
| 49 | +pt1.magnitude == 5 |
| 50 | +pt1 + pt2 == CGPoint(x: 4, y: 2) |
| 51 | +pt2 * 0.5 == CGPoint(x: 0.5, y: -1) |
| 52 | +-pt1 == CGPoint(x: -4, y: -2) |
| 53 | +``` |
| 54 | + |
| 55 | +`Vector` also supports a handful of operations which can combine different kinds of `Vector`s. In the following example, both `CGPoint` and `CGSize` have adopted `Vector`. |
| 56 | + |
| 57 | +```swift |
| 58 | +let point = CGPoint(x: 1, y: -1) |
| 59 | +let size = CGSize(width: 5, height: 10) |
| 60 | + |
| 61 | +let pointDifference: CGPoint = point - size |
| 62 | +let sizeDifference: CGSize = point - size |
| 63 | + |
| 64 | +pointDifference == CGPoint(x: -4, y: -11) |
| 65 | +sizeDifference == CGSize(width: -4, height: -11) |
| 66 | +``` |
| 67 | + |
| 68 | +## Running the tests |
| 69 | + |
| 70 | +To run the tests included in the example project, clone the repo, and run `pod install` from the Example directory first. |
13 | 71 |
|
14 | 72 | ## Installation
|
15 | 73 |
|
16 |
| -VectorKit is available through [CocoaPods](http://cocoapods.org). To install |
| 74 | +Vector is available through [CocoaPods](http://cocoapods.org). To install |
17 | 75 | it, simply add the following line to your Podfile:
|
18 | 76 |
|
19 | 77 | ```ruby
|
20 |
| -pod "VectorKit" |
| 78 | +pod "VectorKit", :git => "https://github.com/davidisaaclee/VectorKit.git" |
21 | 79 | ```
|
22 | 80 |
|
23 | 81 | ## Author
|
24 | 82 |
|
25 |
| -David Lee, aewofij@gmail.com |
| 83 | +David Lee, http://david-lee.net |
26 | 84 |
|
27 | 85 | ## License
|
28 | 86 |
|
29 |
| -VectorKit is available under the MIT license. See the LICENSE file for more info. |
| 87 | +Vector is available under the MIT license. See the LICENSE file for more info. |
0 commit comments