-
Notifications
You must be signed in to change notification settings - Fork 3
/
sketch.js
executable file
·77 lines (70 loc) · 1.63 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
/*
Code written by Selcuk ARTUT 2022
Geometric Patterns with Creative Coding
All rights reserved
*/
// Motif class
class Motif {
constructor(a) {
this.a = a;
}
display() {
let x0, y0, x1, y1, x2, y2, x3, y3;
for (let i = 0; i < 6; i++) {
push();
rotate(i * 60);
translate(cos(30) * this.a * 2, 0);
//line one
beginShape();
x0 = this.a * cos(30);
y0 = this.a * sin(30);
vertex(x0, y0);
x1 = -1 * x0;
y1 = -1 * y0;
vertex(x1, y1);
endShape();
//line two
beginShape();
x2 = x0;
y2 = y1;
vertex(x2, y2);
x3 = x1;
y3 = y0;
vertex(x3, y3);
endShape();
pop();
}
}
}
//scale factor
let a = 24;
let motif = new Motif(a);
let nRow;
let nCol;
let dx, dy;
function setup() {
createCanvas(800, 800);
angleMode(DEGREES);
noFill();
noLoop();
dx = 6 * a * cos(30);
dy = 4.5 * a;
doff = 0.5 * dx;
//approximate the nRow and nCol values
nCol = ceil(width / dx);
nRow = ceil(height / dy);
}
function draw() {
for (let c = 0; c < nCol; c++) {
for (let r = 0; r < nRow; r++) {
push();
if (r % 2 == 0) {
//columns 0,2,4
translate(doff, 0);
}
translate(c * dx, r * dy);
motif.display();
pop();
}
}
}