-
Notifications
You must be signed in to change notification settings - Fork 20
/
sketch.js
executable file
·84 lines (74 loc) · 2.25 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
// Convert a circle into a square
// by gradually shrinking the circle's radius,
// revealing square corners within.
// Golan Levin, August 2016
var radius;
var squarePoints = []; // the vertices of the square
var nSquarePoints = 4;
var quarter;
//-----------------------------------------
function setup() {
createCanvas(400, 400);
radius = width / 2 * 0.75;
quarter = TWO_PI / nSquarePoints;
for (var i = 0; i < nSquarePoints; i++) { // triangle vertices
var x = radius * cos(i * quarter - HALF_PI);
var y = radius * sin(i * quarter - HALF_PI);
squarePoints[i] = {
x, y
};
}
}
//-----------------------------------------
function draw() {
background(255);
push();
translate(width / 2, height / 2);
rotate(PI * 0.25);
strokeJoin(ROUND);
noFill();
stroke(0);
strokeWeight(3);
var minpct = sqrt(2.0) / 2.0;
var amount = map(sin(millis() / 2000.0), -1, 1, minpct, 1.0);
var rad = amount * radius;
var nPts = 60;
// http://mathworld.wolfram.com/Circle-LineIntersection.html
for (var i = 0; i < nSquarePoints; i++) {
var x1 = squarePoints[(i + 0) % nSquarePoints].x;
var y1 = squarePoints[(i + 0) % nSquarePoints].y;
var x2 = squarePoints[(i + 1) % nSquarePoints].x;
var y2 = squarePoints[(i + 1) % nSquarePoints].y;
var dx = x2 - x1;
var dy = y2 - y1;
var dr = sqrt(dx * dx + dy * dy);
var D = (x1 * y2) - (x2 * y1);
var discriminant = rad * rad * dr * dr - D * D;
if (discriminant <= 0) {
line(x1 + cx, y1 + cy, x2 + cx, y2 + cy);
} else {
var px = (D * dy + ((dy < 0) ? -1 : 1) * dx * sqrt(discriminant)) / (dr * dr);
var py = (-D * dx + abs(dy) * sqrt(discriminant)) / (dr * dr);
var qx = (D * dy - ((dy < 0) ? -1 : 1) * dx * sqrt(discriminant)) / (dr * dr);
var qy = (-D * dx - abs(dy) * sqrt(discriminant)) / (dr * dr);
var pAng = atan2(py, px);
var qAng = atan2(qy, qx);
if (i > 1) {
var tmp = pAng;
pAng = qAng;
qAng = tmp;
}
beginShape();
vertex(x2, y2);
for (var j = 0; j <= nPts; j++) {
var t = map(j, 0, nPts, pAng, qAng);
var tx = rad * cos(t);
var ty = rad * sin(t);
vertex(tx, ty);
}
vertex(x1, y1);
endShape();
}
}
pop();
}