-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
95 lines (82 loc) · 3.26 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<!doctype html>
<html>
<head>
<title>polygon</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div><a href="https://github.com/arkenidar/point_in_polygon" target="_blank">
github repo :
arkenidar/point_in_polygon . </a>
</div>
<div> click on canvas to add vertices . </div>
<div> ( remember to not self-intersect and to use counter-clockwise ordering ) . </div>
<div> click on "reload" to clear canvas . </div>
<button onclick="location.reload()">reload</button>
<br>
<canvas id="canvas" style="border: 1px dotted black"></canvas>
<!-- geometric.js for Point-In-Polygon -->
<script src="geometric.js"></script>
<script>
const log = console.log;
const canvas_element = document.getElementById('canvas');
canvas_element.onclick = on_canvas_click;
let polygon_points = []; // vertices, polygon points
function on_canvas_click(click_event) {
function mouse_position(mouse_event) {
let rectangle = canvas_element.getBoundingClientRect();
return {
x: mouse_event.clientX - rectangle.left,
y: mouse_event.clientY - rectangle.top
};
}
const point = mouse_position(click_event);
polygon_points.push(point);
set_polygon_points(polygon_points);
}
// *** 2D canvas
const canvas_2d = canvas_element.getContext("2d");
clear_canvas();
function clear_canvas() {
canvas_2d.clearRect(0, 0, canvas_element.width, canvas_element.height);
}
function set_polygon_points(polygon_points) {
clear_canvas();
// first draw polygon, then draw vertices "big pixels"
draw_polygon(polygon_points);
draw_vertices(polygon_points);
}
function draw_polygon(polygon, fillStyle = 'grey') {
// *** draw pixel
const draw_pixel = (x, y, s = 1) => {
canvas_2d.fillRect(x, y, s, s)
};
let { x_min, y_min, x_max, y_max } = bounds_of_polygon(polygon);
// *** fill polygon
canvas_2d.fillStyle = fillStyle;
for (let x = x_min; x <= x_max; x++)
for (let y = y_min; y <= y_max; y++) {
if (in_concave_polygon({ x, y }, polygon))
draw_pixel(x, y)
}
}
// *** draw "big pixels" for vertices ( in cycled colors )
function draw_vertices(p) {
let draw_big_pixel = (px) => canvas_2d.fillRect(px.x - 5, px.y - 5, 10, 10);
for (const [index, point] of p.entries()) {
canvas_2d.fillStyle = colors_cycled(index); // color of vertices
draw_big_pixel(point);
}
}
function colors_cycled(index) {
let colors = ['red', 'green', 'blue', 'yellow', 'orange', 'violet'];
return colors[index % colors.length]
}
</script>
<hr>
<div>source code :</div>
<script src="show-source.js" data-href="geometric.js"></script>
<script src="show-source.js" data-href="index.html"></script>
</body>
</html>