-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
105 lines (81 loc) · 1.87 KB
/
sketch.js
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
96
97
98
99
100
101
102
103
104
105
// Points object
let Points = function() {
this.p = [];
this.show = function() {
noFill();
stroke(255);
for (let i = 0; i < p.length; i++)
ellipse(p[i].x, p[i].y, 4, 4);
if (p.length > 1) {
stroke(200);
fill(100);
beginShape();
for (let i = 0; i < p.length; i++)
vertex(p[i].x, p[i].y);
endShape();
}
}
this.add = function(x, y) {
p.push(createVector(x, y));
}
this.contain = function(point) {
let j = p.length - 1;
let result = false;
for (let i = 0; i < p.length; i++) {
let f_cond = p[i].y < point.y && p[j].y >= point.y || p[j].y < point.y && p[i].y >= point.y;
let s_cond = p[i].x + (point.y - p[i].y) / (p[j].y - p[i].y) * (p[j].x - p[i].x) < point.x;
if (f_cond && s_cond)
result = !result;
j = i;
}
return result;
}
return this;
}
let CheckPoint = function(x, y) {
this.x = x;
this.y = y;
this.r = 3;
this.is_inside = false;
this.update = function(is_inside) {
this.is_inside = is_inside;
}
this.show = function() {
stroke(255);
fill(this.is_inside ? 'rgb(201, 26, 37)' : 'rgb(13, 91, 216)');
ellipse(this.x, this.y, this.r * 2, this.r * 2);
}
}
let points;
let points_to_check;
function setup() {
createCanvas(600, 600);
points = Points();
points_to_check = [];
createSome(100);
}
function draw() {
background(57);
points.show();
if(points_to_check.length) {
for (let i = 0; i < points_to_check.length; i++) {
points_to_check[i].update(points.contain(points_to_check[i]));
points_to_check[i].show();
}
}
}
function createSome(amount) {
for (let i = 0; i < amount; i++)
points_to_check.push(new CheckPoint(random(width), random(height)));
}
function mousePressed() {
if (mouseButton === LEFT)
points.add(mouseX, mouseY);
}
function keyPressed() {
if (keyCode == CONTROL)
points_to_check.push(new CheckPoint(mouseX, mouseY));
if (keyCode == ESCAPE) {
points.clear();
}
}