Ejercicio 1
int numBalls = 400;
float spring = 0.05;
float gravity = 0.07;
float friction = -0.9;
Ball[] balls = new Ball[numBalls];
void setup()
{
size(440, 440);
noStroke();
smooth();
for (int i = 0; i < numBalls; i++) {
balls[i] = new Ball(random(width), random(height), random(20, 40), i, balls);
}
}
void draw()
{
background(0);
for (int i = 0; i < numBalls; i++) {
balls[i].collide();
balls[i].move();
balls[i].display();
}
}
class Ball {
float x, y;
float diameter;
float vx = 0;
float vy = 0;
int id;
Ball[] others;
Ball(float xin, float yin, float din, int idin, Ball[] oin) {
x = xin;
y = yin;
diameter = din;
id = idin;
others = oin;
}
void collide() {
for (int i = id + 1; i < numBalls; i++) {
float dx = others[i].x - x;
float dy = others[i].y - y;
float distance = sqrt(dx*dx + dy*dy);
float minDist = others[i].diameter/2 + diameter/2;
if (distance < minDist) {
float angle = atan2(dy, dx);
float targetX = x + cos(angle) * minDist;
float targetY = y + sin(angle) * minDist;
float ax = (targetX - others[i].x) * spring;
float ay = (targetY - others[i].y) * spring;
vx -= ax;
vy -= ay;
others[i].vx += ax;
others[i].vy += ay;
}
}
}
void move() {
vy += gravity;
x += vx;
y += vy;
if (x + diameter/2 > width) {
x = width - diameter/2;
vx *= friction;
}
else if (x - diameter/2 < 0) {
x = diameter/2;
vx *= friction;
}
if (y + diameter/2 > height) {
y = height - diameter/2;
vy *= friction;
}
else if (y - diameter/2 < 0) {
y = diameter/2;
vy *= friction;
}
}
void display() {
fill(200,24,40,100);
rect(x, y, diameter, diameter);
}
}
Ejercicio 2
float bricksPerLayer = 26.0;
float brickLayers = 30.0;
Cube brick;
float brickWidth = 80, brickHeight = 45, brickDepth = 35;
float radius = 145.0;
float angle = 0;
void setup(){
size(640, 360, P3D);
brick = new Cube(brickWidth, brickHeight, brickDepth);
}
void draw(){
background(255);
float tempX = 0, tempY = 0, tempZ = 0;
fill(0, 40, 29, 40);
noStroke();
// Add basic light setup
lights();
translate(width/2, height*1.2, -380);
// Tip tower to see inside
rotateX(radians(-45));
// Slowly rotate tower
rotateY(frameCount * PI/600);
for (int i = 0; i < brickLayers; i++){
// Increment rows
tempY-=brickHeight;
// Alternate brick seams
angle = 360.0 / bricksPerLayer * i/2;
for (int j = 0; j < bricksPerLayer; j++){
tempZ = cos(radians(angle))*radius;
tempX = sin(radians(angle))*radius;
pushMatrix();
translate(tempX, tempY, tempZ);
rotateY(radians(angle));
// Add crenelation
if (i==brickLayers-1){
if (j%2 == 0){
brick.create();
}
}
// Create main tower
else {
brick.create();
}
popMatrix();
angle += 360.0/bricksPerLayer;
}
}
}
No hay comentarios:
Publicar un comentario