Cartesian2.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. import Check from './Check.js';
  2. import defaultValue from './defaultValue.js';
  3. import defined from './defined.js';
  4. import DeveloperError from './DeveloperError.js';
  5. import freezeObject from './freezeObject.js';
  6. import CesiumMath from './Math.js';
  7. /**
  8. * A 2D Cartesian point.
  9. * @alias Cartesian2
  10. * @constructor
  11. *
  12. * @param {Number} [x=0.0] The X component.
  13. * @param {Number} [y=0.0] The Y component.
  14. *
  15. * @see Cartesian3
  16. * @see Cartesian4
  17. * @see Packable
  18. */
  19. function Cartesian2(x, y) {
  20. /**
  21. * The X component.
  22. * @type {Number}
  23. * @default 0.0
  24. */
  25. this.x = defaultValue(x, 0.0);
  26. /**
  27. * The Y component.
  28. * @type {Number}
  29. * @default 0.0
  30. */
  31. this.y = defaultValue(y, 0.0);
  32. }
  33. /**
  34. * Creates a Cartesian2 instance from x and y coordinates.
  35. *
  36. * @param {Number} x The x coordinate.
  37. * @param {Number} y The y coordinate.
  38. * @param {Cartesian2} [result] The object onto which to store the result.
  39. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  40. */
  41. Cartesian2.fromElements = function(x, y, result) {
  42. if (!defined(result)) {
  43. return new Cartesian2(x, y);
  44. }
  45. result.x = x;
  46. result.y = y;
  47. return result;
  48. };
  49. /**
  50. * Duplicates a Cartesian2 instance.
  51. *
  52. * @param {Cartesian2} cartesian The Cartesian to duplicate.
  53. * @param {Cartesian2} [result] The object onto which to store the result.
  54. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided. (Returns undefined if cartesian is undefined)
  55. */
  56. Cartesian2.clone = function(cartesian, result) {
  57. if (!defined(cartesian)) {
  58. return undefined;
  59. }
  60. if (!defined(result)) {
  61. return new Cartesian2(cartesian.x, cartesian.y);
  62. }
  63. result.x = cartesian.x;
  64. result.y = cartesian.y;
  65. return result;
  66. };
  67. /**
  68. * Creates a Cartesian2 instance from an existing Cartesian3. This simply takes the
  69. * x and y properties of the Cartesian3 and drops z.
  70. * @function
  71. *
  72. * @param {Cartesian3} cartesian The Cartesian3 instance to create a Cartesian2 instance from.
  73. * @param {Cartesian2} [result] The object onto which to store the result.
  74. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  75. */
  76. Cartesian2.fromCartesian3 = Cartesian2.clone;
  77. /**
  78. * Creates a Cartesian2 instance from an existing Cartesian4. This simply takes the
  79. * x and y properties of the Cartesian4 and drops z and w.
  80. * @function
  81. *
  82. * @param {Cartesian4} cartesian The Cartesian4 instance to create a Cartesian2 instance from.
  83. * @param {Cartesian2} [result] The object onto which to store the result.
  84. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  85. */
  86. Cartesian2.fromCartesian4 = Cartesian2.clone;
  87. /**
  88. * The number of elements used to pack the object into an array.
  89. * @type {Number}
  90. */
  91. Cartesian2.packedLength = 2;
  92. /**
  93. * Stores the provided instance into the provided array.
  94. *
  95. * @param {Cartesian2} value The value to pack.
  96. * @param {Number[]} array The array to pack into.
  97. * @param {Number} [startingIndex=0] The index into the array at which to start packing the elements.
  98. *
  99. * @returns {Number[]} The array that was packed into
  100. */
  101. Cartesian2.pack = function(value, array, startingIndex) {
  102. //>>includeStart('debug', pragmas.debug);
  103. Check.typeOf.object('value', value);
  104. Check.defined('array', array);
  105. //>>includeEnd('debug');
  106. startingIndex = defaultValue(startingIndex, 0);
  107. array[startingIndex++] = value.x;
  108. array[startingIndex] = value.y;
  109. return array;
  110. };
  111. /**
  112. * Retrieves an instance from a packed array.
  113. *
  114. * @param {Number[]} array The packed array.
  115. * @param {Number} [startingIndex=0] The starting index of the element to be unpacked.
  116. * @param {Cartesian2} [result] The object into which to store the result.
  117. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  118. */
  119. Cartesian2.unpack = function(array, startingIndex, result) {
  120. //>>includeStart('debug', pragmas.debug);
  121. Check.defined('array', array);
  122. //>>includeEnd('debug');
  123. startingIndex = defaultValue(startingIndex, 0);
  124. if (!defined(result)) {
  125. result = new Cartesian2();
  126. }
  127. result.x = array[startingIndex++];
  128. result.y = array[startingIndex];
  129. return result;
  130. };
  131. /**
  132. * Flattens an array of Cartesian2s into and array of components.
  133. *
  134. * @param {Cartesian2[]} array The array of cartesians to pack.
  135. * @param {Number[]} [result] The array onto which to store the result.
  136. * @returns {Number[]} The packed array.
  137. */
  138. Cartesian2.packArray = function(array, result) {
  139. //>>includeStart('debug', pragmas.debug);
  140. Check.defined('array', array);
  141. //>>includeEnd('debug');
  142. var length = array.length;
  143. if (!defined(result)) {
  144. result = new Array(length * 2);
  145. } else {
  146. result.length = length * 2;
  147. }
  148. for (var i = 0; i < length; ++i) {
  149. Cartesian2.pack(array[i], result, i * 2);
  150. }
  151. return result;
  152. };
  153. /**
  154. * Unpacks an array of cartesian components into and array of Cartesian2s.
  155. *
  156. * @param {Number[]} array The array of components to unpack.
  157. * @param {Cartesian2[]} [result] The array onto which to store the result.
  158. * @returns {Cartesian2[]} The unpacked array.
  159. */
  160. Cartesian2.unpackArray = function(array, result) {
  161. //>>includeStart('debug', pragmas.debug);
  162. Check.defined('array', array);
  163. //>>includeEnd('debug');
  164. var length = array.length;
  165. if (!defined(result)) {
  166. result = new Array(length / 2);
  167. } else {
  168. result.length = length / 2;
  169. }
  170. for (var i = 0; i < length; i += 2) {
  171. var index = i / 2;
  172. result[index] = Cartesian2.unpack(array, i, result[index]);
  173. }
  174. return result;
  175. };
  176. /**
  177. * Creates a Cartesian2 from two consecutive elements in an array.
  178. * @function
  179. *
  180. * @param {Number[]} array The array whose two consecutive elements correspond to the x and y components, respectively.
  181. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to the x component.
  182. * @param {Cartesian2} [result] The object onto which to store the result.
  183. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  184. *
  185. * @example
  186. * // Create a Cartesian2 with (1.0, 2.0)
  187. * var v = [1.0, 2.0];
  188. * var p = Cesium.Cartesian2.fromArray(v);
  189. *
  190. * // Create a Cartesian2 with (1.0, 2.0) using an offset into an array
  191. * var v2 = [0.0, 0.0, 1.0, 2.0];
  192. * var p2 = Cesium.Cartesian2.fromArray(v2, 2);
  193. */
  194. Cartesian2.fromArray = Cartesian2.unpack;
  195. /**
  196. * Computes the value of the maximum component for the supplied Cartesian.
  197. *
  198. * @param {Cartesian2} cartesian The cartesian to use.
  199. * @returns {Number} The value of the maximum component.
  200. */
  201. Cartesian2.maximumComponent = function(cartesian) {
  202. //>>includeStart('debug', pragmas.debug);
  203. Check.typeOf.object('cartesian', cartesian);
  204. //>>includeEnd('debug');
  205. return Math.max(cartesian.x, cartesian.y);
  206. };
  207. /**
  208. * Computes the value of the minimum component for the supplied Cartesian.
  209. *
  210. * @param {Cartesian2} cartesian The cartesian to use.
  211. * @returns {Number} The value of the minimum component.
  212. */
  213. Cartesian2.minimumComponent = function(cartesian) {
  214. //>>includeStart('debug', pragmas.debug);
  215. Check.typeOf.object('cartesian', cartesian);
  216. //>>includeEnd('debug');
  217. return Math.min(cartesian.x, cartesian.y);
  218. };
  219. /**
  220. * Compares two Cartesians and computes a Cartesian which contains the minimum components of the supplied Cartesians.
  221. *
  222. * @param {Cartesian2} first A cartesian to compare.
  223. * @param {Cartesian2} second A cartesian to compare.
  224. * @param {Cartesian2} result The object into which to store the result.
  225. * @returns {Cartesian2} A cartesian with the minimum components.
  226. */
  227. Cartesian2.minimumByComponent = function(first, second, result) {
  228. //>>includeStart('debug', pragmas.debug);
  229. Check.typeOf.object('first', first);
  230. Check.typeOf.object('second', second);
  231. Check.typeOf.object('result', result);
  232. //>>includeEnd('debug');
  233. result.x = Math.min(first.x, second.x);
  234. result.y = Math.min(first.y, second.y);
  235. return result;
  236. };
  237. /**
  238. * Compares two Cartesians and computes a Cartesian which contains the maximum components of the supplied Cartesians.
  239. *
  240. * @param {Cartesian2} first A cartesian to compare.
  241. * @param {Cartesian2} second A cartesian to compare.
  242. * @param {Cartesian2} result The object into which to store the result.
  243. * @returns {Cartesian2} A cartesian with the maximum components.
  244. */
  245. Cartesian2.maximumByComponent = function(first, second, result) {
  246. //>>includeStart('debug', pragmas.debug);
  247. Check.typeOf.object('first', first);
  248. Check.typeOf.object('second', second);
  249. Check.typeOf.object('result', result);
  250. //>>includeEnd('debug');
  251. result.x = Math.max(first.x, second.x);
  252. result.y = Math.max(first.y, second.y);
  253. return result;
  254. };
  255. /**
  256. * Computes the provided Cartesian's squared magnitude.
  257. *
  258. * @param {Cartesian2} cartesian The Cartesian instance whose squared magnitude is to be computed.
  259. * @returns {Number} The squared magnitude.
  260. */
  261. Cartesian2.magnitudeSquared = function(cartesian) {
  262. //>>includeStart('debug', pragmas.debug);
  263. Check.typeOf.object('cartesian', cartesian);
  264. //>>includeEnd('debug');
  265. return cartesian.x * cartesian.x + cartesian.y * cartesian.y;
  266. };
  267. /**
  268. * Computes the Cartesian's magnitude (length).
  269. *
  270. * @param {Cartesian2} cartesian The Cartesian instance whose magnitude is to be computed.
  271. * @returns {Number} The magnitude.
  272. */
  273. Cartesian2.magnitude = function(cartesian) {
  274. return Math.sqrt(Cartesian2.magnitudeSquared(cartesian));
  275. };
  276. var distanceScratch = new Cartesian2();
  277. /**
  278. * Computes the distance between two points.
  279. *
  280. * @param {Cartesian2} left The first point to compute the distance from.
  281. * @param {Cartesian2} right The second point to compute the distance to.
  282. * @returns {Number} The distance between two points.
  283. *
  284. * @example
  285. * // Returns 1.0
  286. * var d = Cesium.Cartesian2.distance(new Cesium.Cartesian2(1.0, 0.0), new Cesium.Cartesian2(2.0, 0.0));
  287. */
  288. Cartesian2.distance = function(left, right) {
  289. //>>includeStart('debug', pragmas.debug);
  290. Check.typeOf.object('left', left);
  291. Check.typeOf.object('right', right);
  292. //>>includeEnd('debug');
  293. Cartesian2.subtract(left, right, distanceScratch);
  294. return Cartesian2.magnitude(distanceScratch);
  295. };
  296. /**
  297. * Computes the squared distance between two points. Comparing squared distances
  298. * using this function is more efficient than comparing distances using {@link Cartesian2#distance}.
  299. *
  300. * @param {Cartesian2} left The first point to compute the distance from.
  301. * @param {Cartesian2} right The second point to compute the distance to.
  302. * @returns {Number} The distance between two points.
  303. *
  304. * @example
  305. * // Returns 4.0, not 2.0
  306. * var d = Cesium.Cartesian2.distance(new Cesium.Cartesian2(1.0, 0.0), new Cesium.Cartesian2(3.0, 0.0));
  307. */
  308. Cartesian2.distanceSquared = function(left, right) {
  309. //>>includeStart('debug', pragmas.debug);
  310. Check.typeOf.object('left', left);
  311. Check.typeOf.object('right', right);
  312. //>>includeEnd('debug');
  313. Cartesian2.subtract(left, right, distanceScratch);
  314. return Cartesian2.magnitudeSquared(distanceScratch);
  315. };
  316. /**
  317. * Computes the normalized form of the supplied Cartesian.
  318. *
  319. * @param {Cartesian2} cartesian The Cartesian to be normalized.
  320. * @param {Cartesian2} result The object onto which to store the result.
  321. * @returns {Cartesian2} The modified result parameter.
  322. */
  323. Cartesian2.normalize = function(cartesian, result) {
  324. //>>includeStart('debug', pragmas.debug);
  325. Check.typeOf.object('cartesian', cartesian);
  326. Check.typeOf.object('result', result);
  327. //>>includeEnd('debug');
  328. var magnitude = Cartesian2.magnitude(cartesian);
  329. result.x = cartesian.x / magnitude;
  330. result.y = cartesian.y / magnitude;
  331. //>>includeStart('debug', pragmas.debug);
  332. if (isNaN(result.x) || isNaN(result.y)) {
  333. throw new DeveloperError('normalized result is not a number');
  334. }
  335. //>>includeEnd('debug');
  336. return result;
  337. };
  338. /**
  339. * Computes the dot (scalar) product of two Cartesians.
  340. *
  341. * @param {Cartesian2} left The first Cartesian.
  342. * @param {Cartesian2} right The second Cartesian.
  343. * @returns {Number} The dot product.
  344. */
  345. Cartesian2.dot = function(left, right) {
  346. //>>includeStart('debug', pragmas.debug);
  347. Check.typeOf.object('left', left);
  348. Check.typeOf.object('right', right);
  349. //>>includeEnd('debug');
  350. return left.x * right.x + left.y * right.y;
  351. };
  352. /**
  353. * Computes the componentwise product of two Cartesians.
  354. *
  355. * @param {Cartesian2} left The first Cartesian.
  356. * @param {Cartesian2} right The second Cartesian.
  357. * @param {Cartesian2} result The object onto which to store the result.
  358. * @returns {Cartesian2} The modified result parameter.
  359. */
  360. Cartesian2.multiplyComponents = function(left, right, result) {
  361. //>>includeStart('debug', pragmas.debug);
  362. Check.typeOf.object('left', left);
  363. Check.typeOf.object('right', right);
  364. Check.typeOf.object('result', result);
  365. //>>includeEnd('debug');
  366. result.x = left.x * right.x;
  367. result.y = left.y * right.y;
  368. return result;
  369. };
  370. /**
  371. * Computes the componentwise quotient of two Cartesians.
  372. *
  373. * @param {Cartesian2} left The first Cartesian.
  374. * @param {Cartesian2} right The second Cartesian.
  375. * @param {Cartesian2} result The object onto which to store the result.
  376. * @returns {Cartesian2} The modified result parameter.
  377. */
  378. Cartesian2.divideComponents = function(left, right, result) {
  379. //>>includeStart('debug', pragmas.debug);
  380. Check.typeOf.object('left', left);
  381. Check.typeOf.object('right', right);
  382. Check.typeOf.object('result', result);
  383. //>>includeEnd('debug');
  384. result.x = left.x / right.x;
  385. result.y = left.y / right.y;
  386. return result;
  387. };
  388. /**
  389. * Computes the componentwise sum of two Cartesians.
  390. *
  391. * @param {Cartesian2} left The first Cartesian.
  392. * @param {Cartesian2} right The second Cartesian.
  393. * @param {Cartesian2} result The object onto which to store the result.
  394. * @returns {Cartesian2} The modified result parameter.
  395. */
  396. Cartesian2.add = function(left, right, result) {
  397. //>>includeStart('debug', pragmas.debug);
  398. Check.typeOf.object('left', left);
  399. Check.typeOf.object('right', right);
  400. Check.typeOf.object('result', result);
  401. //>>includeEnd('debug');
  402. result.x = left.x + right.x;
  403. result.y = left.y + right.y;
  404. return result;
  405. };
  406. /**
  407. * Computes the componentwise difference of two Cartesians.
  408. *
  409. * @param {Cartesian2} left The first Cartesian.
  410. * @param {Cartesian2} right The second Cartesian.
  411. * @param {Cartesian2} result The object onto which to store the result.
  412. * @returns {Cartesian2} The modified result parameter.
  413. */
  414. Cartesian2.subtract = function(left, right, result) {
  415. //>>includeStart('debug', pragmas.debug);
  416. Check.typeOf.object('left', left);
  417. Check.typeOf.object('right', right);
  418. Check.typeOf.object('result', result);
  419. //>>includeEnd('debug');
  420. result.x = left.x - right.x;
  421. result.y = left.y - right.y;
  422. return result;
  423. };
  424. /**
  425. * Multiplies the provided Cartesian componentwise by the provided scalar.
  426. *
  427. * @param {Cartesian2} cartesian The Cartesian to be scaled.
  428. * @param {Number} scalar The scalar to multiply with.
  429. * @param {Cartesian2} result The object onto which to store the result.
  430. * @returns {Cartesian2} The modified result parameter.
  431. */
  432. Cartesian2.multiplyByScalar = function(cartesian, scalar, result) {
  433. //>>includeStart('debug', pragmas.debug);
  434. Check.typeOf.object('cartesian', cartesian);
  435. Check.typeOf.number('scalar', scalar);
  436. Check.typeOf.object('result', result);
  437. //>>includeEnd('debug');
  438. result.x = cartesian.x * scalar;
  439. result.y = cartesian.y * scalar;
  440. return result;
  441. };
  442. /**
  443. * Divides the provided Cartesian componentwise by the provided scalar.
  444. *
  445. * @param {Cartesian2} cartesian The Cartesian to be divided.
  446. * @param {Number} scalar The scalar to divide by.
  447. * @param {Cartesian2} result The object onto which to store the result.
  448. * @returns {Cartesian2} The modified result parameter.
  449. */
  450. Cartesian2.divideByScalar = function(cartesian, scalar, result) {
  451. //>>includeStart('debug', pragmas.debug);
  452. Check.typeOf.object('cartesian', cartesian);
  453. Check.typeOf.number('scalar', scalar);
  454. Check.typeOf.object('result', result);
  455. //>>includeEnd('debug');
  456. result.x = cartesian.x / scalar;
  457. result.y = cartesian.y / scalar;
  458. return result;
  459. };
  460. /**
  461. * Negates the provided Cartesian.
  462. *
  463. * @param {Cartesian2} cartesian The Cartesian to be negated.
  464. * @param {Cartesian2} result The object onto which to store the result.
  465. * @returns {Cartesian2} The modified result parameter.
  466. */
  467. Cartesian2.negate = function(cartesian, result) {
  468. //>>includeStart('debug', pragmas.debug);
  469. Check.typeOf.object('cartesian', cartesian);
  470. Check.typeOf.object('result', result);
  471. //>>includeEnd('debug');
  472. result.x = -cartesian.x;
  473. result.y = -cartesian.y;
  474. return result;
  475. };
  476. /**
  477. * Computes the absolute value of the provided Cartesian.
  478. *
  479. * @param {Cartesian2} cartesian The Cartesian whose absolute value is to be computed.
  480. * @param {Cartesian2} result The object onto which to store the result.
  481. * @returns {Cartesian2} The modified result parameter.
  482. */
  483. Cartesian2.abs = function(cartesian, result) {
  484. //>>includeStart('debug', pragmas.debug);
  485. Check.typeOf.object('cartesian', cartesian);
  486. Check.typeOf.object('result', result);
  487. //>>includeEnd('debug');
  488. result.x = Math.abs(cartesian.x);
  489. result.y = Math.abs(cartesian.y);
  490. return result;
  491. };
  492. var lerpScratch = new Cartesian2();
  493. /**
  494. * Computes the linear interpolation or extrapolation at t using the provided cartesians.
  495. *
  496. * @param {Cartesian2} start The value corresponding to t at 0.0.
  497. * @param {Cartesian2} end The value corresponding to t at 1.0.
  498. * @param {Number} t The point along t at which to interpolate.
  499. * @param {Cartesian2} result The object onto which to store the result.
  500. * @returns {Cartesian2} The modified result parameter.
  501. */
  502. Cartesian2.lerp = function(start, end, t, result) {
  503. //>>includeStart('debug', pragmas.debug);
  504. Check.typeOf.object('start', start);
  505. Check.typeOf.object('end', end);
  506. Check.typeOf.number('t', t);
  507. Check.typeOf.object('result', result);
  508. //>>includeEnd('debug');
  509. Cartesian2.multiplyByScalar(end, t, lerpScratch);
  510. result = Cartesian2.multiplyByScalar(start, 1.0 - t, result);
  511. return Cartesian2.add(lerpScratch, result, result);
  512. };
  513. var angleBetweenScratch = new Cartesian2();
  514. var angleBetweenScratch2 = new Cartesian2();
  515. /**
  516. * Returns the angle, in radians, between the provided Cartesians.
  517. *
  518. * @param {Cartesian2} left The first Cartesian.
  519. * @param {Cartesian2} right The second Cartesian.
  520. * @returns {Number} The angle between the Cartesians.
  521. */
  522. Cartesian2.angleBetween = function(left, right) {
  523. //>>includeStart('debug', pragmas.debug);
  524. Check.typeOf.object('left', left);
  525. Check.typeOf.object('right', right);
  526. //>>includeEnd('debug');
  527. Cartesian2.normalize(left, angleBetweenScratch);
  528. Cartesian2.normalize(right, angleBetweenScratch2);
  529. return CesiumMath.acosClamped(Cartesian2.dot(angleBetweenScratch, angleBetweenScratch2));
  530. };
  531. var mostOrthogonalAxisScratch = new Cartesian2();
  532. /**
  533. * Returns the axis that is most orthogonal to the provided Cartesian.
  534. *
  535. * @param {Cartesian2} cartesian The Cartesian on which to find the most orthogonal axis.
  536. * @param {Cartesian2} result The object onto which to store the result.
  537. * @returns {Cartesian2} The most orthogonal axis.
  538. */
  539. Cartesian2.mostOrthogonalAxis = function(cartesian, result) {
  540. //>>includeStart('debug', pragmas.debug);
  541. Check.typeOf.object('cartesian', cartesian);
  542. Check.typeOf.object('result', result);
  543. //>>includeEnd('debug');
  544. var f = Cartesian2.normalize(cartesian, mostOrthogonalAxisScratch);
  545. Cartesian2.abs(f, f);
  546. if (f.x <= f.y) {
  547. result = Cartesian2.clone(Cartesian2.UNIT_X, result);
  548. } else {
  549. result = Cartesian2.clone(Cartesian2.UNIT_Y, result);
  550. }
  551. return result;
  552. };
  553. /**
  554. * Compares the provided Cartesians componentwise and returns
  555. * <code>true</code> if they are equal, <code>false</code> otherwise.
  556. *
  557. * @param {Cartesian2} [left] The first Cartesian.
  558. * @param {Cartesian2} [right] The second Cartesian.
  559. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  560. */
  561. Cartesian2.equals = function(left, right) {
  562. return (left === right) ||
  563. ((defined(left)) &&
  564. (defined(right)) &&
  565. (left.x === right.x) &&
  566. (left.y === right.y));
  567. };
  568. /**
  569. * @private
  570. */
  571. Cartesian2.equalsArray = function(cartesian, array, offset) {
  572. return cartesian.x === array[offset] &&
  573. cartesian.y === array[offset + 1];
  574. };
  575. /**
  576. * Compares the provided Cartesians componentwise and returns
  577. * <code>true</code> if they pass an absolute or relative tolerance test,
  578. * <code>false</code> otherwise.
  579. *
  580. * @param {Cartesian2} [left] The first Cartesian.
  581. * @param {Cartesian2} [right] The second Cartesian.
  582. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  583. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  584. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  585. */
  586. Cartesian2.equalsEpsilon = function(left, right, relativeEpsilon, absoluteEpsilon) {
  587. return (left === right) ||
  588. (defined(left) &&
  589. defined(right) &&
  590. CesiumMath.equalsEpsilon(left.x, right.x, relativeEpsilon, absoluteEpsilon) &&
  591. CesiumMath.equalsEpsilon(left.y, right.y, relativeEpsilon, absoluteEpsilon));
  592. };
  593. /**
  594. * An immutable Cartesian2 instance initialized to (0.0, 0.0).
  595. *
  596. * @type {Cartesian2}
  597. * @constant
  598. */
  599. Cartesian2.ZERO = freezeObject(new Cartesian2(0.0, 0.0));
  600. /**
  601. * An immutable Cartesian2 instance initialized to (1.0, 0.0).
  602. *
  603. * @type {Cartesian2}
  604. * @constant
  605. */
  606. Cartesian2.UNIT_X = freezeObject(new Cartesian2(1.0, 0.0));
  607. /**
  608. * An immutable Cartesian2 instance initialized to (0.0, 1.0).
  609. *
  610. * @type {Cartesian2}
  611. * @constant
  612. */
  613. Cartesian2.UNIT_Y = freezeObject(new Cartesian2(0.0, 1.0));
  614. /**
  615. * Duplicates this Cartesian2 instance.
  616. *
  617. * @param {Cartesian2} [result] The object onto which to store the result.
  618. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  619. */
  620. Cartesian2.prototype.clone = function(result) {
  621. return Cartesian2.clone(this, result);
  622. };
  623. /**
  624. * Compares this Cartesian against the provided Cartesian componentwise and returns
  625. * <code>true</code> if they are equal, <code>false</code> otherwise.
  626. *
  627. * @param {Cartesian2} [right] The right hand side Cartesian.
  628. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  629. */
  630. Cartesian2.prototype.equals = function(right) {
  631. return Cartesian2.equals(this, right);
  632. };
  633. /**
  634. * Compares this Cartesian against the provided Cartesian componentwise and returns
  635. * <code>true</code> if they pass an absolute or relative tolerance test,
  636. * <code>false</code> otherwise.
  637. *
  638. * @param {Cartesian2} [right] The right hand side Cartesian.
  639. * @param {Number} relativeEpsilon The relative epsilon tolerance to use for equality testing.
  640. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  641. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  642. */
  643. Cartesian2.prototype.equalsEpsilon = function(right, relativeEpsilon, absoluteEpsilon) {
  644. return Cartesian2.equalsEpsilon(this, right, relativeEpsilon, absoluteEpsilon);
  645. };
  646. /**
  647. * Creates a string representing this Cartesian in the format '(x, y)'.
  648. *
  649. * @returns {String} A string representing the provided Cartesian in the format '(x, y)'.
  650. */
  651. Cartesian2.prototype.toString = function() {
  652. return '(' + this.x + ', ' + this.y + ')';
  653. };
  654. export default Cartesian2;