-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
107 lines (91 loc) · 2.26 KB
/
helpers.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
// GEOMETRY
// Finds a point on a circle of a given radius
function pointOnCircle(posX, posY, radius, angle) {
const x = posX + radius * cos(angle);
const y = posY + radius * sin(angle);
return createVector(x, y);
}
// Draws a right triangle
function rightTriangle(posX, posY, length) {
beginShape();
const a = createVector(posX + length / 2, posY + length / 2);
const b = createVector(posX - length / 2, posY + length / 2);
const c = createVector(posX - length / 2, posY - length / 2);
vertex(a.x, a.y);
vertex(b.x, b.y);
vertex(c.x, c.y);
vertex(a.x, a.y);
endShape();
}
// Draws a hexagon
function hexagon(posX, posY, radius) {
const rotAngle = 360 / 6;
beginShape();
for (let i = 0; i < 6; i++) {
const thisVertex = pointOnCircle(posX, posY, radius, i * rotAngle);
vertex(thisVertex.x, thisVertex.y);
}
endShape(CLOSE);
}
// UTILITY
// Randomly selects boolean value
function randomSelectTwo() {
// Number of Lines
const rand = random(1);
if (rand < 0.5) {
return true;
} else {
return false;
}
}
// Selects a color from PALETTE randomly
function getRandomFromPalette() {
const rand = floor(random(0, PALETTE.length - 1));
return PALETTE[rand];
}
// SHAPES
// Applies a random rotation
function applyRotation() {
const angle = floor(random(4));
rotate(angle * 90);
}
// Handles logic for selecting shape to render
function renderShape(shape, posX, posY, shapeSize, transform) {
if (shape === 0) {
rect(posX, posY, shapeSize);
} else if (shape === 1) {
ellipse(posX, posY, shapeSize);
} else if (shape === 2) {
rotate(transform * 90);
rightTriangle(posX, posY, shapeSize);
}
}
// FORMS
// Select a form to render based on random value
function renderForm(form, size) {
if (form === 1) {
box(size);
} else if (form === 2) {
torus(size / 2, size / 8);
} else if (form === 3) {
cone(size / 2, size / 2);
}
}
function applyTransformation(transform, angle) {
if (transform === 1) {
rotateForm(angle);
} else if (transform === 2) {
scaleForm(angle);
} else if (transform === 3) {
rotateForm(angle);
scaleForm(angle);
}
}
function rotateForm(angle) {
rotateX(angle);
rotateY(angle);
rotateZ(angle);
}
function scaleForm(angle) {
scale(abs(sin(angle)));
}