Debian Codegraph @

bqplot/js/src/selector_utils.ts

raw
1 /* Copyright 2015 Bloomberg Finance L.P.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 /* This module contains elementary geometric functions to determine whether
17 * shapes are contained within selectors.
18 */
19
20 export { point_in_lasso } from './lasso_test';
21
22 export function point_in_rectangle(point, x, y) {
23 // Checks whether `point` is within the rectangle of coordinates
24 // (x0, y0) (x1, y0) (x1, y1) (x0, y1)
25 // If one of x or y is undefined, treat them as [-inf, inf]
26
27 if (x.length == 0 && y.length == 0) {
28 return false;
29 }
30
31 let is_inside = true;
32
33 if (x.length != 0) {
34 is_inside = is_inside && x[0] <= point[0] && point[0] <= x[1];
35 }
36 if (y.length != 0) {
37 is_inside = is_inside && y[0] <= point[1] && point[1] <= y[1];
38 }
39 return is_inside;
40 }
41
42 export function rect_inter_rect(x0, y0, x1, y1) {
43 // Checks whether two rectangles intersect
44 return seg_inter_seg(x0, x1) && seg_inter_seg(y0, y1);
45 }
46
47 export function lasso_inter_rect(x, y, vertices) {
48 // checks whether the lasso intersects the rectangle of coordinates
49 // (x0, y0) (x1, y0) (x1, y1) (x0, y1)
50
51 for (let i = 0; i < vertices.length; i++) {
52 if (point_in_rectangle(vertices[i], x, y)) {
53 return true;
54 }
55 }
56 return false;
57 }
58
59 function seg_inter_seg(p, q) {
60 // Checks whether the 1d segments [p0, p1] and [q0, q1] intersect
61 // If one of the segments is empty, treat it as [-inf, inf]
62 if (p.length == 0 || q.length == 0) {
63 return p.length != 0 || q.length != 0;
64 }
65 p.sort((a, b) => {
66 return a - b;
67 });
68 q.sort((a, b) => {
69 return a - b;
70 });
71 return (
72 p[0] < q[0] != p[0] < q[1] ||
73 p[1] < q[0] != p[1] < q[1] ||
74 q[0] < p[0] != q[0] < p[1] ||
75 q[1] < p[0] != q[1] < p[1]
76 );
77 }