Skip to content

Commit

Permalink
Immutability!
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed May 24, 2016
1 parent d8646cb commit 547196b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,11 @@ Note that the order of transformations matters! The translate must be applied be

<a href="#transform_scale" name="transform_scale">#</a> <i>transform</i>.<b>scale</b>(<i>k</i>)

Returns a new transform, multiplying this transform’s scale *k* by the specified number *k*. The returned transform’s scale is equal to *k₀* × *k*.
Returns a transform whose scale *k* is equal to *k₀* × *k*, where *k₀* is this transform’s scale.

<a href="#transform_translate" name="transform_translate">#</a> <i>transform</i>.<b>translate</b>(<i>x</i>, <i>y</i>)

Returns a new transform, incrementing this transform’s translation *t<sub>x0</sub>* and *t<sub>y0</sub>* by the specified numbers *x* and *y*, respectively. The returned transform’s translation is equal to *t<sub>x0</sub>* + *x* and *t<sub>y0</sub>* + *y*.
Returns a transform whose translation *t<sub>x1</sub>* and *t<sub>y1</sub>* is equal to *t<sub>x0</sub>* + *x* and *t<sub>y0</sub>* + *y*, where *t<sub>x0</sub>* and *t<sub>y0</sub>* is this transform’s translation.

<a href="#transform_apply" name="transform_apply">#</a> <i>transform</i>.<b>apply</b>(<i>point</i>)

Expand Down
4 changes: 2 additions & 2 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ export function Transform(k, x, y) {
Transform.prototype = {
constructor: Transform,
scale: function(k) {
return new Transform(this.k * k, this.x, this.y);
return k === 1 ? this : new Transform(this.k * k, this.x, this.y);
},
translate: function(x, y) {
return new Transform(this.k, this.x + this.k * x, this.y + this.k * y);
return x === 0 & y === 0 ? this : new Transform(this.k, this.x + this.k * x, this.y + this.k * y);
},
apply: function(point) {
return [point[0] * this.k + this.x, point[1] * this.k + this.y];
Expand Down

0 comments on commit 547196b

Please sign in to comment.