Vector

public struct Vector: Equatable, CustomStringConvertible

The Vector class is used for coordinate values and direction vectors.

  • x

    The x-value of the vector.

    Declaration

    Swift

    public var x: Double = 0
  • y

    The y-value of the vector.

    Declaration

    Swift

    public var y: Double = 0
  • z

    The z-value of the vector.

    Declaration

    Swift

    public var z: Double = 0
  • Creates a vector with default values {0,0,0,} let v = Vector()

    Declaration

    Swift

    public init()
  • Create a vector with a cartesian representation: an x and a y coordinates. The z variable is optional. let v = Vector(x: 1.0, y: 1.0, z: 1.0) let v = Vector(x: 1.0, y: 1.0) - parameter x: the x-value of the new vector - parameter y: the y-value of the new vector - parameter z: the z-value of the new vector (defaults to 0)

    Declaration

    Swift

    public init(x: Double, y: Double, z: Double = 0)

    Parameters

    x

    the x-value of the new vector

    y

    the y-value of the new vector

    z

    the z-value of the new vector (defaults to 0)

  • Create a vector with a cartesian representation: an x and a y coordinates. let v = Vector(x: 1, y: 1, z: 1) - parameter x: the x-value of the new vector - parameter y: the y-value of the new vector - parameter z: the z-value of the new vector (defaults to 0)

    Declaration

    Swift

    public init(x: Int, y: Int, z: Int = 0)

    Parameters

    x

    the x-value of the new vector

    y

    the y-value of the new vector

    z

    the z-value of the new vector (defaults to 0)

  • Create a vector with a polar representation: a magnitude and an angle in radians. The z variable is optional. Polar coordinate system - Wikipedia let m = sqrt(2.0) let h = M_PI_4 let v = Vector(magnitude: m, heading: h) v //-> {1,1,0} - parameter magnitude: the magnitude of the new vector - parameter heading: the heading (angle) of the new vector - parameter z: the z-value of the new vector (defaults to 0)

    Declaration

    Swift

    public init(magnitude: Double, heading: Double, z: Double = 0)

    Parameters

    magnitude

    the magnitude of the new vector

    heading

    the heading (angle) of the new vector

    z

    the z-value of the new vector (defaults to 0)

  • Initializes a Vector from a CGPoint - parameter point: a previously initialized CGPoint

    Declaration

    Swift

    public init(_ point: CGPoint)

    Parameters

    point

    a previously initialized CGPoint

  • Initializes a Vector from a Point - parameter point: a previously initialized Point

    Declaration

    Swift

    public init(_ point: Point)

    Parameters

    point

    a previously initialized Point

  • Initializes a Vector from another Vector - parameter copy: a previously initialized Vector

    Declaration

    Swift

    public init(copy original: Vector)

    Parameters

    copy

    a previously initialized Vector

  • The polar representation magnitude of the vector. let v = Vector(x: 2.0, y: 1.0, z: 0.0) v.magnitude //-> √2

    Declaration

    Swift

    public var magnitude: Double
  • The polar representation heading angle of the vector, in radians. let v = Vector(1,1,0) v.heading //-> M_PI_4

    Declaration

    Swift

    public var heading: Double
  • The angle between two vectors, based on {0,0} let v1 = Vector(x: 1, y: 1, z: 0) let v2 = Vector(x: -1, y: 1, z: 0) v1.angleTo(v2) //-> M_PI_2 - parameter vec: The vector used to calcuate the angle to the receiver - returns: The angle, measured in radians, between the receiver and vec

    Declaration

    Swift

    public func angleTo(vec: Vector) -> Double

    Parameters

    vec

    The vector used to calcuate the angle to the receiver

    Return Value

    The angle, measured in radians, between the receiver and vec

  • The angle between two vectors, based on a provided point let v1 = Vector(x: 1, y: 1, z: 0) let v2 = Vector(x: -1, y: 1, z: 0) let b = Vector(x: 0, y: 1, z: 0) v1.angleTo(v2, basedOn: b) //-> PI - parameter vec: The vector used to calcuate the angle to the receiver - parameter basedOn: A second vector used to calcuate the angle to the receiver - returns: The angle, measured in radians, between the receiver and vec

    Declaration

    Swift

    public func angleTo(vec: Vector, basedOn: Vector) -> Double

    Parameters

    vec

    The vector used to calcuate the angle to the receiver

    basedOn

    A second vector used to calcuate the angle to the receiver

    Return Value

    The angle, measured in radians, between the receiver and vec

  • Return the dot product. You should use the ⋅ operator instead. let v1 = Vector(x: 1, y: 1, z: 0) let v2 = Vector(x: -1, y: 1, z: 0) v1.dot(v2) //-> 0.0 - parameter vec: The vector used to calcuate the dot product - returns: The dot product of the receiver and vec

    Declaration

    Swift

    public func dot(vec: Vector) -> Double

    Parameters

    vec

    The vector used to calcuate the dot product

    Return Value

    The dot product of the receiver and vec

  • Return a vector with the same heading but a magnitude of 1. let v1 = Vector(x: 1, y: 1, z: 0) v1.unitVector() //-> {M_PI_4,M_PI_4,0} - returns: A new vector that is the unit vector of the receiver

    Declaration

    Swift

    public func unitVector() -> Vector?

    Return Value

    A new vector that is the unit vector of the receiver

  • Return true if the vector is zero. let v = Vector() v.isZero() //-> true - returns: A boolean, true if all values are 0, false otherwise

    Declaration

    Swift

    public func isZero() -> Bool

    Return Value

    A boolean, true if all values are 0, false otherwise

  • Transform the vector. var v = Vector(x: 1, y: 1, z:0) let t = Transform.makeRotation(M_PI, axis: Vector(x: 0, y:0, z:1)) v.transform(t) //-> {-1, -1, 0} - parameter t: A Transform to apply to the receiver

    Declaration

    Swift

    public mutating func transform(t: Transform)

    Parameters

    t

    A Transform to apply to the receiver

  • A string representation of the vector. - returns: A string formatted to look like {x,y,z}

    Declaration

    Swift

    public var description: String

    Return Value

    A string formatted to look like {x,y,z}