line.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Copyright 2008 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Represents a line in 2D space.
  16. *
  17. * @author robbyw@google.com (Robby Walker)
  18. */
  19. goog.provide('goog.math.Line');
  20. goog.require('goog.math');
  21. goog.require('goog.math.Coordinate');
  22. /**
  23. * Object representing a line.
  24. * @param {number} x0 X coordinate of the start point.
  25. * @param {number} y0 Y coordinate of the start point.
  26. * @param {number} x1 X coordinate of the end point.
  27. * @param {number} y1 Y coordinate of the end point.
  28. * @struct
  29. * @constructor
  30. * @final
  31. */
  32. goog.math.Line = function(x0, y0, x1, y1) {
  33. /**
  34. * X coordinate of the first point.
  35. * @type {number}
  36. */
  37. this.x0 = x0;
  38. /**
  39. * Y coordinate of the first point.
  40. * @type {number}
  41. */
  42. this.y0 = y0;
  43. /**
  44. * X coordinate of the first control point.
  45. * @type {number}
  46. */
  47. this.x1 = x1;
  48. /**
  49. * Y coordinate of the first control point.
  50. * @type {number}
  51. */
  52. this.y1 = y1;
  53. };
  54. /**
  55. * @return {!goog.math.Line} A copy of this line.
  56. */
  57. goog.math.Line.prototype.clone = function() {
  58. return new goog.math.Line(this.x0, this.y0, this.x1, this.y1);
  59. };
  60. /**
  61. * Tests whether the given line is exactly the same as this one.
  62. * @param {goog.math.Line} other The other line.
  63. * @return {boolean} Whether the given line is the same as this one.
  64. */
  65. goog.math.Line.prototype.equals = function(other) {
  66. return this.x0 == other.x0 && this.y0 == other.y0 && this.x1 == other.x1 &&
  67. this.y1 == other.y1;
  68. };
  69. /**
  70. * @return {number} The squared length of the line segment used to define the
  71. * line.
  72. */
  73. goog.math.Line.prototype.getSegmentLengthSquared = function() {
  74. var xdist = this.x1 - this.x0;
  75. var ydist = this.y1 - this.y0;
  76. return xdist * xdist + ydist * ydist;
  77. };
  78. /**
  79. * @return {number} The length of the line segment used to define the line.
  80. */
  81. goog.math.Line.prototype.getSegmentLength = function() {
  82. return Math.sqrt(this.getSegmentLengthSquared());
  83. };
  84. /**
  85. * Computes the interpolation parameter for the point on the line closest to
  86. * a given point.
  87. * @param {number|goog.math.Coordinate} x The x coordinate of the point, or
  88. * a coordinate object.
  89. * @param {number=} opt_y The y coordinate of the point - required if x is a
  90. * number, ignored if x is a goog.math.Coordinate.
  91. * @return {number} The interpolation parameter of the point on the line
  92. * closest to the given point.
  93. * @private
  94. */
  95. goog.math.Line.prototype.getClosestLinearInterpolation_ = function(x, opt_y) {
  96. var y;
  97. if (x instanceof goog.math.Coordinate) {
  98. y = x.y;
  99. x = x.x;
  100. } else {
  101. y = opt_y;
  102. }
  103. var x0 = this.x0;
  104. var y0 = this.y0;
  105. var xChange = this.x1 - x0;
  106. var yChange = this.y1 - y0;
  107. return ((Number(x) - x0) * xChange + (Number(y) - y0) * yChange) /
  108. this.getSegmentLengthSquared();
  109. };
  110. /**
  111. * Returns the point on the line segment proportional to t, where for t = 0 we
  112. * return the starting point and for t = 1 we return the end point. For t < 0
  113. * or t > 1 we extrapolate along the line defined by the line segment.
  114. * @param {number} t The interpolation parameter along the line segment.
  115. * @return {!goog.math.Coordinate} The point on the line segment at t.
  116. */
  117. goog.math.Line.prototype.getInterpolatedPoint = function(t) {
  118. return new goog.math.Coordinate(
  119. goog.math.lerp(this.x0, this.x1, t), goog.math.lerp(this.y0, this.y1, t));
  120. };
  121. /**
  122. * Computes the point on the line closest to a given point. Note that a line
  123. * in this case is defined as the infinite line going through the start and end
  124. * points. To find the closest point on the line segment itself see
  125. * {@see #getClosestSegmentPoint}.
  126. * @param {number|goog.math.Coordinate} x The x coordinate of the point, or
  127. * a coordinate object.
  128. * @param {number=} opt_y The y coordinate of the point - required if x is a
  129. * number, ignored if x is a goog.math.Coordinate.
  130. * @return {!goog.math.Coordinate} The point on the line closest to the given
  131. * point.
  132. */
  133. goog.math.Line.prototype.getClosestPoint = function(x, opt_y) {
  134. return this.getInterpolatedPoint(
  135. this.getClosestLinearInterpolation_(x, opt_y));
  136. };
  137. /**
  138. * Computes the point on the line segment closest to a given point.
  139. * @param {number|goog.math.Coordinate} x The x coordinate of the point, or
  140. * a coordinate object.
  141. * @param {number=} opt_y The y coordinate of the point - required if x is a
  142. * number, ignored if x is a goog.math.Coordinate.
  143. * @return {!goog.math.Coordinate} The point on the line segment closest to the
  144. * given point.
  145. */
  146. goog.math.Line.prototype.getClosestSegmentPoint = function(x, opt_y) {
  147. return this.getInterpolatedPoint(
  148. goog.math.clamp(this.getClosestLinearInterpolation_(x, opt_y), 0, 1));
  149. };