Coder Social home page Coder Social logo

kleopatra999 / vectorarithmetic Goto Github PK

View Code? Open in Web Editor NEW

This project forked from seivan/vectorarithmetic

0.0 2.0 0.0 500 KB

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

License: MIT License

Ruby 3.93% Swift 96.07%

vectorarithmetic's Introduction

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
let vector = CGVector(20, 5.5)
let point = 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
var point:CGPoint = CGPoint(x: 2.0, y: 2.0)
var vector: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
let point = CGPoint(horizontal:2.0,vertical:2.0)
let vector = CGVector(horizontal:2.0,vertical:2.0)
let yourStruct = YourStruct(horizontal:2.0,vertical:2.0)
Type constraints
class Boid<T:VectorOperatable, U:VectorOperatable> {
  var position = T(horizontal: 2.0, vertical: 2.5)
  var velocity = U(horizontal: 2.0, vertical: 2.5)
}

let boid = Boid<CGPoint, CGVector>()
boid.position = CGPointZero
boid.velocity = CGVectorMake(20, 20)
boid.velocity > boid.position // True

Extending

Equatable and Comparable
protocol VectorOperatable  {
  init(horizontal:Double,vertical:Double)
  var horizontal:Double { get set }
  var vertical:Double { get set }
}
Math Functions
protocol VectorArithmetic : VectorOperatable {
  var angleInRadians:Double {get}
  var magnitude:Double {get}
  var length:Double {get}
  var lengthSquared:Double {get}
  func dotProduct <T : VectorArithmetic>(vector:T) -> Double
  func distanceTo <T : VectorArithmetic>(vector:T) -> Double
  var reversed:Self {get}
  var normalized:Self {get}
  func limited(scalar:Double) -> Self
  func scaled(scalar:Double) -> Self
  func angled(scalar:Double) -> Self 
}

###Contact

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

email: [email protected]
twitter: @seivanheidari


License

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


vectorarithmetic's People

Contributors

seivan avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.