Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Ray

A line of infinite length. A ray has a start point (from) and a direction (direction) but no end point.

In general, the ray is considered infinite in both directions, meaning it shoots both forward and back. Certain methods accept RayRange as a parameter, which allows you to specify whether the ray should shoot in both directions or only forward.

Example

import { Point, Ray, Vector } from 'shapetypes';

// Create a new ray
const ray = new Ray(new Point(3,4), new Vector(1,0));

// Get properties of the ray
console.log(ray.from.toString());
// => (3,4)
console.log(ray.direction.toString());
// => ⟨1,0⟩

// Get a point on the ray
const p = ray.pointAt(5);
console.log(p.toString());
// => (8,4)

// Get parameter of intersection between point and ray
console.log(ray.intersection(p));
// => [5]

// Translate the ray
const transformed = ray.translate(new Vector(3,4));
console.log(transformed.from.toString());
// => (6,8)

Hierarchy

Index

Constructors

constructor

Accessors

direction

  • Gets the direction that the ray shoots. Is always a unit vector.

    Returns Vector

from

Create Methods

Static fromPoints

  • Creates a new ray from two points.

    Parameters

    • from: Point

      The start of the ray.

    • pointOnRay: Point

      A point on the ray. The direction of the ray will start at from and point towards pointOnRay.

    Returns Ray

Other Methods

closestParameter

  • Finds the closest point on the ray and returns the parameter for the point.

    Parameters

    • testPoint: Point

      The target to get closest to.

    • Default value range: RayRange = RayRange.both

      The extent of the ray. Specifies whether the ray is shooting both forward and backward, or only forward.

    Returns number

    The parameter of the closest point. The parameter is the distance between from and the closest point. If the value is negative, it means the closest point is in the opposite direction to the direction vector. Entering the parameter into pointAt will return the closest point.

closestPoint

  • Finds the closest point on the ray and returns the point.

    Parameters

    • testPoint: Point

      The target to get closest to.

    • Default value range: RayRange = RayRange.both

      The extent of the ray. Specifies whether the ray is shooting both forward and backward, or only forward.

    Returns Point

equals

  • equals(otherRay: Ray, tolerance?: number): boolean
  • Checks whether another ray has the same from point and direction. Returns true if it does.

    Parameters

    • otherRay: Ray

      The ray to compare against.

    • Default value tolerance: number = shapetypesSettings.absoluteTolerance

      The amount the point and vector can differ and still be considered equal.

    Returns boolean

intersection

  • Calculates where the ray intersects other geometry and returns the parameters for these points of intersection.

    note

    This is an alias for the Intersection.ray function.

    note

    Only accounts for crossings, not coincident overlaps.

    Parameters

    Returns keyof number[]

    The parameters of the intersection points. The array is always sorted from the smallest to largest parameter. The parameter is the distance between from and the intersection point. If the value is negative, it means the point of intersection is in the opposite direction to the direction vector. Entering the parameter into pointAt will return the intersection point.

pointAt

  • pointAt(distance: number): Point
  • Finds the point a given distance from the ray's start (from). Returns the point.

    Parameters

    • distance: number

      The distance from the from point. If positive, it will be in the direction of direction. If negative, it will be in the inverse.

    Returns Point

toString

  • toString(): string
  • Gets the ray as a string in the format: [from,direction].

    Returns string

withDirection

withFrom

  • Creates a copy of the ray with a different from point.

    Parameters

    • newFrom: Point

      The new location of the from point.

    Returns Ray

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 ray by a Transform matrix and returns the result.

    Example

    import { Point, Ray, Transform, Vector } from 'shapetypes';
    
    // Create a new ray
    const ray = new Ray(new Point(2,2), Vector.worldX());
    console.log(ray.from.toString());
    // => (2,2)
    
    // Translate ray using a transform matrix
    const matrix = Transform.translate(new Vector(3,4));
    const moved = ray.transform(matrix);
    console.log(moved.from.toString());
    // => (5,6)
    
    // Translate ray using the direct method
    const otherMoved = ray.translate(new Vector(3,4));
    console.log(otherMoved.from.toString());
    // => (5,6)
    note

    If you're applying the same transformation to 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