CGVector

public extension CGVector

Initializers

  • SwifterSwift: Creates a vector with the given magnitude and angle.

    let vector = CGVector(angle: .pi, magnitude: 1)
    

    Declaration

    Swift

    init(angle: CGFloat, magnitude: CGFloat)

    Parameters

    angle

    The angle of rotation (in radians) counterclockwise from the positive x-axis.

    magnitude

    The lenth of the vector.

Operators

  • SwifterSwift: Multiplies a scalar and a vector (commutative).

    let vector = CGVector(dx: 1, dy: 1)
    let largerVector = vector * 2
    

    Declaration

    Swift

    static func * (vector: CGVector, scalar: CGFloat) -> CGVector

    Parameters

    vector

    The vector to be multiplied

    scalar

    The scale by which the vector will be multiplied

    Return Value

    The vector with its magnitude scaled

  • SwifterSwift: Multiplies a scalar and a vector (commutative).

    let vector = CGVector(dx: 1, dy: 1)
    let largerVector = 2 * vector
    

    Declaration

    Swift

    static func * (scalar: CGFloat, vector: CGVector) -> CGVector

    Parameters

    scalar

    The scalar by which the vector will be multiplied

    vector

    The vector to be multiplied

    Return Value

    The vector with its magnitude scaled

  • SwifterSwift: Compound assignment operator for vector-scalr multiplication

    var vector = CGVector(dx: 1, dy: 1)
    vector *= 2
    

    Declaration

    Swift

    static func *= (vector: inout CGVector, scalar: CGFloat)

    Parameters

    vector

    The vector to be multiplied

    scalar

    The scale by which the vector will be multiplied

  • SwifterSwift: Negates the vector. The direction is reversed, but magnitude remains the same.

    let vector = CGVector(dx: 1, dy: 1)
    let reversedVector = -vector
    

    Declaration

    Swift

    prefix static func - (vector: CGVector) -> CGVector

    Parameters

    vector

    The vector to be negated

    Return Value

    The negated vector