Latest 1.0.0
Homepage https://github.com/davidisaaclee/VectorKit
License MIT
Platforms ios 9.0
Authors

[![CI Status](http://img.shields.io/travis/David Lee/Vector.svg?style=flat)](https://travis-ci.org/David Lee/Vector)
Version
License
Platform

Usage

VectorKit provides a Vector protocol which can give any type the power to perform vector operations.

Here is a CGPoint extension, adopting the Vector protocol.

extension CGPoint: Vector {
    // We need to specify the number of dimensions, so that the vector operations can be configured to fit the dimension.
    public var numberOfDimensions: Int { return 2 }

    // Every `Vector` can be initialized from a collection. This allows easy conversion among similar vector types.
    public init<T where T: CollectionType, T.Generator.Element == CGFloat>(collection: T) {
        var g = collection.generate()
        guard let x = g.next(), let y = g.next() else {
            fatalError()
        }
        self.init(x: x, y: y)
    }

    // `Vector` inherits from `CollectionType`, and internally leverages `CollectionType`'s methods for support
    //   for multi-dimensional vectors.
    public subscript(index: Int) -> CGFloat {
        switch index {
        case 0:
            return x
        case 1:
            return y
        default:
            fatalError()
        }
    }
}

Once we define this adoption, we can use all of Vector‘s operations within CGPoint.

let pt1 = CGPoint(x: 3, y: 4)
let pt2 = CGPoint(collection: [1, -2])

pt1.magnitude == 5
pt1 + pt2 == CGPoint(x: 4, y: 2)
pt2 * 0.5 == CGPoint(x: 0.5, y: -1)
-pt1 == CGPoint(x: -4, y: -2)

Vector also supports a handful of operations which can combine different kinds of Vectors. In the following example, both CGPoint and CGSize have adopted Vector.

let point = CGPoint(x: 1, y: -1)
let size = CGSize(width: 5, height: 10)

let pointDifference: CGPoint = point - size
let sizeDifference: CGSize = point - size

pointDifference == CGPoint(x: -4, y: -11)
sizeDifference == CGSize(width: -4, height: -11)

Running the tests

To run the tests included in the example project, clone the repo, and run pod install from the Example directory first.

Installation

Vector is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "VectorKit", :git => "https://github.com/davidisaaclee/VectorKit.git"

Author

David Lee, http://david-lee.net

License

VectorKit is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "VectorKit",
    "version": "1.0.0",
    "summary": "Powerful vector operations for any type.",
    "description": "VectorKit gives you powerful vector operations for the types you're already using, by providing a Vectornprotocol which can give any type the power to perform vector operations.",
    "homepage": "https://github.com/davidisaaclee/VectorKit",
    "license": "MIT",
    "authors": {
        "David Lee": "[email protected]"
    },
    "social_media_url": "https://twitter.com/davidisaaclee",
    "platforms": {
        "ios": "9.0"
    },
    "source": {
        "git": "https://github.com/davidisaaclee/VectorKit.git",
        "tag": "1.0.0"
    },
    "source_files": [
        "Pod/Classes",
        "Pod/Classes/**/*.swift"
    ],
    "exclude_files": "Pod/Classes/Exclude"
}

Pin It on Pinterest

Share This