Reference

std/geometry/segment2

std/geometry/src/segment2.trb

Segment2, the straight piece of line between two points in the plane: an edge of a polygon, a wall, a swept step.

type Segment2

type Segment2<Scalar: Numeric = Float>

A straight piece of line, from one point to another. Both ends belong to it.

Examples

const wall = Segment2 Vector2(0.0, 0.0), Vector2(4.0, 0.0)
print "{wall.step()} {wall.closestPoint(to: Vector2(2.0, 3.0))}"

Pitfalls

  • A segment whose two ends are the same point is a point, and every test here answers what it should for one - but Segment2.direction has no answer, so it answers a zero vector.

Related

  • Ray2 - the half-line, which is what a hit test is usually written against.
  • Polygon - what a sequence of these bounds.

field from

from: Vector2<Scalar>

Where the segment begins.

field to

to: Vector2<Scalar>

Where the segment ends.

fn step

fn step(): Vector2<Scalar>

The step from one end to the other.

fn sideOf

fn sideOf(point: Vector2<Scalar>): Scalar

Which side of the segment's line the point is on: positive on the left of the step, zero exactly on the line.

fn bounds

fn bounds(): Rectangle<Scalar>

The smallest rectangle that holds the segment. Its size is zero on an axis the segment does not cross.

fn intersects

fn intersects(other: Segment2<Scalar>): Bool

Whether the two segments cross, which includes an end lying on the other segment.

Two segments that lie on one line count as crossing only where they overlap, which is the case a sign test alone cannot answer - so that case is decided by the bounding rectangles.

extend Segment2<Scalar>

extend<Scalar: Signed> Segment2<Scalar>

What a sign buys: the length along the axes.

fn manhattanLength

fn manhattanLength(): Scalar

The length of the step along the axes, |x| + |y|.

extend Segment2<Scalar>

extend<Scalar: Real> Segment2<Scalar>

What a root buys: lengths, the nearest point and the crossing point.

fn length

fn length(): Scalar

How long the segment is.

fn direction

fn direction(): Vector2<Scalar>

The direction from one end to the other, with length one. A segment of no length answers a zero vector.

fn center

fn center(): Vector2<Scalar>

The midpoint.

fn at

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

The point factor of the way along the segment: 0 is from and 1 is to.

fn closestPoint

fn closestPoint(to: Vector2<Scalar>): Vector2<Scalar>

The point of the segment that is nearest to the one given.

fn distanceTo

fn distanceTo(point: Vector2<Scalar>): Scalar

The distance from the point to the nearest point of the segment.

fn intersection

fn intersection(other: Segment2<Scalar>): Vector2<Scalar>?

Where the two segments cross, or None where they do not cross at all or lie on one line.

const across = Segment2 Vector2(0.0, -1.0), Vector2(0.0, 1.0)
print Segment2(Vector2(-1.0, 0.0), Vector2(1.0, 0.0)).intersection(across)