Debian Codegraph @

bqplot/js/src/lasso_test.ts

raw
1 /* Based on http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
2 *
3 * This file is licensed under the following license:
4 *
5 * Copyright (c) 1970-2003, Wm. Randolph Franklin
6 *
7 * Permission is hereby granted, free of charge, to any person
8 * obtaining a copy of this software and associated documentation
9 * files (the "Software"), to deal in the Software without
10 * restriction, including without limitation the rights to use, copy,
11 * modify, merge, publish, distribute, sublicense, and/or sell copies
12 * of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimers.
17 * 2. Redistributions in binary form must reproduce the above
18 * copyright notice in the documentation and/or other materials
19 * provided with the distribution.
20 * 3. The name of W. Randolph Franklin may not be used to endorse or
21 * promote products derived from this Software without specific prior
22 * written permission.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 */
33
34 export function point_in_lasso(point, vertices) {
35 // Checks if a point is in lasso
36 let xi,
37 xj,
38 yi,
39 yj,
40 intersect,
41 x = point[0],
42 y = point[1],
43 is_inside = false;
44
45 for (let i = 0, j = vertices.length - 1; i < vertices.length; j = i++) {
46 (xi = vertices[i][0]),
47 (yi = vertices[i][1]),
48 (xj = vertices[j][0]),
49 (yj = vertices[j][1]),
50 (intersect =
51 yi > y != yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi);
52 if (intersect) {
53 is_inside = !is_inside;
54 }
55 }
56 return is_inside;
57 }