Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Line

A straight edge between two points.

Example

import { Line, Point, Transform, Vector } from 'shapetypes';

// Create a new line
const line = new Line(new Point(1,1), new Point(4,5));

// Get properties of the line
console.log(line.from.toString());
// => (1,1)
console.log(line.to.toString());
// => (4,5)
console.log(line.length);
// => 5
const mid = line.pointAt(0.5);
console.log(mid.toString());
// => (2.5,3)

// Find closest point on the line
const closest = line.closestPoint(new Point(5,5));
console.log(closest.toString());
// => (4,5)

// Translate the line with a transform matrix
const matrix = Transform.translate(new Vector(5, 0));
const moved = line.transform(matrix);
console.log(moved.toString());
// => [(6,1),(9,5)]

Hierarchy

Index

Constructors

constructor

Accessors

boundingBox

direction

  • Gets the vector running the length of the line, starting at from and ending at to. The length of the vector is the length of the line.

    Returns Vector

from

length

  • get length(): number

to

unitTangent

  • Gets the line's tangent vector. This vector is perpendicular to direction and is a unit vector.

    If the environment's y-axis points upwards, it will be on the left side of the line if looking from -> to. If the environment's y-axis points downwards, it will be on the right side of the line if looking from -> to.

    Returns Vector

Create Methods

Static fromCoords

  • fromCoords(coords: keyof [keyof [number, number], keyof [number, number]]): Line
  • Creates a line from a set of coordinates.

    Parameters

    • coords: keyof [keyof [number, number], keyof [number, number]]

      A 2x2 array of values specifying the line's start and end point. Format: [[x1,y1],[x2,y2]].

    Returns Line

Static fromVector

  • fromVector(from: Point, direction: Vector, length?: undefined | number): Line
  • Creates a line from a start point and a vector.

    Parameters

    • from: Point

      The start of the line.

    • direction: Vector

      The direction of the line.

    • Optional length: undefined | number

      The length of the line. If undefined, it will use the length of the direction vector.

    Returns Line

Other Methods

closestParameter

  • closestParameter(testPoint: Point, limitToFiniteSegment?: boolean): number
  • Finds the closest point on the line and returns the parameter for the point.

    Parameters

    • testPoint: Point

      The target to get closest to.

    • Default value limitToFiniteSegment: boolean = true

      If true, the closest point will always be within the bounds of the line. If false, the line is treated as infinite.

    Returns number

    The normalized parameter of the closest point. Entering the parameter into pointAt will return the closest point.

closestPoint

  • closestPoint(testPoint: Point, limitToFiniteSegment?: boolean): Point
  • Finds the closest point on the line and returns the point.

    Parameters

    • testPoint: Point

      The target to get closest to.

    • Default value limitToFiniteSegment: boolean = true

      If true, the closest point will always be within the bounds of the line. If false, the line is treated as infinite.

    Returns Point

distanceTo

  • distanceTo(geometry: Point | Line, limitToFiniteSegment?: boolean): number
  • Calculates the smallest distance to a point or line.

    Parameters

    • geometry: Point | Line

      Line or point to measure the distance to.

    • Default value limitToFiniteSegment: boolean = true

      If false, the line is treated as infinite.

    Returns number

equals

  • equals(otherLine: Line, tolerance?: number): boolean
  • Checks whether another line has the same start and end points. Returns true if it does.

    Parameters

    • otherLine: Line

      The line to compare against.

    • Default value tolerance: number = shapetypesSettings.absoluteTolerance

      The amount that the points can differ and still be considered equal.

    Returns boolean

extend

  • extend(fromDistance: number, toDistance: number): Line
  • Lengthens the line by extending the end points. Returns the resulting line.

    Parameters

    • fromDistance: number

      The distance to move from. If 0, from will remain in place. If greater than 0, the line will lengthen.

    • toDistance: number

      The distance to move to. If 0, to will remain in place. If greater than 0, the line will lengthen.

    Returns Line

flip

intersection

pointAt

  • pointAt(u: number, limitToFiniteSegment?: boolean): Point
  • Finds the point a normalized parameter along the line. Returns the point.

    The parameter ranges from 0, which is the line's start (from), through to 1, which is the line's end (to). The mid point of the line is 0.5.

    Example

    let line = new Line(new Point(0, 0), new Point(10, 0));
    console.log(line.pointAt(0).toString());
    // => (0,0)
    console.log(line.pointAt(0.5).toString());
    // => (5,0)
    console.log(line.pointAt(1).toString());
    // => (10,0)

    Parameters

    • u: number

      The normalized parameter.

    • Default value limitToFiniteSegment: boolean = true

      If true, the point will always be within the bounds of the line (u-values less than 0 will become 0 and values greater than 1 will become 1). If false, the line is treated as infinite.

    Returns Point

pointAtLength

  • pointAtLength(distance: number, limitToFiniteSegment?: boolean): Point
  • Finds the point a given distance from the line's start (from) and returns the point.

    Parameters

    • distance: number

      The distance from the from point.

    • Default value limitToFiniteSegment: boolean = true

      If true, the point will always be within the bounds of the line. If false, the line is treated as infinite.

    Returns Point

toString

  • toString(): string
  • Gets the line as a string in the format: [(x,y),(x,y)].

    Returns string

withFrom

withLength

  • withLength(distance: number): Line
  • Creates a copy of the line with a different length. The from point will remain fixed in place and the to point will be moved to make the line the right length.

    Parameters

    • distance: number

      New length for the line. If the length is set to a negative number, the line's direction will be reversed but the length will remain positive.

    Returns Line

withTo

Transform Methods

changeBasis

  • changeBasis(planeFrom: Plane, planeTo: Plane): this
  • Translates the geometry from one coordinate system to another while keeping the geometry in the same position. In other words, if the geometry is currently described relative to planeFrom, after changeBasis, it will be in the same position but described relative to planeTo.

    see

    Transform.changeBasis.

    Parameters

    • planeFrom: Plane

      The coordinate system the geometry is currently described relative to.

    • planeTo: Plane

      The coordinate system to describe the geometry relative to.

    Returns this

    The geometry in the new coordinate system.

planeToPlane

  • planeToPlane(planeFrom: Plane, planeTo: Plane): this

rotate

  • rotate(angle: number): this
  • rotate(angle: number, pivot: Point): this
  • Rotates the geometry about (0,0).

    see

    Transform.rotate.

    Parameters

    • angle: number

      Angle to rotate the geometry (in radians). The direction is counter-clockwise.

    Returns this

  • Rotates the geometry about a point.

    see

    Transform.rotate.

    category

    Transform

    Parameters

    • angle: number

      Angle to rotate the geometry (in radians). If the environment's y-axis points upwards, the direction is counter-clockwise.

    • pivot: Point

      Point to pivot the geometry about.

    Returns this

scale

  • scale(amount: number): this
  • scale(x: number, y: number): this
  • scale(x: number, y: number, center: Point): this
  • Scales the geometry and returns the resized geometry. The geometry will be scaled about (0,0), meaning everything will shrink or expand away from this point.

    see

    Transform.scale.

    Parameters

    • amount: number

      Magnitude to scale in x- and y-direction. If less than 1, the object will shrink. If greater than 1, it will grow.

    Returns this

  • Scales the geometry and returns the resized geometry. The geometry will be scaled about (0,0), meaning everything will shrink or expand away from this point.

    see

    Transform.scale.

    category

    Transform

    Parameters

    • x: number

      Magnitude to scale in x-direction. If less than 1, the object will shrink. If greater than 1, it will grow.

    • y: number

      Magnitude to scale in y-direction. If less than 1, the object will shrink. If greater than 1, it will grow.

    Returns this

  • Scales the geometry about a point and returns the resized geometry.

    see

    Transform.scale.

    category

    Transform

    Parameters

    • x: number

      Magnitude to scale in x-direction. If less than 1, the object will shrink. If greater than 1, it will grow.

    • y: number

      Magnitude to scale in y-direction. If less than 1, the object will shrink. If greater than 1, it will grow.

    • center: Point

      Center of scaling. Everything will shrink or expand away from this point.

    Returns this

transform

  • Transforms the line by a transform matrix and returns the result.

    Example

    import { Line, Point, Transform, Vector } from 'shapetypes';
    
    // Create a line
    const line = new Line(new Point(0,0), new Point(10,0));
    console.log(line.toString());
    // => [(0,0),(10,0)]
    
    // Translate using a transform matrix
    const matrix = Transform.translate(new Vector(5, 4));
    const moved = line.transform(matrix);
    console.log(moved.toString());
    // => [(5,4),(15,4)]
    
    // Translate using the direct method
    const otherMoved = line.translate(5, 4);
    console.log(otherMoved.toString());
    // => [(5,4),(15,4)]
    note

    If you're applying the same transformation a lot of geometry, creating the Transform matrix once and calling this function is faster than using the direct methods.

    Parameters

    Returns this

translate

  • translate(move: Vector): this
  • translate(move: Vector, distance: number | undefined): this
  • Moves the geometry along a vector and returns the moved geometry. The translation is always linear.

    see

    Transform.translate.

    Parameters

    • move: Vector

      The direction and distance to move the geometry.

    Returns this

  • Moves the geometry along a vector and returns the moved geometry. The translation is always linear.

    see

    Transform.translate.

    category

    Transform

    Parameters

    • move: Vector

      The direction to move the geometry.

    • distance: number | undefined

      The distance to move the geometry.

    Returns this

Generated using TypeDoc