sketch-a-day/s180105/s180105.js

74 wiersze
2.2 KiB
JavaScript
Czysty Zwykły widok Historia

2018-01-05 21:26:47 +00:00
/*
2018-01-05 22:59:47 +00:00
s180105 Tetrahedrons
(c)2018 Alexandre B A Villares
https://abav.lugaralgum.com/sketch-a-day
*/
2018-01-05 21:26:47 +00:00
var tetrah_list = [];
var rot_x = 0;
var rot_y = 0;
function setup() {
canvas = createCanvas(500, 500, WEBGL);
2018-01-06 00:00:08 +00:00
canvas.parent('sketch-holder');
2018-01-05 21:26:47 +00:00
setAttributes('antialias', true);
for (var i = -5; i < 5; i++) {
for (var j = -5; j < 5; j++) {
var x = i*40;
var y = j*40;
var z = random(-20, 20);
2018-01-05 23:48:48 +00:00
var t = new Tetrahedron(x, y, z, 35); // cria um tetraedro
tetrah_list.push(t); // acrescenta no array/lista }
2018-01-05 21:26:47 +00:00
}
}
}
2018-01-05 21:41:18 +00:00
function draw() {
background(0);
strokeWeight(5);
rot_x += 0.05;
rot_y += 0.01;
for (var i = 0; i < tetrah_list.length; i++) { // itera pela lista apas
tetrah_list[i].plot();
2018-01-05 21:26:47 +00:00
}
2018-01-05 21:41:18 +00:00
}
2018-01-05 21:26:47 +00:00
2018-01-05 23:33:00 +00:00
function Tetrahedron(x, y, z, radius) {
2018-01-05 21:41:18 +00:00
this.x = x;
this.y = y;
this.z = z;
this.vert = [];
2018-01-05 23:33:00 +00:00
var a = radius*2/3;
2018-01-05 21:41:18 +00:00
this.vert[0] = new p5.Vector( a, a, a ); // vertex 1
this.vert[1] = new p5.Vector(-a, -a, a ); // vertex 2
this.vert[2] = new p5.Vector(-a, a, -a ); // vertex 3
this.vert[3] = new p5.Vector( a, -a, -a ); // vertex 4
// draws tetrahedron
this.plot = function(rx, ry) {
push();
var tz = this.z
2018-01-05 23:33:00 +00:00
if (dist(mouseX, mouseY, this.x+width/2, this.y+height/2)<50) {
2018-01-05 21:41:18 +00:00
tz = tz * 10;
2018-01-05 23:33:00 +00:00
stroke(0, 100, 200); // azul
2018-01-05 21:41:18 +00:00
} else {
2018-01-05 23:33:00 +00:00
stroke(200, 100, 0); // laranja
2018-01-05 21:26:47 +00:00
}
2018-01-05 21:41:18 +00:00
translate(this.x, this.y, tz);
rotateX(rot_y);
rotateY(rot_x);
line(this.vert[0].x, this.vert[0].y, this.vert[0].z, // vertex 1
this.vert[1].x, this.vert[1].y, this.vert[1].z); // vertex 2
line(this.vert[0].x, this.vert[0].y, this.vert[0].z, // vertex 1
this.vert[2].x, this.vert[2].y, this.vert[2].z); // vertex 3
line(this.vert[0].x, this.vert[0].y, this.vert[0].z, // vertex 1
this.vert[3].x, this.vert[3].y, this.vert[3].z); // vertex 4
line(this.vert[1].x, this.vert[1].y, this.vert[1].z, // vertex 2
this.vert[2].x, this.vert[2].y, this.vert[2].z); // vertex 3
line(this.vert[1].x, this.vert[1].y, this.vert[1].z, // vertex 2
this.vert[3].x, this.vert[3].y, this.vert[3].z); // vertex 4
line(this.vert[2].x, this.vert[2].y, this.vert[2].z, // vertex 3
this.vert[3].x, this.vert[3].y, this.vert[3].z); // vertex 4
pop();
}
}