Vector

public class Vector : Shape
extension Vector: CGDrawable
extension Vector: SVGDrawable
extension Vector: CustomStringConvertible
extension Vector: Hashable

Represents a Euclidean vector

  • x

    The x position of the vector

    Declaration

    Swift

    public var x: Double
  • y

    The y position of the vector

    Declaration

    Swift

    public var y: Double
  • z

    The z position of the vector

    Declaration

    Swift

    public var z: Double
  • Instantiate a new Vector at the specified coordinates

    Declaration

    Swift

    @available(*, deprecated, renamed: "init(_:_:_:﹚")
    public init(x: Double, y: Double, z: Double = 0)

    Parameters

    x

    The x position of the vector

    y

    The y position of the vector

    z

    The z position of the vector

  • Instantiate a new Vector at the specified coordinates

    Declaration

    Swift

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

    Parameters

    x

    The x position of the vector

    y

    The y position of the vector

    z

    The z position of the vector

  • Instantiate a new Vector from the specified point

    Declaration

    Swift

    public init(_ point: NSPoint)

    Parameters

    point

    Point to use as coordinates

  • Instantiate a new Vector with the specified angle

    Declaration

    Swift

    public init(angle: Radians)

    Parameters

    angle

    The angle, in Radians, of the vector

  • Set the coordinates of the receiver to those of the specified vector

    Declaration

    Swift

    public func set(_ vector: Vector)

    Parameters

    v

    The vector whose coordinates will be copied

  • Return a new instance of the receiver

    Declaration

    Swift

    public func copy() -> Vector
  • A Rectangle that contains the receiver

    Declaration

    Swift

    public var boundingBox: Rectangle { get }
  • Return an NSPoint of the receiver

    Declaration

    Swift

    public func nsPoint() -> NSPoint

Static Vector Math Functions

  • Return the distance between two Vectors

    Declaration

    Swift

    @available(*, deprecated, message: "Please use instance methods")
    public static func dist(_ lhs: Vector, _ rhs: Vector) -> Double

    Parameters

    v1

    The first Vector

    v2

    the second Vector

  • Return the dot product of two Vectors

    Declaration

    Swift

    @available(*, deprecated, message: "Please use instance methods")
    public static func dot(_ lhs: Vector, _ rhs: Vector) -> Double

    Parameters

    v1

    The first Vector

    v2

    the second Vector

  • Return the cross product of two Vectors

    Declaration

    Swift

    @available(*, deprecated, message: "Please use instance methods")
    public static func cross(_ lhs: Vector, _ rhs: Vector) -> Vector

    Parameters

    v1

    The first Vector

    v2

    the second Vector

  • Calculates the cross product of two vectors.

    The following formula is used to calculate the cross product:

    (Vector1.X * Vector2.Y) - (Vector1.Y * Vector2.X)

    Implementation from C#.

    Declaration

    Swift

    @available(*, deprecated, message: "Please use instance methods")
    public static func crossProduct(_ lhs: Vector, _ rhs: Vector) -> Double

    Parameters

    v1

    The first vector to evaluate.

    v2

    The second vector to evaluate.

    Return Value

    The cross product of vector1 and vector2.

Vector Math Functions

  • Calculates the magnitude (length) of the vector and returns the result as a float

    This is simply the equation sqrt(x*x + y*y + z*z).

    Declaration

    Swift

    public func mag() -> Double
  • Calculates the squared magnitude of the vector and returns the result as a float

    This is simply the equation (x*x + y*y + z*z).

    Note

    Faster if the real length is not required in the case of comparing vectors, etc.

    Declaration

    Swift

    public func magSq() -> Double
  • Return the distance to another vector

    Declaration

    Swift

    public func dist(_ vector: Vector) -> Double

    Parameters

    vector

    Another vector

    Return Value

    Distance to the second vector

  • Calculates the dot product of two vectors.

    Declaration

    Swift

    public func dot(_ vector: Vector) -> Double
  • Calculates and returns a vector composed of the cross product between two vectors

    Declaration

    Swift

    public func cross(_ vector: Vector) -> Vector
  • Calculates the cross product of two vectors.

    The following formula is used to calculate the cross product:

    (Vector1.X * Vector2.Y) - (Vector1.Y * Vector2.X)

    Implementation from C#.

    Declaration

    Swift

    public func crossProduct(_ vector: Vector) -> Double

    Parameters

    v1

    The first vector to evaluate.

    v2

    The second vector to evaluate.

    Return Value

    The cross product of vector1 and vector2.

  • Normalize the vector to length 1 (make it a unit vector).

    Declaration

    Swift

    public func normalize()
  • Return a normalized copy of the vector.

    Declaration

    Swift

    public func normalized() -> Vector
  • Calculates and returns the angle (in radians) between two vectors.

    Declaration

    Swift

    public func angleBetween(_ vector: Vector) -> Double
  • Provide the heading of the receiver

    Declaration

    Swift

    public func heading() -> Radians
  • Rotate the receiver by the specified angle

    Declaration

    Swift

    public func rotate(by theta: Radians)

    Parameters

    theta

    Angle, in radians, to rotate

  • Set the heading of the receiver to the specified angle

    Declaration

    Swift

    public func rotate(to theta: Radians)

    Parameters

    theta

    The new heading

  • Draw the receiver in the specified context

    Declaration

    Swift

    public func draw(in context: CGContext)

    Parameters

    context

    Context in which to draw

  • Draw a representation of the receiver meant for debugging the shape in the specified context

    Declaration

    Swift

    public func debugDraw(in context: CGContext)

    Parameters

    context

    Context in which to draw

  • Create a XMLElement representing the receiver

    Declaration

    Swift

    public func svgElement() -> XMLElement
  • A textual representation of this instance.

    Declaration

    Swift

    public var description: String { get }
  • Adds two values and produces their sum.

    Declaration

    Swift

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

    Parameters

    lhs

    The first value to add.

    rhs

    The second value to add.

  • Adds two values and stores the result in the left-hand-side variable

    Declaration

    Swift

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

    Parameters

    lhs

    The first value to add.

    rhs

    The second value to add.

  • Add the specified value to a vector

    Declaration

    Swift

    public static func + (vector: Vector, num: Double) -> Vector

    Parameters

    vector

    The first value to add.

    num

    The second value to add.

  • Add the specified value to a vector

    Declaration

    Swift

    public static func += (vector: Vector, num: Double)

    Parameters

    vector

    The first value to add.

    num

    The second value to add.

  • Subtracts one vector from another and produces their difference

    Declaration

    Swift

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

    Parameters

    lhs

    A numeric value.

    rhs

    The value to subtract from lhs.

  • Subtracts the second vector from the first and stores the difference in the left-hand-side variable

    Declaration

    Swift

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

    Parameters

    lhs

    The first vector to subtract.

    rhs

    The second vector to subtract.

  • Subtracts one value from another and produces their difference, rounded to a representable value.

    Declaration

    Swift

    public static func - (vector: Vector, num: Double) -> Vector

    Parameters

    lhs

    A numeric value.

    rhs

    The value to subtract from lhs.

  • Subtracts the second value from the first and stores the difference in the left-hand-side variable.

    Declaration

    Swift

    public static func -= (vector: Vector, num: Double)

    Parameters

    lhs

    A numeric value.

    rhs

    The value to subtract from lhs.

  • Multiplies two values and produces their product

    Declaration

    Swift

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

    Parameters

    lhs

    The first value to multiply.

    num

    The second value to multiply.

  • Multiplies two values and stores the result in the left-hand-side variable.

    Declaration

    Swift

    public static func *= (lhs: Vector, num: Double)

    Parameters

    lhs

    The first value to multiply.

    num

    The second value to multiply.

  • Multiplies two values and produces their product

    Multiplying two Vectors calculates the dot product.

    Declaration

    Swift

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

    Parameters

    lhs

    The first value to multiply.

    num

    The second value to multiply.

  • Returns the quotient of dividing the first value by the second.

    Declaration

    Swift

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

    Parameters

    lhs

    The value to divide.

    num

    The value to divide lhs by.

  • Divides the first value by the second and stores the quotient in the left-hand-side variable.

    Declaration

    Swift

    public static func /= (lhs: Vector, num: Double)

    Parameters

    lhs

    The value to divide.

    num

    The value to divide lhs by.

  • Returns the quotient of dividing the first value by the second.

    Declaration

    Swift

    public static func / (num: Double, vector: Vector) -> Vector

    Parameters

    lhs

    The value to divide.

    num

    The value to divide lhs by.

  • Negate a Vector.

    Declaration

    Swift

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

    Parameters

    vector

    The vector to negate

  • Returns a Boolean value indicating whether two values are equal.

    Equality is the inverse of inequality. For any values a and b, a == b implies that a != b is false.

    Declaration

    Swift

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

    Swift

    public func hash(into hasher: inout Hasher)