blob: 27cd7f1049cfd9f35f8f5cb749f4ca3a6987c911 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001/* Copyright 2016 The Chromium Authors. All Rights Reserved.
2 *
3 * Use of this source code is governed by a BSD-style
4 * license that can be found in the LICENSE file or at
5 * https://developers.google.com/open-source/licenses/bsd
6 */
7
8// shape related classes
9
10/** a point in 2 cartesian dimensions.
11 * @constructor
12 * @param x x-coord.
13 * @param y y-coord.
14 * @param opt_coordinateFrame a key that can be passed to a translation function to
15 * convert from one coordinate frame to another.
16 * Coordinate frames might correspond to things like windows, iframes, or
17 * any element with a position style attribute.
18 */
19function Point(x, y, opt_coordinateFrame) {
20 /** a numeric x coordinate. */
21 this.x = x;
22 /** a numeric y coordinate. */
23 this.y = y;
24 /** a key that can be passed to a translation function to
25 * convert from one coordinate frame to another.
26 * Coordinate frames might correspond to things like windows, iframes, or
27 * any element with a position style attribute.
28 */
29 this.coordinateFrame = opt_coordinateFrame || null;
30}
31Point.prototype.toString = function() {
32 return '[P ' + this.x + ',' + this.y + ']';
33};
34Point.prototype.clone = function() {
35 return new Point(this.x, this.y, this.coordinateFrame);
36};
37
38/** a distance between two points in 2-space in cartesian form.
39 * A delta doesn't have a coordinate frame associated since all the coordinate
40 * frames used in the HTML dom are convertible without rotation/scaling.
41 * If a delta is not being used in pixel-space then it may be annotated with
42 * a coordinate frame, and the undefined coordinate frame can be assumed
43 * to represent pixel space.
44 * @constructor
45 * @param dx distance along x axis
46 * @param dy distance along y axis
47 */
48function Delta(dx, dy) {
49 /** a numeric distance along the x dimension. */
50 this.dx = dx;
51 /** a numeric distance along the y dimension. */
52 this.dy = dy;
53}
54Delta.prototype.toString = function() {
55 return '[D ' + this.dx + ',' + this.dy + ']';
56};
57
58/** a rectangle or bounding region.
59 * @constructor
60 * @param x x-coord of the left edge.
61 * @param y y-coord of the top edge.
62 * @param w width.
63 * @param h height.
64 * @param opt_coordinateFrame a key that can be passed to a translation function to
65 * convert from one coordinate frame to another.
66 * Coordinate frames might correspond to things like windows, iframes, or
67 * any element with a position style attribute.
68 */
69function Rect(x, y, w, h, opt_coordinateFrame) {
70 /** the numeric x coordinate of the left edge. */
71 this.x = x;
72 /** the numeric y coordinate of the top edge. */
73 this.y = y;
74 /** the numeric distance between the right edge and the left. */
75 this.w = w;
76 /** the numeric distance between the top edge and the bottom. */
77 this.h = h;
78 /** a key that can be passed to a translation function to
79 * convert from one coordinate frame to another.
80 * Coordinate frames might correspond to things like windows, iframes, or
81 * any element with a position style attribute.
82 */
83 this.coordinateFrame = opt_coordinateFrame || null;
84}
85
86/**
87 * Determines whether the Rectangle contains the Point.
88 * The Point is considered "contained" if it lies
89 * on the boundary of, or in the interior of, the Rectangle.
90 *
91 * @param {Point} p
92 * @return boolean indicating if this Rect contains p
93 */
94Rect.prototype.contains = function(p) {
95 return this.x <= p.x && p.x < (this.x + this.w) &&
96 this.y <= p.y && p.y < (this.y + this.h);
97};
98
99/**
100 * Determines whether the given rectangle intersects this rectangle.
101 *
102 * @param {Rect} r
103 * @return boolean indicating if this the two rectangles intersect
104 */
105Rect.prototype.intersects = function(r) {
106 let p = function(x, y) {
107 return new Point(x, y, null);
108 };
109
110 return this.contains(p(r.x, r.y)) ||
111 this.contains(p(r.x + r.w, r.y)) ||
112 this.contains(p(r.x + r.w, r.y + r.h)) ||
113 this.contains(p(r.x, r.y + r.h)) ||
114 r.contains(p(this.x, this.y)) ||
115 r.contains(p(this.x + this.w, this.y)) ||
116 r.contains(p(this.x + this.w, this.y + this.h)) ||
117 r.contains(p(this.x, this.y + this.h));
118};
119
120Rect.prototype.toString = function() {
121 return '[R ' + this.w + 'x' + this.h + '+' + this.x + '+' + this.y + ']';
122};
123
124Rect.prototype.clone = function() {
125 return new Rect(this.x, this.y, this.w, this.h, this.coordinateFrame);
126};