Cartesian2.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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 an 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. const length = array.length;
  142. const resultLength = length * 2;
  143. if (!defined(result)) {
  144. result = new Array(resultLength);
  145. } else if (!Array.isArray(result) && result.length !== resultLength) {
  146. //>>includeStart('debug', pragmas.debug);
  147. throw new DeveloperError(
  148. "If result is a typed array, it must have exactly array.length * 2 elements"
  149. );
  150. //>>includeEnd('debug');
  151. } else if (result.length !== resultLength) {
  152. result.length = resultLength;
  153. }
  154. for (let i = 0; i < length; ++i) {
  155. Cartesian2.pack(array[i], result, i * 2);
  156. }
  157. return result;
  158. };
  159. /**
  160. * Unpacks an array of cartesian components into an array of Cartesian2s.
  161. *
  162. * @param {Number[]} array The array of components to unpack.
  163. * @param {Cartesian2[]} [result] The array onto which to store the result.
  164. * @returns {Cartesian2[]} The unpacked array.
  165. */
  166. Cartesian2.unpackArray = function (array, result) {
  167. //>>includeStart('debug', pragmas.debug);
  168. Check.defined("array", array);
  169. Check.typeOf.number.greaterThanOrEquals("array.length", array.length, 2);
  170. if (array.length % 2 !== 0) {
  171. throw new DeveloperError("array length must be a multiple of 2.");
  172. }
  173. //>>includeEnd('debug');
  174. const length = array.length;
  175. if (!defined(result)) {
  176. result = new Array(length / 2);
  177. } else {
  178. result.length = length / 2;
  179. }
  180. for (let i = 0; i < length; i += 2) {
  181. const index = i / 2;
  182. result[index] = Cartesian2.unpack(array, i, result[index]);
  183. }
  184. return result;
  185. };
  186. /**
  187. * Creates a Cartesian2 from two consecutive elements in an array.
  188. * @function
  189. *
  190. * @param {Number[]} array The array whose two consecutive elements correspond to the x and y components, respectively.
  191. * @param {Number} [startingIndex=0] The offset into the array of the first element, which corresponds to the x component.
  192. * @param {Cartesian2} [result] The object onto which to store the result.
  193. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  194. *
  195. * @example
  196. * // Create a Cartesian2 with (1.0, 2.0)
  197. * const v = [1.0, 2.0];
  198. * const p = Cesium.Cartesian2.fromArray(v);
  199. *
  200. * // Create a Cartesian2 with (1.0, 2.0) using an offset into an array
  201. * const v2 = [0.0, 0.0, 1.0, 2.0];
  202. * const p2 = Cesium.Cartesian2.fromArray(v2, 2);
  203. */
  204. Cartesian2.fromArray = Cartesian2.unpack;
  205. /**
  206. * Computes the value of the maximum component for the supplied Cartesian.
  207. *
  208. * @param {Cartesian2} cartesian The cartesian to use.
  209. * @returns {Number} The value of the maximum component.
  210. */
  211. Cartesian2.maximumComponent = function (cartesian) {
  212. //>>includeStart('debug', pragmas.debug);
  213. Check.typeOf.object("cartesian", cartesian);
  214. //>>includeEnd('debug');
  215. return Math.max(cartesian.x, cartesian.y);
  216. };
  217. /**
  218. * Computes the value of the minimum component for the supplied Cartesian.
  219. *
  220. * @param {Cartesian2} cartesian The cartesian to use.
  221. * @returns {Number} The value of the minimum component.
  222. */
  223. Cartesian2.minimumComponent = function (cartesian) {
  224. //>>includeStart('debug', pragmas.debug);
  225. Check.typeOf.object("cartesian", cartesian);
  226. //>>includeEnd('debug');
  227. return Math.min(cartesian.x, cartesian.y);
  228. };
  229. /**
  230. * Compares two Cartesians and computes a Cartesian which contains the minimum components of the supplied Cartesians.
  231. *
  232. * @param {Cartesian2} first A cartesian to compare.
  233. * @param {Cartesian2} second A cartesian to compare.
  234. * @param {Cartesian2} result The object into which to store the result.
  235. * @returns {Cartesian2} A cartesian with the minimum components.
  236. */
  237. Cartesian2.minimumByComponent = function (first, second, result) {
  238. //>>includeStart('debug', pragmas.debug);
  239. Check.typeOf.object("first", first);
  240. Check.typeOf.object("second", second);
  241. Check.typeOf.object("result", result);
  242. //>>includeEnd('debug');
  243. result.x = Math.min(first.x, second.x);
  244. result.y = Math.min(first.y, second.y);
  245. return result;
  246. };
  247. /**
  248. * Compares two Cartesians and computes a Cartesian which contains the maximum components of the supplied Cartesians.
  249. *
  250. * @param {Cartesian2} first A cartesian to compare.
  251. * @param {Cartesian2} second A cartesian to compare.
  252. * @param {Cartesian2} result The object into which to store the result.
  253. * @returns {Cartesian2} A cartesian with the maximum components.
  254. */
  255. Cartesian2.maximumByComponent = function (first, second, result) {
  256. //>>includeStart('debug', pragmas.debug);
  257. Check.typeOf.object("first", first);
  258. Check.typeOf.object("second", second);
  259. Check.typeOf.object("result", result);
  260. //>>includeEnd('debug');
  261. result.x = Math.max(first.x, second.x);
  262. result.y = Math.max(first.y, second.y);
  263. return result;
  264. };
  265. /**
  266. * Constrain a value to lie between two values.
  267. *
  268. * @param {Cartesian2} value The value to clamp.
  269. * @param {Cartesian2} min The minimum bound.
  270. * @param {Cartesian2} max The maximum bound.
  271. * @param {Cartesian2} result The object into which to store the result.
  272. * @returns {Cartesian2} The clamped value such that min <= result <= max.
  273. */
  274. Cartesian2.clamp = function (value, min, max, result) {
  275. //>>includeStart('debug', pragmas.debug);
  276. Check.typeOf.object("value", value);
  277. Check.typeOf.object("min", min);
  278. Check.typeOf.object("max", max);
  279. Check.typeOf.object("result", result);
  280. //>>includeEnd('debug');
  281. const x = CesiumMath.clamp(value.x, min.x, max.x);
  282. const y = CesiumMath.clamp(value.y, min.y, max.y);
  283. result.x = x;
  284. result.y = y;
  285. return result;
  286. };
  287. /**
  288. * Computes the provided Cartesian's squared magnitude.
  289. *
  290. * @param {Cartesian2} cartesian The Cartesian instance whose squared magnitude is to be computed.
  291. * @returns {Number} The squared magnitude.
  292. */
  293. Cartesian2.magnitudeSquared = function (cartesian) {
  294. //>>includeStart('debug', pragmas.debug);
  295. Check.typeOf.object("cartesian", cartesian);
  296. //>>includeEnd('debug');
  297. return cartesian.x * cartesian.x + cartesian.y * cartesian.y;
  298. };
  299. /**
  300. * Computes the Cartesian's magnitude (length).
  301. *
  302. * @param {Cartesian2} cartesian The Cartesian instance whose magnitude is to be computed.
  303. * @returns {Number} The magnitude.
  304. */
  305. Cartesian2.magnitude = function (cartesian) {
  306. return Math.sqrt(Cartesian2.magnitudeSquared(cartesian));
  307. };
  308. const distanceScratch = new Cartesian2();
  309. /**
  310. * Computes the distance between two points.
  311. *
  312. * @param {Cartesian2} left The first point to compute the distance from.
  313. * @param {Cartesian2} right The second point to compute the distance to.
  314. * @returns {Number} The distance between two points.
  315. *
  316. * @example
  317. * // Returns 1.0
  318. * const d = Cesium.Cartesian2.distance(new Cesium.Cartesian2(1.0, 0.0), new Cesium.Cartesian2(2.0, 0.0));
  319. */
  320. Cartesian2.distance = function (left, right) {
  321. //>>includeStart('debug', pragmas.debug);
  322. Check.typeOf.object("left", left);
  323. Check.typeOf.object("right", right);
  324. //>>includeEnd('debug');
  325. Cartesian2.subtract(left, right, distanceScratch);
  326. return Cartesian2.magnitude(distanceScratch);
  327. };
  328. /**
  329. * Computes the squared distance between two points. Comparing squared distances
  330. * using this function is more efficient than comparing distances using {@link Cartesian2#distance}.
  331. *
  332. * @param {Cartesian2} left The first point to compute the distance from.
  333. * @param {Cartesian2} right The second point to compute the distance to.
  334. * @returns {Number} The distance between two points.
  335. *
  336. * @example
  337. * // Returns 4.0, not 2.0
  338. * const d = Cesium.Cartesian2.distance(new Cesium.Cartesian2(1.0, 0.0), new Cesium.Cartesian2(3.0, 0.0));
  339. */
  340. Cartesian2.distanceSquared = function (left, right) {
  341. //>>includeStart('debug', pragmas.debug);
  342. Check.typeOf.object("left", left);
  343. Check.typeOf.object("right", right);
  344. //>>includeEnd('debug');
  345. Cartesian2.subtract(left, right, distanceScratch);
  346. return Cartesian2.magnitudeSquared(distanceScratch);
  347. };
  348. /**
  349. * Computes the normalized form of the supplied Cartesian.
  350. *
  351. * @param {Cartesian2} cartesian The Cartesian to be normalized.
  352. * @param {Cartesian2} result The object onto which to store the result.
  353. * @returns {Cartesian2} The modified result parameter.
  354. */
  355. Cartesian2.normalize = function (cartesian, result) {
  356. //>>includeStart('debug', pragmas.debug);
  357. Check.typeOf.object("cartesian", cartesian);
  358. Check.typeOf.object("result", result);
  359. //>>includeEnd('debug');
  360. const magnitude = Cartesian2.magnitude(cartesian);
  361. result.x = cartesian.x / magnitude;
  362. result.y = cartesian.y / magnitude;
  363. //>>includeStart('debug', pragmas.debug);
  364. if (isNaN(result.x) || isNaN(result.y)) {
  365. throw new DeveloperError("normalized result is not a number");
  366. }
  367. //>>includeEnd('debug');
  368. return result;
  369. };
  370. /**
  371. * Computes the dot (scalar) product of two Cartesians.
  372. *
  373. * @param {Cartesian2} left The first Cartesian.
  374. * @param {Cartesian2} right The second Cartesian.
  375. * @returns {Number} The dot product.
  376. */
  377. Cartesian2.dot = function (left, right) {
  378. //>>includeStart('debug', pragmas.debug);
  379. Check.typeOf.object("left", left);
  380. Check.typeOf.object("right", right);
  381. //>>includeEnd('debug');
  382. return left.x * right.x + left.y * right.y;
  383. };
  384. /**
  385. * Computes the magnitude of the cross product that would result from implicitly setting the Z coordinate of the input vectors to 0
  386. *
  387. * @param {Cartesian2} left The first Cartesian.
  388. * @param {Cartesian2} right The second Cartesian.
  389. * @returns {Number} The cross product.
  390. */
  391. Cartesian2.cross = function (left, right) {
  392. //>>includeStart('debug', pragmas.debug);
  393. Check.typeOf.object("left", left);
  394. Check.typeOf.object("right", right);
  395. //>>includeEnd('debug');
  396. return left.x * right.y - left.y * right.x;
  397. };
  398. /**
  399. * Computes the componentwise product of two Cartesians.
  400. *
  401. * @param {Cartesian2} left The first Cartesian.
  402. * @param {Cartesian2} right The second Cartesian.
  403. * @param {Cartesian2} result The object onto which to store the result.
  404. * @returns {Cartesian2} The modified result parameter.
  405. */
  406. Cartesian2.multiplyComponents = function (left, right, result) {
  407. //>>includeStart('debug', pragmas.debug);
  408. Check.typeOf.object("left", left);
  409. Check.typeOf.object("right", right);
  410. Check.typeOf.object("result", result);
  411. //>>includeEnd('debug');
  412. result.x = left.x * right.x;
  413. result.y = left.y * right.y;
  414. return result;
  415. };
  416. /**
  417. * Computes the componentwise quotient of two Cartesians.
  418. *
  419. * @param {Cartesian2} left The first Cartesian.
  420. * @param {Cartesian2} right The second Cartesian.
  421. * @param {Cartesian2} result The object onto which to store the result.
  422. * @returns {Cartesian2} The modified result parameter.
  423. */
  424. Cartesian2.divideComponents = function (left, right, result) {
  425. //>>includeStart('debug', pragmas.debug);
  426. Check.typeOf.object("left", left);
  427. Check.typeOf.object("right", right);
  428. Check.typeOf.object("result", result);
  429. //>>includeEnd('debug');
  430. result.x = left.x / right.x;
  431. result.y = left.y / right.y;
  432. return result;
  433. };
  434. /**
  435. * Computes the componentwise sum of two Cartesians.
  436. *
  437. * @param {Cartesian2} left The first Cartesian.
  438. * @param {Cartesian2} right The second Cartesian.
  439. * @param {Cartesian2} result The object onto which to store the result.
  440. * @returns {Cartesian2} The modified result parameter.
  441. */
  442. Cartesian2.add = function (left, right, result) {
  443. //>>includeStart('debug', pragmas.debug);
  444. Check.typeOf.object("left", left);
  445. Check.typeOf.object("right", right);
  446. Check.typeOf.object("result", result);
  447. //>>includeEnd('debug');
  448. result.x = left.x + right.x;
  449. result.y = left.y + right.y;
  450. return result;
  451. };
  452. /**
  453. * Computes the componentwise difference of two Cartesians.
  454. *
  455. * @param {Cartesian2} left The first Cartesian.
  456. * @param {Cartesian2} right The second Cartesian.
  457. * @param {Cartesian2} result The object onto which to store the result.
  458. * @returns {Cartesian2} The modified result parameter.
  459. */
  460. Cartesian2.subtract = function (left, right, result) {
  461. //>>includeStart('debug', pragmas.debug);
  462. Check.typeOf.object("left", left);
  463. Check.typeOf.object("right", right);
  464. Check.typeOf.object("result", result);
  465. //>>includeEnd('debug');
  466. result.x = left.x - right.x;
  467. result.y = left.y - right.y;
  468. return result;
  469. };
  470. /**
  471. * Multiplies the provided Cartesian componentwise by the provided scalar.
  472. *
  473. * @param {Cartesian2} cartesian The Cartesian to be scaled.
  474. * @param {Number} scalar The scalar to multiply with.
  475. * @param {Cartesian2} result The object onto which to store the result.
  476. * @returns {Cartesian2} The modified result parameter.
  477. */
  478. Cartesian2.multiplyByScalar = function (cartesian, scalar, result) {
  479. //>>includeStart('debug', pragmas.debug);
  480. Check.typeOf.object("cartesian", cartesian);
  481. Check.typeOf.number("scalar", scalar);
  482. Check.typeOf.object("result", result);
  483. //>>includeEnd('debug');
  484. result.x = cartesian.x * scalar;
  485. result.y = cartesian.y * scalar;
  486. return result;
  487. };
  488. /**
  489. * Divides the provided Cartesian componentwise by the provided scalar.
  490. *
  491. * @param {Cartesian2} cartesian The Cartesian to be divided.
  492. * @param {Number} scalar The scalar to divide by.
  493. * @param {Cartesian2} result The object onto which to store the result.
  494. * @returns {Cartesian2} The modified result parameter.
  495. */
  496. Cartesian2.divideByScalar = function (cartesian, scalar, result) {
  497. //>>includeStart('debug', pragmas.debug);
  498. Check.typeOf.object("cartesian", cartesian);
  499. Check.typeOf.number("scalar", scalar);
  500. Check.typeOf.object("result", result);
  501. //>>includeEnd('debug');
  502. result.x = cartesian.x / scalar;
  503. result.y = cartesian.y / scalar;
  504. return result;
  505. };
  506. /**
  507. * Negates the provided Cartesian.
  508. *
  509. * @param {Cartesian2} cartesian The Cartesian to be negated.
  510. * @param {Cartesian2} result The object onto which to store the result.
  511. * @returns {Cartesian2} The modified result parameter.
  512. */
  513. Cartesian2.negate = function (cartesian, result) {
  514. //>>includeStart('debug', pragmas.debug);
  515. Check.typeOf.object("cartesian", cartesian);
  516. Check.typeOf.object("result", result);
  517. //>>includeEnd('debug');
  518. result.x = -cartesian.x;
  519. result.y = -cartesian.y;
  520. return result;
  521. };
  522. /**
  523. * Computes the absolute value of the provided Cartesian.
  524. *
  525. * @param {Cartesian2} cartesian The Cartesian whose absolute value is to be computed.
  526. * @param {Cartesian2} result The object onto which to store the result.
  527. * @returns {Cartesian2} The modified result parameter.
  528. */
  529. Cartesian2.abs = function (cartesian, result) {
  530. //>>includeStart('debug', pragmas.debug);
  531. Check.typeOf.object("cartesian", cartesian);
  532. Check.typeOf.object("result", result);
  533. //>>includeEnd('debug');
  534. result.x = Math.abs(cartesian.x);
  535. result.y = Math.abs(cartesian.y);
  536. return result;
  537. };
  538. const lerpScratch = new Cartesian2();
  539. /**
  540. * Computes the linear interpolation or extrapolation at t using the provided cartesians.
  541. *
  542. * @param {Cartesian2} start The value corresponding to t at 0.0.
  543. * @param {Cartesian2} end The value corresponding to t at 1.0.
  544. * @param {Number} t The point along t at which to interpolate.
  545. * @param {Cartesian2} result The object onto which to store the result.
  546. * @returns {Cartesian2} The modified result parameter.
  547. */
  548. Cartesian2.lerp = function (start, end, t, result) {
  549. //>>includeStart('debug', pragmas.debug);
  550. Check.typeOf.object("start", start);
  551. Check.typeOf.object("end", end);
  552. Check.typeOf.number("t", t);
  553. Check.typeOf.object("result", result);
  554. //>>includeEnd('debug');
  555. Cartesian2.multiplyByScalar(end, t, lerpScratch);
  556. result = Cartesian2.multiplyByScalar(start, 1.0 - t, result);
  557. return Cartesian2.add(lerpScratch, result, result);
  558. };
  559. const angleBetweenScratch = new Cartesian2();
  560. const angleBetweenScratch2 = new Cartesian2();
  561. /**
  562. * Returns the angle, in radians, between the provided Cartesians.
  563. *
  564. * @param {Cartesian2} left The first Cartesian.
  565. * @param {Cartesian2} right The second Cartesian.
  566. * @returns {Number} The angle between the Cartesians.
  567. */
  568. Cartesian2.angleBetween = function (left, right) {
  569. //>>includeStart('debug', pragmas.debug);
  570. Check.typeOf.object("left", left);
  571. Check.typeOf.object("right", right);
  572. //>>includeEnd('debug');
  573. Cartesian2.normalize(left, angleBetweenScratch);
  574. Cartesian2.normalize(right, angleBetweenScratch2);
  575. return CesiumMath.acosClamped(
  576. Cartesian2.dot(angleBetweenScratch, angleBetweenScratch2)
  577. );
  578. };
  579. const mostOrthogonalAxisScratch = new Cartesian2();
  580. /**
  581. * Returns the axis that is most orthogonal to the provided Cartesian.
  582. *
  583. * @param {Cartesian2} cartesian The Cartesian on which to find the most orthogonal axis.
  584. * @param {Cartesian2} result The object onto which to store the result.
  585. * @returns {Cartesian2} The most orthogonal axis.
  586. */
  587. Cartesian2.mostOrthogonalAxis = function (cartesian, result) {
  588. //>>includeStart('debug', pragmas.debug);
  589. Check.typeOf.object("cartesian", cartesian);
  590. Check.typeOf.object("result", result);
  591. //>>includeEnd('debug');
  592. const f = Cartesian2.normalize(cartesian, mostOrthogonalAxisScratch);
  593. Cartesian2.abs(f, f);
  594. if (f.x <= f.y) {
  595. result = Cartesian2.clone(Cartesian2.UNIT_X, result);
  596. } else {
  597. result = Cartesian2.clone(Cartesian2.UNIT_Y, result);
  598. }
  599. return result;
  600. };
  601. /**
  602. * Compares the provided Cartesians componentwise and returns
  603. * <code>true</code> if they are equal, <code>false</code> otherwise.
  604. *
  605. * @param {Cartesian2} [left] The first Cartesian.
  606. * @param {Cartesian2} [right] The second Cartesian.
  607. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  608. */
  609. Cartesian2.equals = function (left, right) {
  610. return (
  611. left === right ||
  612. (defined(left) &&
  613. defined(right) &&
  614. left.x === right.x &&
  615. left.y === right.y)
  616. );
  617. };
  618. /**
  619. * @private
  620. */
  621. Cartesian2.equalsArray = function (cartesian, array, offset) {
  622. return cartesian.x === array[offset] && cartesian.y === array[offset + 1];
  623. };
  624. /**
  625. * Compares the provided Cartesians componentwise and returns
  626. * <code>true</code> if they pass an absolute or relative tolerance test,
  627. * <code>false</code> otherwise.
  628. *
  629. * @param {Cartesian2} [left] The first Cartesian.
  630. * @param {Cartesian2} [right] The second Cartesian.
  631. * @param {Number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  632. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  633. * @returns {Boolean} <code>true</code> if left and right are within the provided epsilon, <code>false</code> otherwise.
  634. */
  635. Cartesian2.equalsEpsilon = function (
  636. left,
  637. right,
  638. relativeEpsilon,
  639. absoluteEpsilon
  640. ) {
  641. return (
  642. left === right ||
  643. (defined(left) &&
  644. defined(right) &&
  645. CesiumMath.equalsEpsilon(
  646. left.x,
  647. right.x,
  648. relativeEpsilon,
  649. absoluteEpsilon
  650. ) &&
  651. CesiumMath.equalsEpsilon(
  652. left.y,
  653. right.y,
  654. relativeEpsilon,
  655. absoluteEpsilon
  656. ))
  657. );
  658. };
  659. /**
  660. * An immutable Cartesian2 instance initialized to (0.0, 0.0).
  661. *
  662. * @type {Cartesian2}
  663. * @constant
  664. */
  665. Cartesian2.ZERO = Object.freeze(new Cartesian2(0.0, 0.0));
  666. /**
  667. * An immutable Cartesian2 instance initialized to (1.0, 1.0).
  668. *
  669. * @type {Cartesian2}
  670. * @constant
  671. */
  672. Cartesian2.ONE = Object.freeze(new Cartesian2(1.0, 1.0));
  673. /**
  674. * An immutable Cartesian2 instance initialized to (1.0, 0.0).
  675. *
  676. * @type {Cartesian2}
  677. * @constant
  678. */
  679. Cartesian2.UNIT_X = Object.freeze(new Cartesian2(1.0, 0.0));
  680. /**
  681. * An immutable Cartesian2 instance initialized to (0.0, 1.0).
  682. *
  683. * @type {Cartesian2}
  684. * @constant
  685. */
  686. Cartesian2.UNIT_Y = Object.freeze(new Cartesian2(0.0, 1.0));
  687. /**
  688. * Duplicates this Cartesian2 instance.
  689. *
  690. * @param {Cartesian2} [result] The object onto which to store the result.
  691. * @returns {Cartesian2} The modified result parameter or a new Cartesian2 instance if one was not provided.
  692. */
  693. Cartesian2.prototype.clone = function (result) {
  694. return Cartesian2.clone(this, result);
  695. };
  696. /**
  697. * Compares this Cartesian against the provided Cartesian componentwise and returns
  698. * <code>true</code> if they are equal, <code>false</code> otherwise.
  699. *
  700. * @param {Cartesian2} [right] The right hand side Cartesian.
  701. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  702. */
  703. Cartesian2.prototype.equals = function (right) {
  704. return Cartesian2.equals(this, right);
  705. };
  706. /**
  707. * Compares this Cartesian against the provided Cartesian componentwise and returns
  708. * <code>true</code> if they pass an absolute or relative tolerance test,
  709. * <code>false</code> otherwise.
  710. *
  711. * @param {Cartesian2} [right] The right hand side Cartesian.
  712. * @param {Number} [relativeEpsilon=0] The relative epsilon tolerance to use for equality testing.
  713. * @param {Number} [absoluteEpsilon=relativeEpsilon] The absolute epsilon tolerance to use for equality testing.
  714. * @returns {Boolean} <code>true</code> if they are within the provided epsilon, <code>false</code> otherwise.
  715. */
  716. Cartesian2.prototype.equalsEpsilon = function (
  717. right,
  718. relativeEpsilon,
  719. absoluteEpsilon
  720. ) {
  721. return Cartesian2.equalsEpsilon(
  722. this,
  723. right,
  724. relativeEpsilon,
  725. absoluteEpsilon
  726. );
  727. };
  728. /**
  729. * Creates a string representing this Cartesian in the format '(x, y)'.
  730. *
  731. * @returns {String} A string representing the provided Cartesian in the format '(x, y)'.
  732. */
  733. Cartesian2.prototype.toString = function () {
  734. return `(${this.x}, ${this.y})`;
  735. };
  736. export default Cartesian2;