Functions

The following functions are available globally.

  • Prints a string to the console. Replacement for the noisy NSlog.

    C4Log("A message")
    C4Log(0)
    

    Declaration

    Swift

    public func C4Log<T>(value: T)

    Parameters

    value

    An object to print to the console

  • Returns a rectangle that contains all of the specified coordinates in an array.

    let points = [CGPointZero,CGPointMake(10,10)]
    let cgrect = CGRectMakeFromPoints(points)
    

    Declaration

    Swift

    public func CGRectMakeFromPoints(points: [CGPoint]) -> CGRect

    Parameters

    points

    An array of CGPoint coordinates

    Return Value

    The smallest CGRect that contains all of the points in the specified array

  • Sets a time to wait before executing of a block of code.

    delay(0.25) {
        //code to execute
    }
    

    Declaration

    Swift

    public func wait(seconds: Double, action: ()->())

    Parameters

    delay

    The amount of time in seconds to wait before executing the block of code.

    action

    A block of code to perform after the delay.

  • Clamp a value to the range [min, max].

    If the value is less than min this function returns min, if the value is greater than max the function returns max, otherwise it returns the value.

    clamp(10, 0, 5) = 5
    clamp(10, 0, 20) = 10
    clamp(10,20,30) = 20
    

    Declaration

    Swift

    public func clamp<T: Comparable>(val: T, min: T, max: T) -> T

    Parameters

    val

    The value

    min

    The lower bound

    max

    The upper bound

    Return Value

    The clamped value

  • Linear interpolation. For any two values a and b return a linear interpolation with parameter param.

    lerp(0, 100, 0.5) = 50
    lerp(100, 200, 0.5) = 150
    lerp(500, 1000, 0.33) = 665
    

    Declaration

    Swift

    public func lerp(a: Double, _ b: Double, at: Double) -> Double

    Parameters

    a

    first value

    b

    second value

    param

    parameter between 0 and 1 for interpolation

    Return Value

    The interpolated value

  • Linear mapping. Maps a value in the source range [min, max] to a value in the target range [toMin, toMax] using linear interpolation.

    map(10, 0, 20, 0, 200) = 100
    map(10, 0, 100, 200, 300) = 210
    map(10, 0, 20, 200, 300) = 250
    

    Declaration

    Swift

    public func map(val: Double, min: Double, max: Double, toMin: Double, toMax: Double) -> Double

    Parameters

    val

    Source value

    min

    Source range lower bound

    max

    Source range upper bound

    toMin

    Target range lower bound

    toMax

    Target range upper bound

    Return Value

    The mapped value.

  • Return a random integer below below

    let x = random(below: 20)
    

    Declaration

    Swift

    public func random(below below: Int) -> Int

    Parameters

    below

    The upper bound

    Return Value

    A random value smaller than below

  • Return a random integer greater than or equal to min and less than max.

    let x = random(10,20)
    

    Declaration

    Swift

    public func random(min min: Int, max: Int) -> Int

    Parameters

    min

    The lower bound

    max

    The upper bound

    Return Value

    A random value greater than or equal to min and less than max.

  • Return a random Double in the interval [0, 1)

    let x = random01()
    

    Declaration

    Swift

    public func random01() -> Double

    Return Value

    A random Double uniformly distributed between 0 and 1

  • Converts radian values to degrees.

    Uses the following equation: value * 180.0 / PI

    radToDeg(M_PI) = 180
    radToDeg(M_PI_2) = 90
    

    Declaration

    Swift

    public func radToDeg(val: Double) -> Double

    Parameters

    val

    The value in radians.

    Return Value

    A double value representation of the radian value in degrees.

  • Converts degree values to radians.

    Uses the following equation: value * PI / 180.0

    degToRad(270) = 3*M_PI_2 (4.712...)
    degToRad(360) = 2*PI (6.283...)
    

    Declaration

    Swift

    public func degToRad(val: Double) -> Double

    Parameters

    val

    The value in degrees.

    Return Value

    A double value representation of the degree value in radians.

  • Determine if two paths are equal - parameter left: the first path to compare - parameter right: the second path to compare - returns: a boolean, true if the patrhs are equal, otherwise false

    Declaration

    Swift

    public func == (left: Path, right: Path) -> Bool

    Parameters

    left

    the first path to compare

    right

    the second path to compare

    Return Value

    a boolean, true if the patrhs are equal, otherwise false

  • Translate a point by the given vector.

    Declaration

    Swift

    public func += (inout lhs: Point, rhs: Vector)

    Parameters

    lhs

    a Point to translate

    rhs

    a Vector whose values will be applied to the point

  • Translate a point by the negative of the given vector

    Declaration

    Swift

    public func -= (inout lhs: Point, rhs: Vector)

    Parameters

    lhs

    a Point to translate

    rhs

    a Vector whose values will be applied to the point

  • Calculate the vector between two points

    Declaration

    Swift

    public func - (lhs: Point, rhs: Point) -> Vector

    Parameters

    lhs

    a Point

    rhs

    a Point

    Return Value

    a Vector whose value is the left-hand side the right-hand side

  • Translate a point by the given vector.

    Declaration

    Swift

    public func + (lhs: Point, rhs: Vector) -> Point

    Parameters

    lhs

    a Point to translate

    rhs

    a Vector whose values will be applied to the point

    Return Value

    A new point whose coordinates have been translated by the values from the vector (e.g. point.x = lhs.x + rhs.x)

  • Translate a point by the negative of the vector.

    Declaration

    Swift

    public func - (lhs: Point, rhs: Vector) -> Point

    Parameters

    lhs

    a Point to translate

    rhs

    a Vector whose values will be applied to the point

    Return Value

    A new point whose coordinates have been translated by the negative vector (e.g. point.x = lhs.x - rhs.x)

  • Calculates the distance between two points.

    Declaration

    Swift

    public func distance(lhs: Point, rhs: Point) -> Double

    Parameters

    lhs

    left-hand point

    rhs

    right-hand point

    Return Value

    The linear distance between two points

  • Checks to see if two points are equal.

    Declaration

    Swift

    public func == (lhs: Point, rhs: Point) -> Bool

    Parameters

    lhs

    a Point

    rhs

    a Point

    Return Value

    true if the two structs have identical coordinates

  • Linear interpolation.

    For any two points a and b return a point that is the linear interpolation between a and b for interpolation parameter param. For instance, a parameter of 0 will return a, a parameter of 1 will return b and a parameter of 0.5 will return the midpoint between a and b.

    Declaration

    Swift

    public func lerp(a: Point, _ b: Point, at: Double) -> Point

    Parameters

    a

    the first point

    b

    the second point

    param

    a Double value (between 0.0 and 1.0) used to calculate the point between a and b

    Return Value

    an interpolated point

  • Checks to see if two C4Rects share identical origin and size

    let r1 = Rect(0,0,10,10)
    let r2 = Rect(0,0,10,10.5)
    println(r1 == r2) //-> false
    

    Declaration

    Swift

    public func == (lhs: Rect, rhs: Rect) -> Bool

    Parameters

    lhs

    The first rectangle to compare

    rhs

    The second rectangle to compare

    Return Value

    A bool, true if the rects are identical, otherwise false.

  • Returns the intersection of two rectangles.

    let r1 = Rect(0,0,10,10)
    let r2 = Rect(5,5,10,10)
    intersection(r1,r2) //-> {5,5,5,5}
    

    Declaration

    Swift

    public func intersection(rect1: Rect, rect2: Rect) -> Rect

    Parameters

    rect1

    The first source rectangle.

    rect2

    The second source rectangle.

    Return Value

    A rectangle that represents the intersection of the two specified rectangles.

  • Returns the smallest rectangle that contains the two source rectangles.

    let r1 = Rect(0,0,10,10)
    let r2 = Rect(5,5,10,10)
    intersection(r1,r2) //-> {0,0,15,15}
    

    Declaration

    Swift

    public func union(rect1: Rect, rect2: Rect) -> Rect

    Parameters

    rect1

    The first source rectangle.

    rect2

    The second source rectangle.

    Return Value

    The smallest rectangle that completely contains both of the source rectangles.

  • Returns the smallest rectangle that results from converting the source rectangle values to integers.

    let r = Rect(0.1, 0.9, 9.1, 9.9)
    integral(r) //-> {0, 0, 10, 10}
    

    Declaration

    Swift

    public func integral(r: Rect) -> Rect

    Parameters

    r

    The source rectangle.

    Return Value

    A rectangle with the smallest integer values for its origin and size that contains the source rectangle.

  • Returns a rectangle with a positive width and height.

    let r = Rect(0, 0, -10, -10)
    standardize(r) //-> {-10, -10, 10, 10}
    

    Declaration

    Swift

    public func standardize(r: Rect) -> Rect

    Parameters

    r

    The source rectangle.

    Return Value

    A rectangle that represents the source rectangle, but with positive width and height values.

  • Returns a rectangle that is smaller or larger than the source rectangle, with the same center point.

    let r = Rect(0,0,10,10)
    inset(r, 1, 1) //-> {1,1,8,8}
    

    Declaration

    Swift

    public func inset(r: Rect, dx: Double, dy: Double) -> Rect

    Parameters

    r

    The source Rect structure.

    dx

    The x-coordinate value to use for adjusting the source rectangle.

    dy

    The y-coordinate value to use for adjusting the source rectangle.

    Return Value

    A rectangle.

  • Returns true if the two source Size structs share identical dimensions

    let s1 = Size()
    let s2 = Size(1,1)
    s1 == s2 //-> false
    

    Declaration

    Swift

    public func == (lhs: Size, rhs: Size) -> Bool

    Parameters

    lhs

    The first size to compare

    rhs

    The second size to compare

    Return Value

    A boolean, true if the sizes are equal, otherwise false

  • Returns true if the left-hand size is bigger than the right-hand size

    let s1 = Size(3,4)
    let s2 = Size(4,3)
    let s3 = Size(2,2)
    
    s1 > s2 //-> false
    s2 > s3 //-> true
    

    Declaration

    Swift

    public func > (lhs: Size, rhs: Size) -> Bool

    Parameters

    lhs

    The first size to compare

    rhs

    The second size to compare

    Return Value

    A boolean, true if the area of lhs is greater than that of rhs

  • Returns true if the left-hand size is smaller than the right-hand size

    let s1 = Size(3,4)
    let s2 = Size(4,3)
    let s3 = Size(2,2)
    
    s1 < s2 //-> false
    s2 < s3 //-> false
    

    Declaration

    Swift

    public func < (lhs: Size, rhs: Size) -> Bool

    Parameters

    lhs

    The first size to compare

    rhs

    The second size to compare

    Return Value

    A boolean, true if the area of lhs is less than that of rhs

  • Returns true if the left-hand size is greater than or equal to the right-hand size

    let s1 = Size(3,4)
    let s2 = Size(4,3)
    let s3 = Size(2,2)
    
    s1 => s2 //-> true
    s2 => s3 //-> true
    

    Declaration

    Swift

    public func >= (lhs: Size, rhs: Size) -> Bool

    Parameters

    lhs

    The first size to compare

    rhs

    The second size to compare

    Return Value

    A boolean, true if the area of lhs is greater than or equal to that of rhs

  • Returns true if the left-hand size is smaller than or equal to the right-hand size

    let s1 = Size(3,4)
    let s2 = Size(4,3)
    let s3 = Size(2,2)
    
    s1 <= s2 //-> true
    s2 <= s3 //-> false
    

    Declaration

    Swift

    public func <= (lhs: Size, rhs: Size) -> Bool

    Parameters

    lhs

    The first size to compare

    rhs

    The second size to compare

    Return Value

    A boolean, true if the area of lhs is less than or equal to that of rhs

  • Returns true if the two source Transform structs share identical dimensions - parameter lhs: The first transform to compare - parameter rhs: The second transform to compare - returns: A boolean, true if the both transforms are equal

    Declaration

    Swift

    public func == (lhs: Transform, rhs: Transform) -> Bool

    Parameters

    lhs

    The first transform to compare

    rhs

    The second transform to compare

    Return Value

    A boolean, true if the both transforms are equal

  • Transform matrix multiplication - parameter lhs: The first transform to multiply - parameter rhs: The second transform to multiply - returns: A new transform that is the result of multiplying lhs and rhs

    Declaration

    Swift

    public func * (lhs: Transform, rhs: Transform) -> Transform

    Parameters

    lhs

    The first transform to multiply

    rhs

    The second transform to multiply

    Return Value

    A new transform that is the result of multiplying lhs and rhs

  • Transform matrix scalar multiplication - parameter t: The transform to scale - parameter s: A scalar value to apply to the transform - returns: A new trasform whose values are the scalar multiple of t

    Declaration

    Swift

    public func * (t: Transform, s: Double) -> Transform

    Parameters

    t

    The transform to scale

    s

    A scalar value to apply to the transform

    Return Value

    A new trasform whose values are the scalar multiple of t

  • Transform matrix scalar multiplication - parameter s: A scalar value to apply to the transform - parameter t: The transform to scale - returns: A new trasform whose values are the scalar multiple of t

    Declaration

    Swift

    public func * (s: Double, t: Transform) -> Transform

    Parameters

    s

    A scalar value to apply to the transform

    t

    The transform to scale

    Return Value

    A new trasform whose values are the scalar multiple of t

  • Concatenate two transformations. This is the same as t2 * t1. - parameter t1: The first transform to contatenate - parameter t2: The second transform to contatenate - returns: A new transform that is the contcatenation of t1 and t2

    Declaration

    Swift

    public func concat(t1: Transform, t2: Transform) -> Transform

    Parameters

    t1

    The first transform to contatenate

    t2

    The second transform to contatenate

    Return Value

    A new transform that is the contcatenation of t1 and t2

  • Calculates the inverse of a transfomation. - parameter t: The transform to invert - returns: A new transform that is the inverse of t

    Declaration

    Swift

    public func inverse(t: Transform) -> Transform?

    Parameters

    t

    The transform to invert

    Return Value

    A new transform that is the inverse of t

  • Returns true if the coordinates of both vectors are identical

    let v1 = Vector(x: 1, y: 1)
    let v2 = Vector(x: 1, y: 0)
    v1 == v2 //-> false
    

    Declaration

    Swift

    public func == (lhs: Vector, rhs: Vector) -> Bool

    Parameters

    lhs

    A Vector

    rhs

    A Vector

    Return Value

    A boolean, true if the vectors are equal, false otherwise

  • Transforms the left-hand vector by adding the values of the right-hand vector

    let v1 = Vector(x: 1, y: 1)
    let v2 = Vector(x: 1, y: 0)
    v1 += v2 //-> v1 = {2,1,0}
    

    Declaration

    Swift

    public func += (inout lhs: Vector, rhs: Vector)

    Parameters

    lhs

    A Vector to which the values of rhs will be added

    rhs

    A Vector

  • Transforms the left-hand vector by subtracting the values of the right-hand vector

    let v1 = Vector(x: 1, y: 1)
    let v2 = Vector(x: 1, y: 0)
    v1 += v2 //-> v1 = {0,1,0}
    

    Declaration

    Swift

    public func -= (inout lhs: Vector, rhs: Vector)

    Parameters

    lhs

    A Vector to which the values of rhs will be subtracted

    rhs

    A Vector

  • Transforms the left-hand vector by multiplying each by the values of the right-hand vector

    let v1 = Vector(x: 1, y: 1)
    v *= 2.0 //-> v1 = {2,2,0}
    

    Declaration

    Swift

    public func *= (inout lhs: Vector, rhs: Double)

    Parameters

    lhs

    A Vector whose values will be multiplied by rhs

    rhs

    A scalar value

  • Transforms the left-hand vector by dividing each by the values of the right-hand vector

    let v1 = Vector(x: 1, y: 1)
    v /= 2.0 //-> v = {0.5,0.5,0.0}
    

    Declaration

    Swift

    public func /= (inout lhs: Vector, rhs: Double)

    Parameters

    lhs

    A Vector whose values will be divided by rhs

    rhs

    A scalar value

  • Returns a new vector whose coordinates are the sum of both input vectors

    let v1 = Vector(x: 1, y: 1)
    let v2 = Vector(x: 1, y: 0)
    v1+v2 //-> {2,1,0}
    

    Declaration

    Swift

    public func + (lhs: Vector, rhs: Vector) -> Vector

    Parameters

    lhs

    A Vector

    rhs

    A Vector

    Return Value

    A new vector whose values are the sum of lhs and rhs

  • Returns a new vector whose coordinates are the subtraction of the right-hand vector from the left-hand vector

    var v1 = Vector(x: 1, y: 1)
    var v2 = Vector(x: 1, y: 1)
    v1-v2 //-> {0,0,0}
    

    Declaration

    Swift

    public func - (lhs: Vector, rhs: Vector) -> Vector

    Parameters

    lhs

    A Vector

    rhs

    A Vector

    Return Value

    A new vector whose values are the difference of lhs and rhs

  • Returns a new vector that is the dot product of the both input vectors. Use this instead of v.dot(v)

    let v1 = Vector(x: 1, y: 1)
    let v2 = Vector(x: -1, y: 1)
    v1 ⋅ v2 //-> 0.0
    

    Declaration

    Swift

    public func  (lhs: Vector, rhs: Vector) -> Double

    Parameters

    lhs

    A Vector

    rhs

    A Vector

    Return Value

    The dot product of lhs and rhs

  • Returns a new vector whose coordinates are the division of the left-hand vector coordinates by those of the right-hand vector

    var v1 = Vector(x: 1, y: 1)
    var v2 = v1 / 2.0
    v2 //-> {0.5,0.5,0}
    

    Declaration

    Swift

    public func / (lhs: Vector, rhs: Double) -> Vector

    Parameters

    lhs

    A Vector

    rhs

    A scalar

    Return Value

    A new vector whose values are those of lhs divided by rhs

  • Returns a new vector whose coordinates are the multiplication of the left-hand vector coordinates by those of the right-hand vector

    var v1 = Vector(x: 1, y: 1)
    var v2 = v2 * 2.0
    v2 //-> {2,2,0}
    

    Declaration

    Swift

    public func * (lhs: Vector, rhs: Double) -> Vector

    Parameters

    lhs

    A Vector

    rhs

    A scalar

    Return Value

    A new vector whose values are those of lhs multiplied by rhs

  • Returns a new vector whose coordinates are the multiplication of the right-hand vector coordinates by the left-hand scalar

    var v1 = Vector(x: 1, y: 1)
    var v2 = 2.0 * v2
    v2 //-> {2,2,0}
    - parameter lhs: A scalar
    - parameter rhs: A Vector
    - returns: A new vector whose values are those of `lhs` divided by `rhs`
    

    Declaration

    Swift

    public func * (lhs: Double, rhs: Vector) -> Vector
  • Returns a new vector whose coordinates are the negative values of the receiver

    var v1 = Vector(x: 1, y: 1)
    var v2 = -v1
    v2 //-> {-1,-1}
    

    Declaration

    Swift

    public prefix func - (vector: Vector) -> Vector

    Parameters

    vector

    A Vector

    Return Value

    A new vector whose values are the negative of vector