Path: blob/trunk/third_party/closure/goog/math/line.js
2868 views
// Copyright 2008 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.131415/**16* @fileoverview Represents a line in 2D space.17*18* @author [email protected] (Robby Walker)19*/2021goog.provide('goog.math.Line');2223goog.require('goog.math');24goog.require('goog.math.Coordinate');25262728/**29* Object representing a line.30* @param {number} x0 X coordinate of the start point.31* @param {number} y0 Y coordinate of the start point.32* @param {number} x1 X coordinate of the end point.33* @param {number} y1 Y coordinate of the end point.34* @struct35* @constructor36* @final37*/38goog.math.Line = function(x0, y0, x1, y1) {39/**40* X coordinate of the first point.41* @type {number}42*/43this.x0 = x0;4445/**46* Y coordinate of the first point.47* @type {number}48*/49this.y0 = y0;5051/**52* X coordinate of the first control point.53* @type {number}54*/55this.x1 = x1;5657/**58* Y coordinate of the first control point.59* @type {number}60*/61this.y1 = y1;62};636465/**66* @return {!goog.math.Line} A copy of this line.67*/68goog.math.Line.prototype.clone = function() {69return new goog.math.Line(this.x0, this.y0, this.x1, this.y1);70};717273/**74* Tests whether the given line is exactly the same as this one.75* @param {goog.math.Line} other The other line.76* @return {boolean} Whether the given line is the same as this one.77*/78goog.math.Line.prototype.equals = function(other) {79return this.x0 == other.x0 && this.y0 == other.y0 && this.x1 == other.x1 &&80this.y1 == other.y1;81};828384/**85* @return {number} The squared length of the line segment used to define the86* line.87*/88goog.math.Line.prototype.getSegmentLengthSquared = function() {89var xdist = this.x1 - this.x0;90var ydist = this.y1 - this.y0;91return xdist * xdist + ydist * ydist;92};939495/**96* @return {number} The length of the line segment used to define the line.97*/98goog.math.Line.prototype.getSegmentLength = function() {99return Math.sqrt(this.getSegmentLengthSquared());100};101102103/**104* Computes the interpolation parameter for the point on the line closest to105* a given point.106* @param {number|goog.math.Coordinate} x The x coordinate of the point, or107* a coordinate object.108* @param {number=} opt_y The y coordinate of the point - required if x is a109* number, ignored if x is a goog.math.Coordinate.110* @return {number} The interpolation parameter of the point on the line111* closest to the given point.112* @private113*/114goog.math.Line.prototype.getClosestLinearInterpolation_ = function(x, opt_y) {115var y;116if (x instanceof goog.math.Coordinate) {117y = x.y;118x = x.x;119} else {120y = opt_y;121}122123var x0 = this.x0;124var y0 = this.y0;125126var xChange = this.x1 - x0;127var yChange = this.y1 - y0;128129return ((Number(x) - x0) * xChange + (Number(y) - y0) * yChange) /130this.getSegmentLengthSquared();131};132133134/**135* Returns the point on the line segment proportional to t, where for t = 0 we136* return the starting point and for t = 1 we return the end point. For t < 0137* or t > 1 we extrapolate along the line defined by the line segment.138* @param {number} t The interpolation parameter along the line segment.139* @return {!goog.math.Coordinate} The point on the line segment at t.140*/141goog.math.Line.prototype.getInterpolatedPoint = function(t) {142return new goog.math.Coordinate(143goog.math.lerp(this.x0, this.x1, t), goog.math.lerp(this.y0, this.y1, t));144};145146147/**148* Computes the point on the line closest to a given point. Note that a line149* in this case is defined as the infinite line going through the start and end150* points. To find the closest point on the line segment itself see151* {@see #getClosestSegmentPoint}.152* @param {number|goog.math.Coordinate} x The x coordinate of the point, or153* a coordinate object.154* @param {number=} opt_y The y coordinate of the point - required if x is a155* number, ignored if x is a goog.math.Coordinate.156* @return {!goog.math.Coordinate} The point on the line closest to the given157* point.158*/159goog.math.Line.prototype.getClosestPoint = function(x, opt_y) {160return this.getInterpolatedPoint(161this.getClosestLinearInterpolation_(x, opt_y));162};163164165/**166* Computes the point on the line segment closest to a given point.167* @param {number|goog.math.Coordinate} x The x coordinate of the point, or168* a coordinate object.169* @param {number=} opt_y The y coordinate of the point - required if x is a170* number, ignored if x is a goog.math.Coordinate.171* @return {!goog.math.Coordinate} The point on the line segment closest to the172* given point.173*/174goog.math.Line.prototype.getClosestSegmentPoint = function(x, opt_y) {175return this.getInterpolatedPoint(176goog.math.clamp(this.getClosestLinearInterpolation_(x, opt_y), 0, 1));177};178179180