line2
install
npm install line2
or include it in a script tag in the browser
use
var Line = ; var l = 0 0 10 10; console; // 1 0 0
api surface
Line2(x1, y1, x2, y2) or Line2(slope, yIntercept)
returns a new Line2
change(fn)
add an observer that will be notified of any change.
callback signature: function(line) {}
ignore([fn])
Removes a listener, if no fn
is passed remove them all.
notify()
Call all of the listeners, manually.
yintercept([y])
return the computed y-intercept or null
if verical
When y
is passed it will set the y-intercept of this line and the x-intercept if appropriate (not horizontal)
xintercept([x])
return the computed x-intercept or null
if horizontal
when x
is passed it will set the x-intercept of this line and the y-intercept if appropriate (not vertical)
slope([slope])
return the computed slope.
When slope
is passed it will set the slope of this line
note: this will be Infinity
if the line is vertical
intersectSegment(x1, y1, x2, y2)
returns:
true
when the segment is colinear with this linefalse
when the segment does not intersect with this lineVec2
representing where the point of interesection
createPerpendicular(vec2)
returns a new line instance that is perpendicular to this line and goes through the provided point
intersectCircle(vec2, radius)
returns an array of Vec2
s which represent the intersections.
a length of:
0
means no intersections1
means the line is tangent to the provided circle2
means the line fully intersects
solveForX(y)
return the x
coordinate using the provided y
solveForY(x)
return the y
coordinate using the provided x
intersect(line2) or intersect(x1, y1, x2, y2)
returns a Vec2
representing the point of intersection if there was one.
If the lines are colinear, it returns true
If there is no intersection, it returns false
isHorizontal()
return boolean
isVertical()
return boolean
closestPointTo(vec2)
returns the a Vec2
representing the closest point on this line to the provided vec2
containsPoint(vec2)
returns true if the provided vec2 is on this line
license
MIT (see: license.txt)