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