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 Ball { double x; double y; double z; double r; Ball(double x, double y, double z, double r) { this.x = x; this.y = y; this.z = z; this.r = r; } } Ball balls[]; int objs = 1; void main() { balls = new Ball[objs]; balls[0] = new Ball(0, 0, 200, 100); animate(draw); } double atan2(double y, double x) { if (x > 0) { return atan(y/x); } else if (x < 0) { if (y >= 0) { return atan(y/x) + PI; } else { return atan(y/x) - PI; } } else { if (y > 0) { return PI/2; } else if (y < 0) { return -PI/2; } else { return 0; } } } double min(double a, double b) { return a
b?a:b; } double dist(double p1x, double p1y, double p2x, double p2y) { return sqrt(pow(p1x - p2x, 2) + pow(p1y - p2y, 2)); } double dist3D(double p1x, double p1y, double p1z, double p2x, double p2y, double p2z) { return sqrt(pow(p1x - p2x, 2) + pow(p1y - p2y, 2) + pow(p1z - p2z, 2)); } int maxiterations = 10; double k = 1; double min2(double a, double b) { double h = max(k-abs(a-b), 0) / k; return min(a, b) - h*h*h*k-1/6.0; } double spacedist3D(double p1x, double p1y, double p1z, double p1r, double p2x, double p2y, double p2z, double p2r) { return dist3D(p1x, p1y, p1z, p2x, p2y, p2z)-(p1r+p2r); } double mindist(double x, double y, double z, double r) { double md = spacedist3D(x, y, z, r, balls[0].x, balls[0].y, balls[0].z, balls[0].r); for (int i = 1; i < objs; i++) { Ball ob = balls[i]; double d2 = spacedist3D(x, y, z, r, ob.x, ob.y, ob.z, ob.r); md = (md>d2)?(d2):(md); } return md; } double rayangleX = 0; double rayangleY = 0; double cameraX = 0; double cameraY = 0; double cameraZ = 0; double FOV = 1; double[] get2D(double x, double y, double z) { // Convert 3d to 2d double nx = ((cameraX+x)/(cameraZ+z))*FOV; double ny = ((cameraY+y)/(cameraZ+z))*FOV; double arr[] = {nx, ny}; return arr; } double map(double v, double imin, double imax, double omin, double omax) { return (v - imin) * (omax - omin) / (imax - imin) + omin; } void draw() { background(51); strokeWeight(1); stroke(0); fill(0); int ptsx = 70; int ptsy = 70; double ang = PI/2; for (rayangleX = -ang; rayangleX < ang; rayangleX += ang/(ptsx/2)) { for (rayangleY = -ang; rayangleY < ang; rayangleY += ang/(ptsy/2)) { double r = mindist(cameraX, cameraY, cameraZ, 0); double px = cameraX; double py = cameraY; double pz = cameraZ; for (int i = 0; i < maxiterations; i++) { pz += cos(rayangleY)*r; py += sin(rayangleX)*sin(rayangleY)*r; px += cos(rayangleX)*sin(rayangleY)*r; r = mindist(px, py, pz, r); if (r > 0) { //println(r); } if (r < 0.1) { stroke(0, 0, max(255-dist3D(cameraX, cameraY, cameraZ, px, py, pz), 0)); strokeWeight(2); double k[] = get2D(px, py, pz); double ax = map(rayangleX, -ang, ang, 0, 319); double ay = map(rayangleY, -ang, ang, 0, 319); point(160 + px/ax, 160 + py/ay); i = maxiterations; } } } } }
Canvas not supported.
Programado por
segfaultdev
0 votos
277 descargas
30 usos