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;} } vector vec3d(double x, double y, double z) { return new vector(x, y, z); } vector vec2d(double x, double y) { return new vector(x, y, 0); } vector tri[][] = { // Conexiones entre puntos {vec3d(-1, -1, -1), vec3d(1, -1, -1), vec3d(-1, 1, -1)}, // Cara 1 {vec3d(1, 1, -1), vec3d(1, -1, -1), vec3d(-1, 1, -1)}, {vec3d(-1, -1, -1), vec3d(-1, 1, -1), vec3d(-1, -1, 1)}, // Cara 2 {vec3d(-1, 1, 1), vec3d(-1, 1, -1), vec3d(-1, -1, 1)}, {vec3d(-1, -1, -1), vec3d(-1, -1, 1), vec3d(1, -1, -1)}, // Cara 3 {vec3d(1, -1, 1), vec3d(-1, -1, 1), vec3d(1, -1, -1)}, {vec3d(-1, -1, 1), vec3d(1, -1, 1), vec3d(-1, 1, 1)}, // Cara 4 {vec3d(1, 1, 1), vec3d(1, -1, 1), vec3d(-1, 1, 1)}, {vec3d(1, -1, -1), vec3d(1, 1, -1), vec3d(1, -1, 1)}, // Cara 5 {vec3d(1, 1, 1), vec3d(1, 1, -1), vec3d(1, -1, 1)}, {vec3d(-1, 1, -1), vec3d(-1, 1, 1), vec3d(1, 1, -1)}, // Cara 6 {vec3d(1, 1, 1), vec3d(-1, 1, 1), vec3d(1, 1, -1)} }; int utp[] = { // Referencias 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; int ox[] = utp; int perspective[][] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 0}}; int size = 25; // 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 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 main() { // Las formulas provienen de: https://es.wikipedia.org/wiki/Matriz_de_rotaci%C3%B3n animate(draw, 16); } 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(vector i) { return dist(rotate(i), new vector(0, 0, -1))*factor; } void draw3Dline(vector i, vector j) { line(160+get2Dvec(i).x, 160+get2Dvec(i).y, 160+get2Dvec(j).x, 160+get2Dvec(j).y); } vector get2Dvec(vector i) { return vec2d((getPoint(rotate(i)).x*size*dst(i)), (getPoint(rotate(i)).y*size*dst(i))); } void draw3Dtriangle(vector i, vector j, vector k) { draw3Dline(i, j); draw3Dline(j, k); draw3Dline(k, i); double ldst = v3Ddist(tri3Dmid(rotate(i), rotate(j), rotate(k)), vec3d(0, 0, -1)); fill(ldst * ldst * 63); strokeWeight(1); stroke(ldst * ldst * 63); triangle(160+get2Dvec(i).x, 160+get2Dvec(i).y, 160+get2Dvec(j).x, 160+get2Dvec(j).y, 160+get2Dvec(k).x, 160+get2Dvec(k).y); } vector tri3Dmid(vector i, vector j, vector k) { return vec3d((i.x + j.x + k.x) / 3, (i.y + j.y + k.y) / 3, (i.z + j.z + k.z) / 3); } double v3Ddist(vector m1, vector m2) { double xd = m1.x - m2.x; double yd = m1.y - m2.y; double zd = m1.z - m2.z; return sqrt((xd*xd) + (yd*yd) + (zd*zd)); } double tri3Ddist(vector i1, vector j1, vector k1, vector i2, vector j2, vector k2) { return v3Ddist(tri3Dmid(i1, j1, k1), tri3Dmid(i2, j2, k2)); } void sorttriangles() { for (int i = 0; i < utp.length; i++) { for (int j = i; j < utp.length; j++) { double dsti = v3Ddist(tri3Dmid(rotate(tri[utp[i]][0]), rotate(tri[utp[i]][1]), rotate(tri[utp[i]][2])), vec3d(0, 0, -1)); double dstj = v3Ddist(tri3Dmid(rotate(tri[utp[j]][0]), rotate(tri[utp[j]][1]), rotate(tri[utp[j]][2])), vec3d(0, 0, -1)); if (dstj < dsti) { int tmp = utp[i]; utp[i] = utp[j]; utp[j] = tmp; } } } } void draw3Dobject() { sorttriangles(); for (int i = 0; i < utp.length; i++) { draw3Dtriangle(tri[utp[i]][0], tri[utp[i]][1], tri[utp[i]][2]); } } int uk = 0; void draw() { background(0); 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(); ax += 0.03; ay += 0.02; az += 0.01; }
Canvas not supported.
Programado por
segfaultdev
1 votos
156 descargas
38 usos