/*
 * Paper.js
 *
 * This file is part of Paper.js, a JavaScript Vector Graphics Library,
 * based on Scriptographer.org and designed to be largely API compatible.
 * http://paperjs.org/
 * http://scriptographer.org/
 *
 * Distributed under the MIT license. See LICENSE file for details.
 *
 * Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
 * http://lehni.org/ & http://jonathanpuckey.com/
 *
 * All rights reserved.
 */

var Line = this.Line = Base.extend({
	/** @lends Line# */
	
	// DOCS: document Line class and constructor
	/**
	 * Creates a Line object.
	 * 
	 * @param {Point} point1
	 * @param {Point} point2
	 * @param {Boolean} [infinite=true]
	 * 
	 * @class The Line object represents..
	 * @constructs Line
	 */
	initialize: function(point1, point2, infinite) {
		// Convention: With 3 parameters, both points are absolute, and infinite
		// controls wether the line extends beyond the defining points, meaning
		// intersection outside the line segment are allowed.
		// With two parameters, the 2nd parameter is a direction, and infinite
		// is automatially true, since we're describing an infinite line.
		point1 = Point.read(arguments, 0, 1);
		point2 = Point.read(arguments, 1, 1);
		if (arguments.length == 3) {
			this.point = point1;
			this.vector = point2.subtract(point1);
			this.infinite = infinite;
		} else {
			this.point = point1;
			this.vector = point2;
			this.infinite = true;
		}
	},

	/**
	 * The starting point of the line
	 *
	 * @name Line#point
	 * @type Point
	 */

	/**
	 * The vector of the line
	 *
	 * @name Line#vector
	 * @type Point
	 */

	/**
	 * Specifies whether the line extends infinitely
	 *
	 * @name Line#infinite
	 * @type Boolean
	 */

	/**
	 * @param {Line} line
	 * @return {Point} the intersection point of the lines
	 */
	intersect: function(line) {
		var cross = this.vector.cross(line.vector);
		// Epsilon tolerance
		if (Math.abs(cross) <= 10e-6)
			return null;
		var v = line.point.subtract(this.point),
			t1 = v.cross(line.vector) / cross,
			t2 = v.cross(this.vector) / cross;
		// Check the ranges of t parameters if the line is not allowed to
		// extend beyond the definition points.
		return (this.infinite || 0 <= t1 && t1 <= 1)
				&& (line.infinite || 0 <= t2 && t2 <= 1)
			? this.point.add(this.vector.multiply(t1)) : null;
	},

	// DOCS: document Line#getSide(point)
	/**
	 * @param {Point} point
	 * @return {Number}
	 */
	getSide: function(point) {
		var v1 = this.vector,
			v2 = point.subtract(this.point),
			ccw = v2.cross(v1);
		if (ccw == 0) {
			ccw = v2.dot(v1);
			if (ccw > 0) {
				ccw = (v2 - v1).dot(v1);
				if (ccw < 0)
				    ccw = 0;
			}
		}
		return ccw < 0 ? -1 : ccw > 0 ? 1 : 0;
	}
});

@@ -1860,31 +1860,8 @@
 r);%0A
-%09%09// Epsilon tolerance%0A
 %09%09if
@@ -1885,13 +1885,27 @@
  %3C= 
-10e-6
+Numerical.TOLERANCE
 )%0A%09%09
@@ -2674,15 +2674,502 @@
  1 : 0;%0A
+%09%7D,%0A%0A%09// DOCS: document Line#getDistance(point)%0A%09/**%0A%09 * @param %7BPoint%7D point%0A%09 * @return %7BNumber%7D%0A%09 */%0A%09getDistance: function(point) %7B%0A%09%09var m = this.vector.y / this.vector.x, // slope%0A%09%09%09b = this.point.y - (m * this.point.x); // y offset%0A%09%09// Distance to the linear equation%0A%09%09var dist = Math.abs(point.y - (m * point.x) - b) / Math.sqrt(m * m + 1);%0A%09%09return this.infinite ? dist : Math.min(dist,%0A%09%09%09%09point.getDistance(this.point),%0A%09%09%09%09point.getDistance(this.point.add(this.vector)));%0A
 %09%7D%0A%7D);%0A

Define Line#getDistance(point).