-
Notifications
You must be signed in to change notification settings - Fork 20
/
sketch.js
executable file
·78 lines (66 loc) · 1.94 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
// Convert a circle into a triangle
// by considering it as a set of alternating straight lines and arcs
// in which the arcs shrink while the lines grow.
// (Technically, a variant of Circle06)
// Golan Levin, January 2017
var radius;
var trianglePoints = [];
var nTrianglePoints = 3;
var bDrawDebug = false;
//-----------------------------------------
function setup() {
createCanvas(400, 400);
radius = width / 2 * 0.75;
for (var i = 0; i < nTrianglePoints; i++) { // triangle vertices
var x = radius * cos(i * TWO_PI / nTrianglePoints - HALF_PI);
var y = radius * sin(i * TWO_PI / nTrianglePoints - HALF_PI);
trianglePoints[i] = {
x, y
};
}
}
//-----------------------------------------
function draw() {
background(255);
noFill();
push();
translate(width / 2, height / 2);
rotate(PI);
var currentRadii01 = 0.5 + 0.5 * sin(millis() / 2000.0);
var rad = currentRadii01 * radius;
var phase = cos(millis() / 2000.0);
if (phase > 0){
scale(-1,1);
}
stroke(0);
strokeWeight(3);
strokeJoin(ROUND);
beginShape();
for (var i = 0; i < nTrianglePoints; i++) {
px = 0-map(currentRadii01, 0, 1, trianglePoints[i].x, 0);
py = 0-map(currentRadii01, 0, 1, trianglePoints[i].y, 0);
var ang1 = (i + 1) * TWO_PI / nTrianglePoints + HALF_PI / 3 + PI;
var ang2 = (i + 2) * TWO_PI / nTrianglePoints + HALF_PI / 3 + PI;
var nPointsInArc = 60;
for (var j = 0; j <= nPointsInArc; j++) {
var t = map(j,0,nPointsInArc, ang1,ang2);
var ax = px + rad * cos(t);
var ay = py + rad * sin(t);
vertex(ax, ay);
}
}
endShape(CLOSE);
if (bDrawDebug) {
stroke(255, 0, 0, 64);
strokeWeight(1);
for (var i = 0; i < nTrianglePoints; i++) {
var px = map(currentRadii01, 0, 1, trianglePoints[i].x, 0);
var py = map(currentRadii01, 0, 1, trianglePoints[i].y, 0);
line(0, 0, px, py);
}
}
pop();
}
function keyPressed(){
bDrawDebug = !bDrawDebug;
}