Skip to content
This repository was archived by the owner on Nov 12, 2024. It is now read-only.

Repository files navigation

VectorArithmetic

Overview

Extending CGPoint, CGSize and CGVector with the protocols VectorOperatable & VectorArithmetic

Handles some of the Swift bugs related to architecture differences.

Just want to point out that Apple themselves uses CGPoint throughout UIKit for velocity and other vectors. So remember that when you see myPoint + myOtherPoint * 2

Dependency

To get better interoperability between different numerical types use

Usage

Math Functions
letvector=CGVector(20,5.5)letpoint=CGPoint(x:5, y:5)
vector.angleInRadians // -1.30243011588899
vector.magnitude // 20.7424685126915
vector.length // synonym to magnitude
vector.lengthSquared // 430.25
vector.dotProduct(point) // 127.5
vector.crossProduct(point) // -72.5
vector.distanceTo(point) // 15.0083310198036
vector.reversed // {dx -20.0, dy -5.5}
vector.normalized // {dx 0.96420539280379, dy 0.265156483021042} vector.limited(20) // {dx 19.2841078560758, dy 5.30312966042085}
vector.scaled(20) // synonym to limited()
vector.angled(90) // {dx -9.29415287392715, dy 18.5436976451859}
Operators
varpoint:CGPoint=CGPoint(x:2.0, y:2.0)varvector:CGVector=CGVector(horizontal:2.0, vertical:2.0)
point = point + vector
point += vector
point = point - vector
point -= vector
point = point * vector
point *= vector
point = point / vector
point /= vector
vector = vector *4.5
vector *=20.5
vector = vector /2.0
vector /=2.0
Equatable and Comparable
point == vector
point != vector
point < vector
point <= vector
point > vector
point >= vector
Generic initializer
letpoint=CGPoint(horizontal:2.0,vertical:2.0)letvector=CGVector(horizontal:2.0,vertical:2.0)letyourStruct=YourStruct(horizontal:2.0,vertical:2.0)
Type constraints
classBoid<T:VectorOperatable, U:VectorOperatable>{varposition=T(horizontal:2.0, vertical:2.5)varvelocity=U(horizontal:2.0, vertical:2.5)}letboid=Boid<CGPoint,CGVector>()
boid.position = CGPointZero
boid.velocity =CGVectorMake(20,20)
boid.velocity > boid.position // True

Extending

Equatable and Comparable
protocolVectorOperatable{init(horizontal:Double,vertical:Double)varhorizontal:Double{getset}varvertical:Double{getset}}
Math Functions
protocolVectorArithmetic:VectorOperatable{varangleInRadians:Double{get}varmagnitude:Double{get}varlength:Double{get}varlengthSquared:Double{get}func dotProduct <T :VectorArithmetic>(vector:T)->Doublefunc distanceTo <T :VectorArithmetic>(vector:T)->Doublevarreversed:Self{get}varnormalized:Self{get}func limited(scalar:Double)->Selffunc scaled(scalar:Double)->Selffunc angled(scalar:Double)->Self}

###Contact

If you end up using VectorArithmetic in a project, I'd love to hear about it.

email: seivan.heidari@icloud.com
twitter: @seivanheidari


License

VectorArithmetic is © 2014 Seivan and may be freely distributed under the MIT license. See the LICENSE.md file.


About

Operators on top of vectors CGPoint, CGSize and CGVector. With added math functions and comparisons

Resources

Contributing

Stars

42 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages