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 point2D { double x; double y; point2D(double x, double y) { this.x = x; this.y = y; } } point2D intersect(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { if ((x1 == x2 && y1 == y2) || (x3 == x4 && y3 == y4)) { return null; } double denominator = ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1)); if (denominator == 0.0) { return null; } double ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / denominator; double ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / denominator; if (ua < 0 || ua > 1 || ub < 0 || ub > 1) { return null; } double x = x1 + ua * (x2 - x1); double y = y1 + ua * (y2 - y1); return new point2D(x, y); } void main() { int x1 = 0; int y1 = 0; int x2 = 319; int y2 = 319; for (double i = 0; i < ptcount; i++) { double prc = i/ptcount; pts[round(i)] = new point2D((x1*prc)+(x2*(1-prc)), (y1*prc)+(y2*(1-prc))); //pts[round(i)] = new point2D(cos(prc*2*PI)*100, sin(prc*2*PI)*100); } animate(draw); } double camX = 160; double camY = 240; double viewAngle = PI/2.5; double viewDist = 500; double minDist = 10; double FOV = 30; double angle = -2; double speed = 5; boolean topCam = true; double ptcount = 128; point2D pts[] = new point2D[ptcount]; point2D sorted[] = new point2D[ptcount]; double dist(double x1, double y1, double x2, double y2) { return sqrt(pow(x1-x2, 2) + pow(y1-y2, 2)); } boolean intriangle(double x1, double y1, double x2, double y2, double x3, double y3, double x, double y) { double denominator = ((y2 - y3)*(x1 - x3) + (x3 - x2)*(y1 - y3)); double a = ((y2 - y3)*(x - x3) + (x3 - x2)*(y - y3)) / denominator; double b = ((y3 - y1)*(x - x3) + (x1 - x3)*(y - y3)) / denominator; double c = 1 - a - b; return 0 <= a && a <= 1 && 0 <= b && b <= 1 && 0 <= c && c <= 1; } 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; } } } void draw() { background(51); stroke(255); strokeWeight(1); viewAngle = PI/(FOV/minDist); point(camX, camY); double ux1 = camX+(cos(viewAngle/2 + angle)*viewDist); double uy1 = camY+(sin(viewAngle/2 + angle)*viewDist); double ux2 = camX+(cos(-viewAngle/2 + angle)*viewDist); double uy2 = camY+(sin(-viewAngle/2 + angle)*viewDist); line(camX, camY, camX+(cos(viewAngle/2 + angle)*viewDist), camY+(sin(viewAngle/2 + angle)*viewDist)); line(camX, camY, camX+(cos(-viewAngle/2 + angle)*viewDist), camY+(sin(-viewAngle/2 + angle)*viewDist)); fill(255, 255, 255, 0.3); triangle(camX, camY, ux1, uy1, ux2, uy2); double x1 = camX+(cos(viewAngle/2 + angle)*FOV); double y1 = camY+(sin(viewAngle/2 + angle)*FOV); double x2 = camX+(cos(-viewAngle/2 + angle)*FOV); double y2 = camY+(sin(-viewAngle/2 + angle)*FOV); double x3 = camX; double y3 = camY; topCam = mousePressed; line(x1, y1, x2, y2); noStroke(); double w = 320; double h = 320; if (!topCam) { fill(127, 255, 255); rect(-1, -1, w+1, h+1); fill(127, 255, 127); rect(-1, (h/2)-1, w+1, (h/2)+1); } for (int i = 0; i < ptcount; i++) { sorted[i] = pts[i]; } for (int i = 0; i < ptcount; i++) { for (int j = 0; j < i; j++) { double a = dist(camX, camY, sorted[i].x, sorted[i].y); double b = dist(camX, camY, sorted[j].x, sorted[j].y); if (a>b) { point2D c = sorted[i]; sorted[i] = sorted[j]; sorted[j] = c; } } } for (int i = 0; i < ptcount; i++) { if (intriangle(camX, camY, ux1, uy1, ux2, uy2, sorted[i].x, sorted[i].y)) { stroke(255); strokeWeight(1); double x4 = sorted[i].x; double y4 = sorted[i].y; double k = dist(camX, camY, x4, y4); point2D p = intersect(x1, y1, x2, y2, x3, y3, x4, y4); double ang = atan2(camY-y4, camX-x4); if (topCam) { stroke(255); strokeWeight(1); line(x3, y3, x4, y4); if (p != null) { stroke(0, k, k); strokeWeight(4); point(p.x, p.y); } } else { if (p != null) { double px1 = camX+(cos(viewAngle/2 + angle)*k); double py1 = camY+(sin(viewAngle/2 + angle)*k); double px2 = camX+(cos(-viewAngle/2 + angle)*k); double py2 = camY+(sin(-viewAngle/2 + angle)*k); double subang = -14; strokeWeight(12); stroke((1-pow(k/viewDist, 1.5))*255); double vl = ((viewDist/2)-(dist(p.x, p.y, (px1+px2)/2, (py1+py2)/2))); double lineh = ((vl<0?0:vl)/viewDist) * h; double linex = (dist(x2, y2, p.x, p.y) * 11); line(linex, h/2 - lineh, linex, h/2 + lineh); } } } } if (keyPressed) { if (key == "w") { camX += (cos(angle)*speed); camY += (sin(angle)*speed); } else if (key == "s") { camX -= (cos(angle)*speed); camY -= (sin(angle)*speed); } else if (key == "a") { angle -= 0.05; } else if (key == "d") { angle += 0.05; } } }
Canvas not supported.
Programado por
segfaultdev
2 votos
326 descargas
73 usos