Class: Line

Sylvester version: 0.1.0 onwards

Class methods: create, X, Y, Z

Instance methods: contains, distanceFrom, dup, eql, intersectionWith, intersects, isParallelTo, liesIn, pointClosestTo, positionOf, reflectionIn, reverse, rotate, setVectors, translate

Instance variables:

  • anchor – a 3D Vector corresponding to a point on the line
  • direction – a normalized 3D Vector representing the line’s direction


The Line class is designed to model infinite straight lines in 3 dimensions. It is possible to specify lines using 2D Vectors, but they will be stored in the Line instance as 3D vectors whose third element is zero. Lines created in this way are thus 3D lines confined to the x-y plane. You can also specify 2D vectors as arguments for many of Line’s instance methods – again, these will be interpreted as 3D vectors with a zero third component.

Class methods

Line.create(anchor, direction) 0.1.0

Creates a Line instance with the specified properties. anchor and direction can each be either 2- or 3-dimensional arrays or Vectors, and they will be stored in the Line’s properties as 3D vectors. (The third element will be zero if a 2D vector was supplied.) direction will be normalized before being saved. The following are all fine:

var A = Line.create([4,8], [1,5]);
var B = Line.create($V([4,8]), $V([1,5]));
var C = Line.create([9,2,5], $V([8,2,0]));

For situations where x, y and z are used to refer to co-ordinates, x corresponds to the first element of a vector, y the second and z the third.

This method is aliased as $L.

Line.X, Line.Y, Line.Z 0.1.0

Predefined Line instances representing the x, y and z axes respectively.

Instance methods

contains(point) 0.1.0

Returns true iff the vector point is a point that lies on the receiver.

distanceFrom(obj) 0.1.0

Returns the shortest distance between the receiver and obj, which can be a Line, a Plane or a Vector. If a Plane, this method will only return a non-zero value if the receiver is parallel to obj.

dup() 0.1.0

Returns a copy of the receiver.

eql(line) 0.1.0

Returns true iff line is equal to the receiver, that is, if they both represent the same region of space. Their anchor and direction properties do not have to be identical for this to be the case. As long as line.anchor is a point on the receiver, and line.direction is (anti)parallel to the receiver’s direction, then this method returns true.

intersectionWith(obj) 0.1.0

Returns a Vector representing the unique point of intersection of the receiver with obj, which can be either a Line or a Plane. If no such point exists, returns null.

intersects(obj) 0.1.0

Returns true iff the receiver has a unique point of intersection with obj, which can be either a Line or a Plane.

isParallelTo(line) 0.1.0

Returns true iff the receiver and line are parallel lines. Their direction vectors can point in opposite directions – two lines with opposing directions represent the same set of points.

liesIn(plane) 0.1.0

Returns true iff the receiver lies in the given plane.

pointClosestTo(obj) 0.1.0

Returns a Vector representing the point on the receiver that is closest to obj, which can be either a Vector or a Line. If a Line, returns null if the lines are parallel – there is no unique closest point.

reflectionIn(obj) 0.1.0

Returns a Line representing the result of reflecting (inverting) the receiver in obj, which can be a Line, a Plane or a Vector.

rotate(angle, axis) 0.1.0

Returns the result of rotating the receiver by angle radians about axis, which can be a Line, or a Vector. If a Vector, the receiver is rotated about a line whose anchor is axis and whose direction is [0, 0, 1]. This is useful for working in 2D:

var L = Line.X.rotate(Math.PI/2, $V([5,0]));

var test = L.eql(Line.create([5,0], [0,1]));
// Returns true - the 90-degree rotation leaves Line.X parallel to the y axis

Be careful when working with axis as a Line – rotations are performed in a right-handed fashion about the line’s direction.

setVectors(anchor, direction) 0.1.0

Sets the receiver’s properties accordingly. See create.

translate(vector) 0.1.0

Returns the result of translating the receiver by adding vector to its anchor property. vector can be a 2- or 3- dimensional array or Vector. If 2-dimensional, a zero third component will be added.