Code Monkey home page Code Monkey logo

mathutil's Introduction

MathUtil

Language License: MIT Carthage compatible Build Status

Overview

  • Strongly typed radian, degree and hour angle classes and conversions.
  • 3x3 matrix, 4x4 matrix and quaternion support.
    • Conversion between matrix, quaternion, Euler angle, and axis and rotation.
    • Matrix, quaternion multiplication, inversion and scalar multiplication.
    • Overloaded multiplication and division operators in contexts of matrix and quaternion.
  • Interpolation support for linear, quadratic and cubic curves with ease-in, ease-out, ease-in-out modes.
  • Detect whether two line segments cross each other.
  • Operator overloading for SCNVector3 and more.

Installation

Carthage

# Swift 4.0
github "DJBen/MathUtil" ~> 0.3.0

This version is compatible with Swift 4. To use Swift 3.x compatible version, use 0.1.x instead.

# Swift 3.x
github "DJBen/MathUtil" ~> 0.1.0

Usage

Strongly Typed Angles

Radian-degree Angle Conversion

RadianAngle(degreeAngle: DegreeAngle(30)).value // 0.523...
DegreeAngle(radianAngle: RadianAngle(Double.Pi / 2)).value  // 90

Hour Angle Conversion and Formatting

// Sirius' right ascension is 6h 45m 9s
let deg = DegreeAngle(hour: 6, minute: 45, second: 9)
deg.value // 101.2875

let hourAngle = HourAngle(value: deg)
// Use any custom number formatters for different decimal lengths
hourAngle.compoundDecimalNumberFormatter = NumberFormatter()
print(hourAngle.compoundDescription) // 6h 45m 9s

// Antares' right ascension is −26.432002 degrees
let degreeAngle = DegreeAngle(-26.432002)
print(degreeAngle.compoundDescription) // -26° 25′ 55″

Matrix, Quaternion and Vector Arithmetics

See documentation for VectorMath.

Interpolations

Linear interpolation

// Starts at 2.1; ends at 7.5; wants to know 85% value.
let interp = Easing(startValue: 2.1, endValue: 7.5)
interp.value(at: 0.85)   // 6.69

Quadratic, Cubic, Ease In, Ease Out

The following easing functions are available and here is their mathematic definitions.

public enum EasingFunction {
    case linear
    case quadraticEaseIn
    case quadraticEaseOut
    case quadraticEaseInOut
    case cubicEaseIn
    case cubicEaseOut
    case cubicEaseInOut
}

Just pass in the function when initializing Easing and take advantage of it!

Easing(easingMethod: .cubicEaseInOut, startValue: 21, endValue: -9)

Operator overloading

  • Add operator arithmetics for CGVector and SCNVector3.

Remarks

MathUtil/VectorMath** are based on @nicklockwood's VectorMath. Great thanks!

  • Based on 0.3 version. Not bumping up to the latest release because I adopted a different order of deriving Euler angles from Quaternion and vice versa.

mathutil's People

Contributors

djben avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

johndpope e-001

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.