Toggle navigation
Explora
(current)
Aprende
Crea
Retos
×
Aspectos básicos
void main() {...}
for ( int i = 0 ; i < N ; i++ ) {...}
while (condición) {...}
do {...} while (condición);
if (condición) {...}
if (condición) {...} else {...}
switch (valor) {...}
Mostrar y pedir datos
print()
println()
readInteger()
readDouble()
readChar()
readString()
Funciones matemáticas
abs(n)
log(n)
sqrt(n)
pow(b,e)
floor(n)
ceil(n)
round(n)
sin(n)
cos(n)
tan(n)
asin(n)
acos(n)
atan(n)
random(n)
Funciones gráficas
point(x,y)
line(x1,y1,x2,y2)
ellipse(x,y,w,h)
rect(x,y,w,h)
triangle(x1,y1,x2,y2,x3,y3)
text(msg,x,y)
textWidth(msg)
textSize(n)
background(r,g,b,a)
strokeWeight(n)
stroke(r,g,b,a)
noStroke()
fill(r,g,b,a)
noFill()
image(url, x,y,w,h)
Nuevo
Ayuda
Probar
...
double max(double a, double b) { return a>b?a:b; } double min(double a, double b) { return a
= 319) { if (x - (size / 2) <= 0) { dx = abs(dx)*bfric; } else { dx = -abs(dx)*bfric; } x = (x - (size / 2) <= 0) ? (1 + (size / 2)) : (318 - (size / 2)); } if (y - (size / 2) <= 0 || y + (size / 2) >= 319) { if (y - (size / 2) <= 0) { dy = abs(dy)*bfric; } else { dy = -abs(dy)*bfric; } y = (y - (size / 2) <= 0) ? (1 + (size / 2)) : (318 - (size / 2)); } } x += dx; y += dy; } } void show() { if (staticm) { fill(192); } else if (magn) { fill(255, 127, 0); } else if (powered) { fill(255, 255, 63); } else { fill(255); } noStroke(); ellipse(x, y, size, size); } }; // bug list // - when ball collides with +2 balls at the same time the one with movement disappears // - (unknown cause) crashes browser final int balls = 200; final double clsloss = 0.1; Ball b[] = new Ball[balls]; int gb = 0; int mx = -1; int my = -1; int s = -1; boolean t = false; void main() { println("Si pones el cursor sobre una figura y"); println("mantienes el boton izquierdo del raton"); println("podras ver una linea. Cuando sueltes,"); println("lanzara la figura en la direccion"); println("contraria a la que has movido el raton."); println("Cuanto mas lejos este el raton, mas"); println("rapido y con mas fuerza se mueve."); animate(draw); } void avoid() { for (int i = 0; i < gb; i++) { for (int j = i; j < gb; j++) { if (i != j) { Ball ba = b[i]; Ball bb = b[j]; double di = dist(ba.x, ba.y, bb.x, bb.y); double siz = (ba.size/2) + (bb.size/2); if (di < siz) { double bs1 = abs(sqrt((b[i].dx*b[i].dx) + (b[i].dy*b[i].dy))); double bs2 = abs(sqrt((b[j].dx*b[j].dx) + (b[j].dy*b[j].dy))); double bm1 = PI*(ba.size/2)*(ba.size/2); double bm2 = PI*(bb.size/2)*(bb.size/2); double bf2 = abs((((bs1)*(bm1/(bm1+bm2)))+bs2)-(clsloss/2)); double bf1 = abs((((bs2)*(bm2/(bm1+bm2)))+bs1)-(clsloss/2)); double an = angle(ba.x, ba.y, bb.x, bb.y); while ((!(b[i].staticm || b[j].staticm)) && (bf1+bf2 > bs1+bs2)) { if (!b[i].staticm) { bf1 *= 0.99; } if (!b[j].staticm) { bf2 *= 0.99; } } if (ba.magn || bb.magn) { b[i].dx = (cos(an)*bf1); b[i].dy = (sin(an)*bf1); b[j].dx = -(cos(an)*bf2); b[j].dy = -(sin(an)*bf2); } else { b[i].dx = -(cos(an)*bf1); b[i].dy = -(sin(an)*bf1); b[j].dx = (cos(an)*bf2); b[j].dy = (sin(an)*bf2); } } } } } } double dist(double x1, double y1, double x2, double y2) { return sqrt(pow(x1-x2, 2) + pow(y1-y2, 2)); } double atan2(double y, double x) { double u = atan(y/x); if( x < 0.0 ) { if( u > 0.0 ) u -= PI; else u += PI; } return u; } double angle(double x1, double y1, double x2, double y2) { double res = atan2(y1-y2, x1-x2); res += PI; return res; } int nearestBall(int a, int c) { double betterd = 400; int ind = -1; for (int i = 0; i < gb; i++) { for (int j = 0; j < gb; j++) { if (dist(b[j].x, b[j].y, a, c) < betterd) { if (!(b[i].staticm || b[j].staticm)) { ind = j; betterd = dist(b[j].x, b[j].y, a, c); } } } } return ind; } int tck = 0; int tcki = 0; int tcko = 0; int oldx = -1; int oldy = -1; boolean first = true; double grav = 0; double fric = 0.9; double bfric = 0.9; void draw() { background(0); avoid(); for (int i = 0; i < gb; i++) { if (grav != 0.0) { b[i].gravity(grav); } b[i].show(); b[i].move(); if (b[i].magn) { b[i].applyFriction(0); } else { b[i].applyFriction(fric); } } if (mousePressed && mouseButton == RIGHTBUTTON) { if (tcki == 0) { tcki++; oldx = mouseX; oldy = mouseY; } tcko = 0; noStroke(); fill(255); ellipse(oldx, oldy, dist(oldx, oldy, mouseX, mouseY)*2, dist(oldx, oldy, mouseX, mouseY)*2); stroke(0, 255, 0); line(mouseX, mouseY, oldx, oldy); first = false; } else { tcki = 0; if (tcko == 0 && !first) { tcko++; boolean isStatic = (keyPressed&&(key=="up")); boolean magnetic = (keyPressed&&(key=="down")); boolean hasPower = (keyPressed&&(key=="shift")); if (gb < balls) { b[gb] = new Ball(oldx, oldy, round(dist(oldx, oldy, mouseX, mouseY)*2), true, isStatic, magnetic, hasPower); gb++; } } } if (gb > 0) { if (mousePressed && mouseButton == LEFTBUTTON) { if (tck == 0) { s = nearestBall(mouseX, mouseY); } tck++; stroke(255, 0, 0); strokeWeight(1); line(b[s].x, b[s].y, mouseX, mouseY); t = true; } else { tck = 0; if (t) { b[s].force = dist(b[s].x, b[s].y, mouseX, mouseY)/3; b[s].rot = angle(mouseX, mouseY, b[s].x, b[s].y); b[s].setDir(); t = false; } } } }
Canvas not supported.
Programado por
segfaultdev
5 votos
338 descargas
97 usos