-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsketch.js
More file actions
111 lines (85 loc) · 2.18 KB
/
Copy pathsketch.js
File metadata and controls
111 lines (85 loc) · 2.18 KB
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
106
107
108
109
110
111
let circles;
let img;
let pixels;
const path = 'assets/image.jpeg';
const funky = 1;
const codyFunky = 2;
function preload () {
img = loadImage(path);
}
function setup () {
createCanvas(398, 500);
pixelDensity(1);
img.loadPixels();
circles = [];
}
function draw () {
background(0);
frameRate(60);
let total = 15;
let count = 0;
let attempts = 0;
while (count < total) {
let newC = getNewCircle();
if (newC !== null) {
circles.push(newC);
count++;
}
attempts++;
if (attempts > 1000) {
noLoop();
}
}
circles.forEach(circle => {
if (circle.growing) {
if (circle.touchingScreenEdges()) {
circle.growing = false;
} else {
circles.forEach(other => {
checkIfCircleTouchingOtherCircle(circle, other);
});
}
}
circle.display();
circle.grow();
});
}
function getNewCircle () {
let withinOtherCircle = false;
const x = random(0, img.width);
const y = random(0, img.height);
circles.forEach(circle => {
withinOtherCircle = checkIfLocationWithinOtherCircle(x, y, circle);
});
if (!withinOtherCircle) {
const c = getCircleColor(x, y);
return new Circle(x, y, color(c));
} else {
return null;
}
}
function checkIfLocationWithinOtherCircle (x, y, circle) {
let within = false;
const distance = dist(x, y, circle.x, circle.y);
if (distance < circle.r) {
within = true;
}
return within;
}
function checkIfCircleTouchingOtherCircle (circle, other) {
if (circle !== other) {
const actualDistance = dist(circle.x, circle.y, other.x, other.y);
const buffer = 2;
const validDistance = circle.r + other.r;
if (actualDistance - buffer < validDistance) {
circle.growing = false;
}
}
}
function getCircleColor (x, y) {
const index = (int(x) + int(y) * img.width) * 4;
const r = img.pixels[index];
const g = img.pixels[index + 1];
const b = img.pixels[index + 2];
return color(r, g, b);
}