1 | import {Point} from './Point';
|
2 | import * as Util from '../core/Util';
|
3 |
|
4 | /*
|
5 | * @class Transformation
|
6 | * @aka L.Transformation
|
7 | *
|
8 | * Represents an affine transformation: a set of coefficients `a`, `b`, `c`, `d`
|
9 | * for transforming a point of a form `(x, y)` into `(a*x + b, c*y + d)` and doing
|
10 | * the reverse. Used by Leaflet in its projections code.
|
11 | *
|
12 | * @example
|
13 | *
|
14 | * ```js
|
15 | * var transformation = L.transformation(2, 5, -1, 10),
|
16 | * p = L.point(1, 2),
|
17 | * p2 = transformation.transform(p), // L.point(7, 8)
|
18 | * p3 = transformation.untransform(p2); // L.point(1, 2)
|
19 | * ```
|
20 | */
|
21 |
|
22 |
|
23 | // factory new L.Transformation(a: Number, b: Number, c: Number, d: Number)
|
24 | // Creates a `Transformation` object with the given coefficients.
|
25 | export function Transformation(a, b, c, d) {
|
26 | if (Util.isArray(a)) {
|
27 | // use array properties
|
28 | this._a = a[0];
|
29 | this._b = a[1];
|
30 | this._c = a[2];
|
31 | this._d = a[3];
|
32 | return;
|
33 | }
|
34 | this._a = a;
|
35 | this._b = b;
|
36 | this._c = c;
|
37 | this._d = d;
|
38 | }
|
39 |
|
40 | Transformation.prototype = {
|
41 | // @method transform(point: Point, scale?: Number): Point
|
42 | // Returns a transformed point, optionally multiplied by the given scale.
|
43 | // Only accepts actual `L.Point` instances, not arrays.
|
44 | transform: function (point, scale) { // (Point, Number) -> Point
|
45 | return this._transform(point.clone(), scale);
|
46 | },
|
47 |
|
48 | // destructive transform (faster)
|
49 | _transform: function (point, scale) {
|
50 | scale = scale || 1;
|
51 | point.x = scale * (this._a * point.x + this._b);
|
52 | point.y = scale * (this._c * point.y + this._d);
|
53 | return point;
|
54 | },
|
55 |
|
56 | // @method untransform(point: Point, scale?: Number): Point
|
57 | // Returns the reverse transformation of the given point, optionally divided
|
58 | // by the given scale. Only accepts actual `L.Point` instances, not arrays.
|
59 | untransform: function (point, scale) {
|
60 | scale = scale || 1;
|
61 | return new Point(
|
62 | (point.x / scale - this._b) / this._a,
|
63 | (point.y / scale - this._d) / this._c);
|
64 | }
|
65 | };
|
66 |
|
67 | // factory L.transformation(a: Number, b: Number, c: Number, d: Number)
|
68 |
|
69 | // @factory L.transformation(a: Number, b: Number, c: Number, d: Number)
|
70 | // Instantiates a Transformation object with the given coefficients.
|
71 |
|
72 | // @alternative
|
73 | // @factory L.transformation(coefficients: Array): Transformation
|
74 | // Expects an coefficients array of the form
|
75 | // `[a: Number, b: Number, c: Number, d: Number]`.
|
76 |
|
77 | export function toTransformation(a, b, c, d) {
|
78 | return new Transformation(a, b, c, d);
|
79 | }
|