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
...
class vector { double x;double y;double z; public vector(double nx,double ny){x = nx;y = ny;z = 0;} public vector(double nx,double ny,double nz){x = nx;y = ny;z = nz;} } // Puedes modificar las variables p y l para cambiar la forma de la figura. Yo // he puesto un cubo con las caras divididas en dos triangulos como ejemplo de // este simple visualizador de figuras en 3D. boolean glow = false; vector p[] = { // Puntos de la figura new vector(1, -1, -1), new vector(1, 1, -1), new vector(1, 1, 1), new vector(1, -1, 1), new vector(-1, -1, -1), new vector(-1, 1, -1), new vector(-1, 1, 1), new vector(-1, -1, 1) }; int l[][] = { // Conexiones entre puntos {0, 4}, {4, 5}, {5, 0}, {4, 3}, {3, 0}, {3, 7}, {7, 4}, {0, 1}, {1, 5}, {1, 2}, {2, 6}, {6, 5}, {2, 3}, {3, 1}, {6, 7}, {7, 2}, {4, 6}, {1, 6} }; int utp[] = { // Referencias 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 }; int ox[] = utp; int perspective[][] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 0}}; int size = 40; // Escala de la figura double ax = 0; // Rotacion en eje X double ay = 0; // Rotacion en eje Y double az = 0; // Rotacion en eje Z int lx = 0; // Punto de luz (es sustituido por mouseX) int ly = 0; // " (es sustituido por mouseY) 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; } vector rotateZ(vector base) { return new vector((base.x*cos(az)) + (base.y*(-sin(az))), (base.x*sin(az)) + (base.y*cos(az)), base.z); } vector rotateY(vector base) { return new vector((base.x*cos(ay))+(base.z*sin(ay)), (base.y), (base.x*(-sin(ay))) + (base.z*cos(ay))); } vector rotateX(vector base) { return new vector((base.x), (base.y*cos(ax)) + (base.z*(-sin(ax))), (base.y*sin(ax)) + (base.z*cos(ax))); } vector rotate(vector bass) { return rotateX(rotateY(rotateZ(bass))); } vector getPoint(vector base) { return new vector((base.x*perspective[0][0]) + (base.y*perspective[0][1]) + (base.z*perspective[0][2]), (base.x*perspective[1][0]) + (base.y*perspective[1][1]) + (base.z*perspective[1][2])); } void Line(double x1, double y1, double x2, double y2) { double ang = atan2(abs(y1-y2), abs(x1-x2)); double dst = dist(new vector(x1, y1), new vector(x2, y2)); double nx = x1; double ny = y1; lx = mouseX; ly = mouseY; for (double i = 0; i < dst; i += 1) { double d1 = dist(new vector(lx, ly), new vector(nx, ny)); double d2 = dist(new vector(0, 0), new vector(279, 279)); stroke((1-(d1/d2))*255); point(nx, ny); if ((x1-x2) > 0) { nx -= cos(ang); } else { nx += cos(ang); } if ((y1-y2) > 0) { ny -= sin(ang); } else { ny += sin(ang); } } } void main() { println("A S D"); println("/\\ /\\ /\\"); println("RX RY RZ"); println("\\/ \\/ \\/"); println("Z X C"); println(""); println("RX: Rotacion eje X"); println("RY: Rotacion eje Y"); println("RZ: Rotacion eje Z"); println(""); println("Las flechas hacia arriba y hacia abajo sirver para acercar o alejar la figura."); println(""); println("El raton se usa para elegir de donde proviene la luz."); // Las formulas provienen de: https://es.wikipedia.org/wiki/Matriz_de_rotaci%C3%B3n animate(draw); } double dist(vector p1, vector p2) { return sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2) + pow(p1.z - p2.z, 2)); } vector mid(vector p1, vector p2) { return new vector((p1.x+p2.x)/2, (p1.y+p2.y)/2, (p1.z+p2.z)/2); } double fullDist = dist(new vector(-1, -1, -1), new vector(1, 1, 1)); int factor = 1; double dst(int i) { return dist(rotate(p[i]), new vector(0, 0, -1))*factor; } void draw3Dline(int i, int j) { // Remplazar por Line if (i == 0 && j == 4) { stroke(255, 0, 0); } else { if (i == 2 && j == 3) { stroke(0, 255, 0); } else { stroke(255, 255, 255); } } line(160+(getPoint(rotate(p[i])).x*size*dst(i)), 160+(getPoint(rotate(p[i])).y*size*dst(i)), 160+(getPoint(rotate(p[j])).x*size*dst(j)), 160+(getPoint(rotate(p[j])).y*size*dst(j))); } void draw3Dobject() { for (int i = l.length-1; i >= 0; i--) { if (utp[i] > -1) { draw3Dline(l[utp[i]][0], l[utp[i]][1]); } } } int uk = 0; double getDist(int line) { int p1 = l[line][0]; int p2 = l[line][1]; return (dst(p1)+dst(p2))/2; } void orderPoints() { int c; for (int i = 0; i < 17*18; i++) { int i2 = i % 17; int a = utp[i2]; int b = utp[i2+1]; if (getDist(b) > getDist(a)) { utp[i2+1] = a; utp[i2] = b; } } } void draw() { background(0, 0, 0, glow?0.1:1); stroke(255); utp = ox; strokeWeight(2); text("RX: " + (((int)(ax*1000)))/100, 0, 20); text("RY: " + (((int)(ay*1000)))/100, 0, 40); text("RZ: " + (((int)(az*1000)))/100, 0, 60); draw3Dobject(); for (int i = 0; i < utp.length; i++) { textSize(12); text(utp[i], 290, 20+(i*15)); } //size = (20*cos(az))+20; ax += 0.03; ay += 0.02; az += 0.01; orderPoints(); fill(255); //noFill(); //stroke(255, 0, 0); noStroke(); for (int i = 5; i >= 0; i--) { //for (int i = 0; i < 6; i++) { int a = (i*3); int b = a+1; int c = a+2; //triangle(160+getPoint(rotate(p[l[utp[a]][0]])).x*size*dst(l[utp[a]][0]), 160+getPoint(rotate(p[l[utp[a]][0]])).y*size*dst(l[utp[a]][0]), 160+getPoint(rotate(p[l[utp[a]][1]])).x*size*dst(l[utp[a]][1]), 160+getPoint(rotate(p[l[utp[a]][1]])).y*size*dst(l[utp[a]][1]), 160+getPoint(rotate(p[l[utp[b]][0]])).x*size*dst(l[utp[b]][0]), 160+getPoint(rotate(p[l[utp[b]][0]])).y*size*dst(l[utp[b]][0])); //triangle(160+getPoint(rotate(p[l[utp[a]][0]])).x*size*dst(l[utp[a]][0]), 160+getPoint(rotate(p[l[utp[a]][0]])).y*size*dst(l[utp[a]][0]), 160+getPoint(rotate(p[l[utp[a]][1]])).x*size*dst(l[utp[a]][1]), 160+getPoint(rotate(p[l[utp[a]][1]])).y*size*dst(l[utp[a]][1]), 160+getPoint(rotate(p[l[utp[c]][0]])).x*size*dst(l[utp[c]][0]), 160+getPoint(rotate(p[l[utp[c]][0]])).y*size*dst(l[utp[c]][0])); } fill(255); if (keyPressed) { if (key == "a") { ax += 0.02; } if (key == "z") { ax -= 0.02; } if (key == "s") { ay += 0.02; } if (key == "x") { ay -= 0.02; } if (key == "d") { az += 0.02; } if (key == "c") { az -= 0.02; } //if (key == "y") { //orderPoints(); //} if (key == "up") { size += 7; } if (key == "down") { size -= 7; } } }
Canvas not supported.
Programado por
segfaultdev
4 votos
561 descargas
154 usos