coordinate3.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 A utility class for representing three-dimensional points.
  16. *
  17. * Based heavily on coordinate.js by:
  18. */
  19. goog.provide('goog.math.Coordinate3');
  20. /**
  21. * Class for representing coordinates and positions in 3 dimensions.
  22. *
  23. * @param {number=} opt_x X coordinate, defaults to 0.
  24. * @param {number=} opt_y Y coordinate, defaults to 0.
  25. * @param {number=} opt_z Z coordinate, defaults to 0.
  26. * @struct
  27. * @constructor
  28. */
  29. goog.math.Coordinate3 = function(opt_x, opt_y, opt_z) {
  30. /**
  31. * X-value
  32. * @type {number}
  33. */
  34. this.x = goog.isDef(opt_x) ? opt_x : 0;
  35. /**
  36. * Y-value
  37. * @type {number}
  38. */
  39. this.y = goog.isDef(opt_y) ? opt_y : 0;
  40. /**
  41. * Z-value
  42. * @type {number}
  43. */
  44. this.z = goog.isDef(opt_z) ? opt_z : 0;
  45. };
  46. /**
  47. * Returns a new copy of the coordinate.
  48. *
  49. * @return {!goog.math.Coordinate3} A clone of this coordinate.
  50. */
  51. goog.math.Coordinate3.prototype.clone = function() {
  52. return new goog.math.Coordinate3(this.x, this.y, this.z);
  53. };
  54. if (goog.DEBUG) {
  55. /**
  56. * Returns a nice string representing the coordinate.
  57. *
  58. * @return {string} In the form (50, 73, 31).
  59. * @override
  60. */
  61. goog.math.Coordinate3.prototype.toString = function() {
  62. return '(' + this.x + ', ' + this.y + ', ' + this.z + ')';
  63. };
  64. }
  65. /**
  66. * Compares coordinates for equality.
  67. *
  68. * @param {goog.math.Coordinate3} a A Coordinate3.
  69. * @param {goog.math.Coordinate3} b A Coordinate3.
  70. * @return {boolean} True iff the coordinates are equal, or if both are null.
  71. */
  72. goog.math.Coordinate3.equals = function(a, b) {
  73. if (a == b) {
  74. return true;
  75. }
  76. if (!a || !b) {
  77. return false;
  78. }
  79. return a.x == b.x && a.y == b.y && a.z == b.z;
  80. };
  81. /**
  82. * Returns the distance between two coordinates.
  83. *
  84. * @param {goog.math.Coordinate3} a A Coordinate3.
  85. * @param {goog.math.Coordinate3} b A Coordinate3.
  86. * @return {number} The distance between {@code a} and {@code b}.
  87. */
  88. goog.math.Coordinate3.distance = function(a, b) {
  89. var dx = a.x - b.x;
  90. var dy = a.y - b.y;
  91. var dz = a.z - b.z;
  92. return Math.sqrt(dx * dx + dy * dy + dz * dz);
  93. };
  94. /**
  95. * Returns the squared distance between two coordinates. Squared distances can
  96. * be used for comparisons when the actual value is not required.
  97. *
  98. * Performance note: eliminating the square root is an optimization often used
  99. * in lower-level languages, but the speed difference is not nearly as
  100. * pronounced in JavaScript (only a few percent.)
  101. *
  102. * @param {goog.math.Coordinate3} a A Coordinate3.
  103. * @param {goog.math.Coordinate3} b A Coordinate3.
  104. * @return {number} The squared distance between {@code a} and {@code b}.
  105. */
  106. goog.math.Coordinate3.squaredDistance = function(a, b) {
  107. var dx = a.x - b.x;
  108. var dy = a.y - b.y;
  109. var dz = a.z - b.z;
  110. return dx * dx + dy * dy + dz * dz;
  111. };
  112. /**
  113. * Returns the difference between two coordinates as a new
  114. * goog.math.Coordinate3.
  115. *
  116. * @param {goog.math.Coordinate3} a A Coordinate3.
  117. * @param {goog.math.Coordinate3} b A Coordinate3.
  118. * @return {!goog.math.Coordinate3} A Coordinate3 representing the difference
  119. * between {@code a} and {@code b}.
  120. */
  121. goog.math.Coordinate3.difference = function(a, b) {
  122. return new goog.math.Coordinate3(a.x - b.x, a.y - b.y, a.z - b.z);
  123. };
  124. /**
  125. * Returns the contents of this coordinate as a 3 value Array.
  126. *
  127. * @return {!Array<number>} A new array.
  128. */
  129. goog.math.Coordinate3.prototype.toArray = function() {
  130. return [this.x, this.y, this.z];
  131. };
  132. /**
  133. * Converts a three element array into a Coordinate3 object. If the value
  134. * passed in is not an array, not array-like, or not of the right length, an
  135. * error is thrown.
  136. *
  137. * @param {Array<number>} a Array of numbers to become a coordinate.
  138. * @return {!goog.math.Coordinate3} A new coordinate from the array values.
  139. * @throws {Error} When the oject passed in is not valid.
  140. */
  141. goog.math.Coordinate3.fromArray = function(a) {
  142. if (a.length <= 3) {
  143. return new goog.math.Coordinate3(a[0], a[1], a[2]);
  144. }
  145. throw Error('Conversion from an array requires an array of length 3');
  146. };