Reference

std/geometry/ray2

std/geometry/src/ray2.trb

Ray2, the half-line in the plane, and the hit tests written against it: the shape a pick, a line of sight and a one-step sweep are all asked in terms of.

type Ray2

type Ray2<Scalar: Real = Float>

A half-line: where it starts and which way it points.

Every hit test answers how far along the ray the hit is, as a distance in the length of direction, and not the point itself: a caller that wants the nearest of several hits compares those numbers, and a caller that wants the point asks Ray2.at for it. A direction of length one therefore makes every answer a distance.

Examples

const look = Ray2 Vector2(0.0, 0.0), Vector2(1.0, 0.0)
print look.hits(Circle(Vector2(5.0, 0.0), 1.0))

Pitfalls

  • A direction of zero has no hits at all, and every test answers None for it rather than panicking.
  • A ray that starts inside a shape answers 0, not the exit distance. A caller that wants the exit asks the shape.

Related

  • Segment2 - the piece of line with two ends.
  • Ray3 - the same in space.

field origin

origin: Vector2<Scalar>

Where the ray starts.

field direction

direction: Vector2<Scalar>

Which way it points. The hit distances are measured in this vector's length.

fn towards

static fn towards(from: Vector2<Scalar>, to: Vector2<Scalar>): Ray2<Scalar>

The ray from one point towards another, with a direction of length one.

fn at

fn at(distance: Scalar): Vector2<Scalar>

The point that far along the ray. A negative distance reads backwards, behind the origin.

fn hits

fn hits(circle: Circle<Scalar>): Scalar?

How far along the ray the disc is first met, or None where the ray misses it. A ray that starts inside answers zero.

fn hitsRectangle

fn hitsRectangle(rectangle: Rectangle<Scalar>): Scalar?

How far along the ray the rectangle is first met, or None where the ray misses it. This is the slab test: the ray enters the rectangle where the last axis lets it in and leaves where the first axis shuts it out.

fn hitsSegment

fn hitsSegment(segment: Segment2<Scalar>): Scalar?

How far along the ray the segment is met, or None where the ray misses it.