Skip to content

fix missed z in translate, maptalks/issues#741 #2425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions src/geo/Coordinate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ class Coordinate extends Position {
* @param y - coordinate to add
* @returns result
*/
add(x: number, y: number): Coordinate;
add(x: number, y: number, z?: number): Coordinate;

/**
* 与传入坐标相加,返回一个新 Coordinate
Expand All @@ -226,19 +226,28 @@ class Coordinate extends Position {
* @param y - coordinate to add
* @returns result
*/
add(x: any, y?: number) {
let nx, ny;
add(x: any, y?: number, z?: number) {
let nx, ny, nz = this.z;
if (!isNil(x.x)) {
nx = this.x + x.x;
ny = this.y + x.y;
if (!isNil(x.z)) {
nz = (this.z || 0) + x.z;
}
} else if (!isNil(x[0])) {
nx = this.x + x[0];
ny = this.y + x[1];
if (!isNil(x[2])) {
nz = (this.z || 0) + x[2];
}
} else {
nx = this.x + x;
ny = this.y + y;
if (!isNil(z)) {
nz = (this.z || 0) + z;
}
}
return new Coordinate(nx, ny);
return new Coordinate(nx, ny, nz);
}

/**
Expand All @@ -260,9 +269,10 @@ class Coordinate extends Position {
* Returns the result of subtraction of another coordinate.
* @param x - coordinate to add
* @param y - coordinate to add
* @param z - altitude to add
* @returns result
*/
sub(x: number, y: number): Coordinate;
sub(x: number, y: number, z?: number): Coordinate;

/**
* 与传入坐标相减,返回一个新 Coordinate。
Expand All @@ -274,19 +284,28 @@ class Coordinate extends Position {
* @param [y=undefined] - optional, coordinate to add
* @returns result
*/
sub(x: any, y?: number): any {
let nx, ny;
sub(x: any, y?: number, z?: number): any {
let nx, ny, nz = this.z;
if (!isNil(x.x)) {
nx = this.x - x.x;
ny = this.y - x.y;
if (!isNil(x.z)) {
nz = (this.z || 0) - x.z;
}
} else if (!isNil(x[0])) {
nx = this.x - x[0];
ny = this.y - x[1];
if (!isNil(x[2])) {
nz = (this.z || 0) - x[2];
}
} else {
nx = this.x - x;
ny = this.y - y;
if (!isNil(z)) {
nz = (this.z || 0) - z;
}
}
return new Coordinate(nx, ny);
return new Coordinate(nx, ny, nz);
}

/**
Expand All @@ -295,7 +314,7 @@ class Coordinate extends Position {
* @returns result
*/
multi(ratio: number) {
return new Coordinate(this.x * ratio, this.y * ratio);
return new Coordinate(this.x * ratio, this.y * ratio, isNil(this.z) ? this.z : this.z * ratio);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/geometry/Geometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -942,16 +942,17 @@ export class Geometry extends JSONAble(Eventable(Handlerable(Class))) {
*
* @param {Number} x - x offset
* @param {Number} y - y offset
* @param {Number} z - z offset
* @return {Geometry} this
* @fires Geometry#positionchange
* @fires Geometry#shapechange
*/
translate(x: number | Coordinate, y?: number): this {
translate(x: number | Coordinate, y?: number, z?: number): this {
if (isNil(x)) {
return this;
}
const offset = new Coordinate(x as number, y);
if (offset.x === 0 && offset.y === 0) {
const offset = new Coordinate(x as number, y, z);
if (offset.x === 0 && offset.y === 0 && (isNil(offset.z) || offset.z === 0)) {
return this;
}
const coordinates: any = this.getCoordinates();
Expand Down
4 changes: 2 additions & 2 deletions test/geometry/animation/GeometryAnimationSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('Geometry.Animation', function () {
var expected = GEN_GEOMETRIES_OF_ALL_TYPES();
var i;
for (i = 0; i < expected.length; i++) {
expected[i].translate([0.01, 0.01]);
expected[i].translate([0.01, 0.01, 2]);
}
var geometries = GEN_GEOMETRIES_OF_ALL_TYPES();
var counter = 0;
Expand All @@ -26,7 +26,7 @@ describe('Geometry.Animation', function () {
}
for (i = 0; i < geometries.length; i++) {
var player = geometries[i].animate({
translate : new maptalks.Coordinate(0.01, 0.01)
translate : [0.01, 0.01, 2]
}, { duration : animSpeed }, cmp);
expect(player).to.be.ok();
}
Expand Down