segment2

v0.1.0
2d line segment
2d line segment line segment geometry

segment2

2d line segment

install

npm install segment2

or include it in the browser

use


var Segment2 = require('segment2');
var Vec2 = require('vec2');

var s = new Segment2(Vec2(0, 0), Vec2(10, 0));

console.log(s.midpoint().toString()); // (5, 0)

console.log(s.intersect(Segment2(Vec2(5, 10), Vec2(5, -10))).toString()) // (5, 0)

properties

.start - the starting point of the line

.end - the ending point of the line

methods

fn change([fn])

Add a listener to be called when the line changes. Returns fn

callback signature: function(segment2, vec2) {} where segment2 is the segment that changed and vec2 is the component that changed

ignore([fn])

Remove a listener, or all of them if fn is not passed

clone()

Create a copy of this segment.

note: this function uses this.constructor to create clones of subclasses.

Pass segmentCtor if you have subclassed Segment2

Pass vecCtor if you have subclassed Vec2

length()

return the length of the line segment

lengthSquared()

return the length of this line segment (squared)

closestPointTo(vec2)

return the closest point on this segment to the passed vec2

containsPoint(vec2)

return true if vec2 lies on the segment

midpoint()

return the vec2 equidistant from segment2.start and segment.end

slope()

return the computed slope of this line segment.

note: this function will return Infinity for vertical lines

rotate(rads[, origin [, returnNew]])

apply a rotation to the current start/end points around origin. If origin is not passed the midpoint is used. If returnNew is truthy, a new Segment2 instance will be returnd instead of applying the result on this

note: calling it like seg.rotate(Math.PI, true) is also valid and will return a copy of seg rotated around seg's midpoint.

collinear(seg)

returns true when the passed seg is collinear with this

parallel(seg)

returns true when the passed seg is parallel with this

license

MIT (see: license.txt)

Metadata

  • MIT
  • Whatever
  • Elijah Insua
  • released 2/5/2014

Downloads

Maintainers