LineSegment

public struct LineSegment<Vector> : LineType, CustomStringConvertible where Vector : VectorType
extension LineSegment: Equatable where Vector: Equatable, Scalar: Equatable
extension LineSegment: Hashable where Vector: Hashable, Scalar: Hashable
extension LineSegment: Encodable where Vector: Encodable, Scalar: Encodable
extension LineSegment: Decodable where Vector: Decodable, Scalar: Decodable
extension LineSegment: BoundableType where Vector: VectorComparable
extension LineSegment: LineAdditive where Vector: VectorAdditive
extension LineSegment: LineMultiplicative where Vector: VectorMultiplicative
extension LineSegment: LineSigned where Vector: VectorSigned
extension LineSegment: LineDivisible where Vector: VectorDivisible
extension LineSegment: LineFloatingPoint & PointProjectableType & SignedDistanceMeasurableType where Vector: VectorFloatingPoint
extension LineSegment: LineReal where Vector: VectorReal

Represents a line segment as a pair of start and end N-dimensional vectors which describe a closed interval.

  • Declaration

    Swift

    public typealias Scalar = Vector.Scalar
  • The bounded start of this line segment, inclusive.

    Declaration

    Swift

    public var start: Vector
  • end

    The bounded end of this line segment, inclusive.

    Declaration

    Swift

    public var end: Vector
  • a

    Alias for start.

    Declaration

    Swift

    public var a: Vector { get }
  • b

    Alias for b.

    Declaration

    Swift

    public var b: Vector { get }
  • Declaration

    Swift

    @inlinable
    public var category: LineCategory { get }
  • Declaration

    Swift

    public var description: String { get }
  • Declaration

    Swift

    public init(start: Vector, end: Vector)
  • Returns a Line representation of this line segment, where the result’s a matches start and b matches end.

    Declaration

    Swift

    var asLine: Line<Vector> { get }
  • Returns a Ray representation of this line segment, where the result’s start matches start and b matches end.

    Declaration

    Swift

    var asRay: Ray<Vector> { get }
  • Returns a new line segment that has the start and end points of this line, in reverse order.

    Declaration

    Swift

    @inlinable
    var reversed: `Self` { get }

Available where Vector: VectorComparable

  • Returns the minimal AABB capable of containing this line segment’s points.

    Declaration

    Swift

    public var bounds: AABB<Vector> { get }

Available where Vector: VectorAdditive

  • Declaration

    Swift

    public func offsetBy(_ vector: Vector) -> LineSegment<Vector>

Available where Vector: VectorMultiplicative

Available where Vector: VectorDivisible

  • Gets the center point of this line segment.

    Declaration

    Swift

    public var center: Vector { get }

Available where Vector: VectorFloatingPoint

  • Returns the length of this line.

    Seealso

    lengthSquared

    Declaration

    Swift

    public var length: Scalar { get }
  • Returns a DirectionalRay representation of this ray, where the result’s start matches start and direction matches (end - start).normalized().

    Precondition

    (self.end - self.start).length > 0

    Declaration

    Swift

    public var asDirectionalRay: DirectionalRay<Vector> { get }
  • Returns true for projected scalars (0-1), which describes a line segment.

    Declaration

    Swift

    public func containsProjectedNormalizedMagnitude(
        _ scalar: Vector.Scalar
    ) -> Bool
  • Returns a projected normalized magnitude that is guaranteed to be contained in this line.

    For LineSegment, this is a clamped inclusive (0-1) range.

    Declaration

    Swift

    public func clampProjectedNormalizedMagnitude(
        _ scalar: Vector.Scalar
    ) -> Vector.Scalar
  • Returns the squared distance between this line and a given vector.

    The projected point on which the distance is taken is capped between the start and end points.

    let line = LineSegment2D(x1: 1, y1: 1, x2: 3, y2: 1)
    let point1 = Vector2D(x: 0, y: 0)
    let point2 = Vector2D(x: 2, y: 0)
    let point3 = Vector2D(x: 4, y: 0)
    
    print(line.distanceSquared(to: point1)) // Prints "2"
    print(line.distanceSquared(to: point2)) // Prints "1"
    print(line.distanceSquared(to: point3)) // Prints "2"
    

    Declaration

    Swift

    @inlinable
    public func distanceSquared(to vector: Vector) -> Scalar