-
Notifications
You must be signed in to change notification settings - Fork 20
/
sketch.js
executable file
·176 lines (159 loc) · 4.4 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Convert a circle into a square
// by treating points along the perimeter as a series of springy particles
// Golan Levin, August 2016
var nPoints, quarter, offset;
var radius;
var nSquarePoints = 4;
var squarePoints = []; // the vertices of the square
var srcPoints = []; // points along the circle
var dstPoints = []; // points along the triangle
var targetPts = [];
var particles = [];
var target = 1;
var DAMPING = 0.97;
var MASS = 10;
var NEIGHBOR = 0.5;
var SWITCHTHRESH = 15;
//-----------------------------------------
function setup() {
createCanvas(400, 400);
nPoints = 60;
quarter = (nPoints / nSquarePoints);
offset = 0;
radius = width / 2 * 0.75;
for (var i = 0; i < nSquarePoints; i++) { // triangle vertices
var x = radius * cos(i * TWO_PI / nSquarePoints - HALF_PI);
var y = radius * sin(i * TWO_PI / nSquarePoints - HALF_PI);
squarePoints[i] = {
x, y
};
}
// compute srcPoints: points on the circle.
for (var j = 0; j < nPoints; j++) {
var t = map(j, 0, nPoints, 0, TWO_PI);
var x = radius * cos(t);
var y = radius * sin(t);
srcPoints[j] = {
x, y
};
}
// compute dstPoints: points along the square
for (var j = 0; j < nPoints; j++) {
var i = (floor((j + nPoints - offset) / quarter) + 1) % nSquarePoints;
var p1x = squarePoints[(i + 0) % nSquarePoints].x;
var p1y = squarePoints[(i + 0) % nSquarePoints].y;
var p2x = squarePoints[(i + 1) % nSquarePoints].x;
var p2y = squarePoints[(i + 1) % nSquarePoints].y;
var jt = (j + nPoints - (offset - 0)) % quarter;
var x = map(jt, 0, quarter, p1x, p2x);
var y = map(jt, 0, quarter, p1y, p2y);
dstPoints[j] = {
x, y
};
targetPts[j] = {
x, y
}
}
for (var j = 0; j < nPoints; j++) {
var px = srcPoints[j].x;
var py = srcPoints[j].y;
particles[j] = new Particle(px, py, 0, 0);
}
}
//-----------------------------------------
function draw() {
background(255);
noFill();
push();
translate(width/2, height/2);
rotate(PI * 0.25);
stroke(0);
strokeWeight(3);
strokeJoin(ROUND);
var error = 0;
for (var j = 0; j < nPoints; j++) {
var px = particles[j].px;
var py = particles[j].py;
var tx = targetPts[j].x;
var ty = targetPts[j].y;
var dx = tx - px;
var dy = ty - py;
var dh = sqrt(dx * dx + dy * dy);
error += dh;
if (dh > 0) {
var fx = dx / dh;
var fy = dy / dh;
particles[j].applyForce(fx, fy);
}
}
print(error);
if (error < SWITCHTHRESH) {
flipTarget();
}
for (var j = 0; j < nPoints; j++) {
var ix = particles[(j - 1 + nPoints) % nPoints].px;
var iy = particles[(j - 1 + nPoints) % nPoints].py;
var jx = particles[(j + 0 + nPoints) % nPoints].px;
var jy = particles[(j + 0 + nPoints) % nPoints].py;
var kx = particles[(j + 1 + nPoints) % nPoints].px;
var ky = particles[(j + 1 + nPoints) % nPoints].py;
var ijdx = ix - jx;
var ijdy = iy - jy;
var ijdh = sqrt(ijdx * ijdx + ijdy * ijdy);
if (ijdh > 0) {
var ifx = ijdx / ijdh * NEIGHBOR;
var ify = ijdy / ijdh * NEIGHBOR;
particles[j].applyForce(ifx, ify);
}
var kjdx = kx - jx;
var kjdy = ky - jy;
var kjdh = sqrt(kjdx * kjdx + kjdy * kjdy);
if (kjdh > 0) {
var kfx = kjdx / kjdh * NEIGHBOR;
var kfy = kjdy / kjdh * NEIGHBOR;
particles[j].applyForce(kfx, kfy);
}
}
for (var j = 0; j < nPoints; j++) {
particles[j].update();
}
var ofs = 2;
beginShape(); // render particles
var px = particles[(0 + ofs) % nPoints].px;
var py = particles[(0 + ofs) % nPoints].py;
curveVertex(px, py);
for (var j = 0; j < nPoints; j++) {
px = particles[(j + ofs) % nPoints].px;
py = particles[(j + ofs) % nPoints].py;
curveVertex(px, py);
}
endShape(CLOSE);
}
//-----------------------------------------
function flipTarget() {
for (var j = 0; j < nPoints; j++) {
x = (target === 1) ? dstPoints[j].x : srcPoints[j].x;
y = (target === 1) ? dstPoints[j].y : srcPoints[j].y;
targetPts[j] = {
x, y
}
}
target = 1 - target;
}
//-----------------------------------------
function Particle(px, py, vx, vy) {
this.px = px;
this.py = py;
this.vx = vx;
this.vy = vy;
this.applyForce = function(fx, fy) {
this.vx += fx / MASS;
this.vy += fy / MASS;
}
this.update = function() {
this.vx *= DAMPING;
this.vy *= DAMPING;
this.px += this.vx;
this.py += this.vy;
}
}