Cartesian2.js 24 KB

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