Skip to content

RayIntersection and SegmentIntersection

RayIntersection and SegmentIntersection (both from scenerystack/kite) are small, immutable data classes describing the result of two different kinds of intersection query. RayIntersection describes where a Ray2 crosses a Segment or Shape — the result of segment.intersection( ray ) or shape.intersection( ray ). SegmentIntersection describes where two segments cross each other — the result of Segment.intersect( a, b ), used internally by shape simplification and boolean operations.

ts
import { Shape, Segment } from 'scenerystack/kite';
import { Ray2, Vector2 } from 'scenerystack/dot';

const shape = Shape.circle( 0, 0, 50 );
const ray = new Ray2( new Vector2( -100, 0 ), new Vector2( 1, 0 ) );

const hits = shape.intersection( ray ); // RayIntersection[]
hits[ 0 ].point;    // Vector2(-50, 0) - where the ray first crosses the circle
hits[ 0 ].distance; // 50 - distance from the ray's origin to that point

RayIntersection

One crossing point between a ray and a single segment (a Shape's intersection( ray ) collects these across every segment in every subpath).

FieldMeaning
pointThe Vector2 location of the intersection
distanceDistance from the ray's origin to point (always >= 0)
normalUnit normal to the segment at the intersection, oriented so its dot product with the ray's direction is <= 0
wind1 or -1 — the winding contribution of this crossing, used by the non-zero fill rule that backs Shape.containsPoint()
tThe parametric value (0 to 1) along the specific segment where the crossing occurs

RayIntersection is a plain constructed value (new RayIntersection( distance, point, normal, wind, t )) — you receive instances of it as return values; you won't typically construct one yourself.

SegmentIntersection

One crossing point between two arbitrary segments, as returned by the static Segment.intersect( a, b ).

FieldMeaning
pointThe Vector2 location of the intersection
aTParametric value (0 to 1) along the first segment (a) where the crossing occurs
bTParametric value (0 to 1) along the second segment (b) where the crossing occurs
getSwapped()Returns an equivalent SegmentIntersection with aT/bT swapped — useful if you called Segment.intersect( a, b ) but want the result as if you'd called Segment.intersect( b, a )
ts
import { Segment, Shape } from 'scenerystack/kite';

const line = Shape.lineSegment( 0, -50, 0, 50 ).subpaths[ 0 ].segments[ 0 ];
const circleArc = Shape.circle( 0, 0, 30 ).subpaths[ 0 ].segments[ 0 ];

const crossings = Segment.intersect( line, circleArc ); // SegmentIntersection[]

These describe where two things cross — not whether a shape contains a point

shape.containsPoint( point ) and boolean shape operations (Shape.union/intersection/xor, see Graph and Boolean Shape Operations) are built out of exactly these intersection primitives internally (RayIntersection.wind for hit-testing, SegmentIntersection for splitting overlapping segments during simplification) — but as an API consumer you'd reach for RayIntersection/SegmentIntersection directly only for a custom geometry query (e.g. "where along this path does a laser beam first hit"), not for ordinary hit-testing or shape combination, which already have dedicated, higher-level methods.