Skip to content

Commit

Permalink
Because rotation will be applied to the canvas, it needs to be optional
Browse files Browse the repository at this point in the history
By default, we don't actually want to include rotation in when projecting/unprojecting
  • Loading branch information
bhousel committed Feb 26, 2024
1 parent 98d6235 commit c8e7ad5
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 284 deletions.
10 changes: 5 additions & 5 deletions packages/math/src/Viewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ export class Viewport {
* view.project([-180, 85.0511287798]); // returns [-256, -256]
* ```
*/
project(loc: Vec2): Vec2 {
project(loc: Vec2, includeRotation?: boolean): Vec2 {
const { x, y, k, r } = this._transform;
const lambda: number = loc[0] * DEG2RAD;
const phi: number = numClamp(loc[1] * DEG2RAD, MIN_PHI, MAX_PHI);
const mercatorX: number = lambda
const mercatorY: number = Math.log(Math.tan((HALF_PI + phi) / 2));
const point: Vec2 = [mercatorX * k + x, y - mercatorY * k];
if (r) {
if (includeRotation && r) {
return vecRotate(point, r, this._dimensions.center());
} else {
return point;
Expand All @@ -82,9 +82,9 @@ export class Viewport {
* view.unproject([-256, -256]); // returns [-180, 85.0511287798]
* ```
*/
unproject(point: Vec2): Vec2 {
unproject(point: Vec2, includeRotation?: boolean): Vec2 {
const { x, y, k, r } = this._transform;
if (r) {
if (includeRotation && r) {
point = vecRotate(point, -r, this._dimensions.center());
}
const mercatorX: number = (point[0] - x) / k;
Expand Down Expand Up @@ -198,7 +198,7 @@ export class Viewport {
const extent = new Extent();

for (let i = 0; i < polygon.length - 1; i++) { // skip last point, it's first point repeated
extent.extendSelf(this.unproject(polygon[i]));
extent.extendSelf(this.unproject(polygon[i], true));
}
return extent;
}
Expand Down

0 comments on commit c8e7ad5

Please sign in to comment.