Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/third_party/closure/goog/math/coordinate.js
4527 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview A utility class for representing two-dimensional positions.
9
*/
10
11
12
goog.provide('goog.math.Coordinate');
13
14
goog.require('goog.math');
15
16
17
18
/**
19
* Class for representing coordinates and positions.
20
* @param {number=} opt_x Left, defaults to 0.
21
* @param {number=} opt_y Top, defaults to 0.
22
* @struct
23
* @constructor
24
*/
25
goog.math.Coordinate = function(opt_x, opt_y) {
26
'use strict';
27
/**
28
* X-value
29
* @type {number}
30
*/
31
this.x = (opt_x !== undefined) ? opt_x : 0;
32
33
/**
34
* Y-value
35
* @type {number}
36
*/
37
this.y = (opt_y !== undefined) ? opt_y : 0;
38
};
39
40
41
/**
42
* Returns a new copy of the coordinate.
43
* @return {!goog.math.Coordinate} A clone of this coordinate.
44
*/
45
goog.math.Coordinate.prototype.clone = function() {
46
'use strict';
47
return new goog.math.Coordinate(this.x, this.y);
48
};
49
50
51
if (goog.DEBUG) {
52
/**
53
* Returns a nice string representing the coordinate.
54
* @return {string} In the form (50, 73).
55
* @override
56
*/
57
goog.math.Coordinate.prototype.toString = function() {
58
'use strict';
59
return '(' + this.x + ', ' + this.y + ')';
60
};
61
}
62
63
64
/**
65
* Returns whether the specified value is equal to this coordinate.
66
* @param {*} other Some other value.
67
* @return {boolean} Whether the specified value is equal to this coordinate.
68
*/
69
goog.math.Coordinate.prototype.equals = function(other) {
70
'use strict';
71
return other instanceof goog.math.Coordinate &&
72
goog.math.Coordinate.equals(this, other);
73
};
74
75
76
/**
77
* Compares coordinates for equality.
78
* @param {goog.math.Coordinate} a A Coordinate.
79
* @param {goog.math.Coordinate} b A Coordinate.
80
* @return {boolean} True iff the coordinates are equal, or if both are null.
81
*/
82
goog.math.Coordinate.equals = function(a, b) {
83
'use strict';
84
if (a == b) {
85
return true;
86
}
87
if (!a || !b) {
88
return false;
89
}
90
return a.x == b.x && a.y == b.y;
91
};
92
93
94
/**
95
* Returns the distance between two coordinates.
96
* @param {!goog.math.Coordinate} a A Coordinate.
97
* @param {!goog.math.Coordinate} b A Coordinate.
98
* @return {number} The distance between `a` and `b`.
99
*/
100
goog.math.Coordinate.distance = function(a, b) {
101
'use strict';
102
var dx = a.x - b.x;
103
var dy = a.y - b.y;
104
return Math.sqrt(dx * dx + dy * dy);
105
};
106
107
108
/**
109
* Returns the magnitude of a coordinate.
110
* @param {!goog.math.Coordinate} a A Coordinate.
111
* @return {number} The distance between the origin and `a`.
112
*/
113
goog.math.Coordinate.magnitude = function(a) {
114
'use strict';
115
return Math.sqrt(a.x * a.x + a.y * a.y);
116
};
117
118
119
/**
120
* Returns the angle from the origin to a coordinate.
121
* @param {!goog.math.Coordinate} a A Coordinate.
122
* @return {number} The angle, in degrees, clockwise from the positive X
123
* axis to `a`.
124
*/
125
goog.math.Coordinate.azimuth = function(a) {
126
'use strict';
127
return goog.math.angle(0, 0, a.x, a.y);
128
};
129
130
131
/**
132
* Returns the squared distance between two coordinates. Squared distances can
133
* be used for comparisons when the actual value is not required.
134
*
135
* Performance note: eliminating the square root is an optimization often used
136
* in lower-level languages, but the speed difference is not nearly as
137
* pronounced in JavaScript (only a few percent.)
138
*
139
* @param {!goog.math.Coordinate} a A Coordinate.
140
* @param {!goog.math.Coordinate} b A Coordinate.
141
* @return {number} The squared distance between `a` and `b`.
142
*/
143
goog.math.Coordinate.squaredDistance = function(a, b) {
144
'use strict';
145
var dx = a.x - b.x;
146
var dy = a.y - b.y;
147
return dx * dx + dy * dy;
148
};
149
150
151
/**
152
* Returns the difference between two coordinates as a new
153
* goog.math.Coordinate.
154
* @param {!goog.math.Coordinate} a A Coordinate.
155
* @param {!goog.math.Coordinate} b A Coordinate.
156
* @return {!goog.math.Coordinate} A Coordinate representing the difference
157
* between `a` and `b`.
158
*/
159
goog.math.Coordinate.difference = function(a, b) {
160
'use strict';
161
return new goog.math.Coordinate(a.x - b.x, a.y - b.y);
162
};
163
164
165
/**
166
* Returns the sum of two coordinates as a new goog.math.Coordinate.
167
* @param {!goog.math.Coordinate} a A Coordinate.
168
* @param {!goog.math.Coordinate} b A Coordinate.
169
* @return {!goog.math.Coordinate} A Coordinate representing the sum of the two
170
* coordinates.
171
*/
172
goog.math.Coordinate.sum = function(a, b) {
173
'use strict';
174
return new goog.math.Coordinate(a.x + b.x, a.y + b.y);
175
};
176
177
178
/**
179
* Rounds the x and y fields to the next larger integer values.
180
* @return {!goog.math.Coordinate} This coordinate with ceil'd fields.
181
*/
182
goog.math.Coordinate.prototype.ceil = function() {
183
'use strict';
184
this.x = Math.ceil(this.x);
185
this.y = Math.ceil(this.y);
186
return this;
187
};
188
189
190
/**
191
* Rounds the x and y fields to the next smaller integer values.
192
* @return {!goog.math.Coordinate} This coordinate with floored fields.
193
*/
194
goog.math.Coordinate.prototype.floor = function() {
195
'use strict';
196
this.x = Math.floor(this.x);
197
this.y = Math.floor(this.y);
198
return this;
199
};
200
201
202
/**
203
* Rounds the x and y fields to the nearest integer values.
204
* @return {!goog.math.Coordinate} This coordinate with rounded fields.
205
*/
206
goog.math.Coordinate.prototype.round = function() {
207
'use strict';
208
this.x = Math.round(this.x);
209
this.y = Math.round(this.y);
210
return this;
211
};
212
213
214
/**
215
* Translates this box by the given offsets. If a `goog.math.Coordinate`
216
* is given, then the x and y values are translated by the coordinate's x and y.
217
* Otherwise, x and y are translated by `tx` and `opt_ty`
218
* respectively.
219
* @param {number|goog.math.Coordinate} tx The value to translate x by or the
220
* the coordinate to translate this coordinate by.
221
* @param {number=} opt_ty The value to translate y by.
222
* @return {!goog.math.Coordinate} This coordinate after translating.
223
*/
224
goog.math.Coordinate.prototype.translate = function(tx, opt_ty) {
225
'use strict';
226
if (tx instanceof goog.math.Coordinate) {
227
this.x += tx.x;
228
this.y += tx.y;
229
} else {
230
this.x += Number(tx);
231
if (typeof opt_ty === 'number') {
232
this.y += opt_ty;
233
}
234
}
235
return this;
236
};
237
238
239
/**
240
* Scales this coordinate by the given scale factors. The x and y values are
241
* scaled by `sx` and `opt_sy` respectively. If `opt_sy`
242
* is not given, then `sx` is used for both x and y.
243
* @param {number} sx The scale factor to use for the x dimension.
244
* @param {number=} opt_sy The scale factor to use for the y dimension.
245
* @return {!goog.math.Coordinate} This coordinate after scaling.
246
*/
247
goog.math.Coordinate.prototype.scale = function(sx, opt_sy) {
248
'use strict';
249
var sy = (typeof opt_sy === 'number') ? opt_sy : sx;
250
this.x *= sx;
251
this.y *= sy;
252
return this;
253
};
254
255
256
/**
257
* Rotates this coordinate clockwise about the origin (or, optionally, the given
258
* center) by the given angle, in radians.
259
* @param {number} radians The angle by which to rotate this coordinate
260
* clockwise about the given center, in radians.
261
* @param {!goog.math.Coordinate=} opt_center The center of rotation. Defaults
262
* to (0, 0) if not given.
263
*/
264
goog.math.Coordinate.prototype.rotateRadians = function(radians, opt_center) {
265
'use strict';
266
var center = opt_center || new goog.math.Coordinate(0, 0);
267
268
var x = this.x;
269
var y = this.y;
270
var cos = Math.cos(radians);
271
var sin = Math.sin(radians);
272
273
this.x = (x - center.x) * cos - (y - center.y) * sin + center.x;
274
this.y = (x - center.x) * sin + (y - center.y) * cos + center.y;
275
};
276
277
278
/**
279
* Rotates this coordinate clockwise about the origin (or, optionally, the given
280
* center) by the given angle, in degrees.
281
* @param {number} degrees The angle by which to rotate this coordinate
282
* clockwise about the given center, in degrees.
283
* @param {!goog.math.Coordinate=} opt_center The center of rotation. Defaults
284
* to (0, 0) if not given.
285
*/
286
goog.math.Coordinate.prototype.rotateDegrees = function(degrees, opt_center) {
287
'use strict';
288
this.rotateRadians(goog.math.toRadians(degrees), opt_center);
289
};
290
291