|
|
Línea 16: |
Línea 16: |
|
| |
|
| [[Archivo:Hazluz0747.png|150px|thumb|left|Comportamiento de la luz al colisionar con un objeto]][[Archivo:Hazluz0748.png|150px|thumb|center|Imagen desde processing]][[Archivo:Hazluz0749.png|150px|thumb|right|Imagen desde processing]] | | [[Archivo:Hazluz0747.png|150px|thumb|left|Comportamiento de la luz al colisionar con un objeto]][[Archivo:Hazluz0748.png|150px|thumb|center|Imagen desde processing]][[Archivo:Hazluz0749.png|150px|thumb|right|Imagen desde processing]] |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
| ===Código Processing===
| |
|
| |
| <pre>
| |
| Particula part;
| |
| muro[] muro = new muro[5]; //defino la cantidad de muros
| |
| luz[] luz = new luz[360];
| |
|
| |
| void setup() {
| |
| size(800, 600);
| |
| background(0);
| |
| part = new Particula(width/2, height/2);
| |
|
| |
| for (int i = 0; i<muro.length; i++) {
| |
| muro[i] = new muro(random(width), random(height), random(width), random(height));
| |
| }
| |
|
| |
| for (int i = 0; i<luz.length; i++) {
| |
| luz[i] = new luz( i*TWO_PI/luz.length);
| |
| }
| |
| }
| |
|
| |
| void draw() {
| |
| background(0);
| |
|
| |
|
| |
| // control with Mouse
| |
| part.MouseMove();
| |
|
| |
| //part.move();
| |
|
| |
| part.show();
| |
|
| |
| for (int i = 0; i<muro.length; i++) {
| |
| muro[i].show();
| |
| }
| |
| }
| |
|
| |
| class Particula {
| |
| float x, y, nx, ny, t1, t2, xSpeed, ySpeed;
| |
| PVector pos;
| |
|
| |
|
| |
| Particula(float _x, float _y) {
| |
| x = _x;
| |
| y = _y;
| |
| }
| |
|
| |
| void MouseMove() { // posición del mouse
| |
| x = mouseX;
| |
| y = mouseY;
| |
| pos = new PVector(x, y);
| |
| point(pos.x, pos.y);
| |
| }
| |
|
| |
| void show() {
| |
| for (int i = 0; i<luz.length; i++) {
| |
| PVector closest = new PVector (0, 0);
| |
| float rec = 100000;
| |
| for (int j = 0; j<muro.length; j++) {
| |
|
| |
| luz[i].CollisionCheck(muro[j].p1.x, muro[j].p1.y, muro[j].p2.x, muro[j].p2.y);
| |
|
| |
| if (0 <= luz[i].t && luz[i].t <= 1 && 0 <= luz[i].u) {
| |
| float d = dist(luz[i].pos.x, luz[i].pos.y, luz[i].poi.x, luz[i].poi.y);
| |
| if (d < rec) {
| |
| rec = d;
| |
| closest = luz[i].poi;
| |
| }
| |
| }
| |
| }
| |
| stroke(255, 255, 90); // color y espesor haz de luz
| |
| strokeWeight(1);
| |
| if (closest.x == 0 & closest.y == 0) {
| |
| luz[i].show();
| |
| } else {
| |
| line(luz[i].pos.x, luz[i].pos.y, closest.x, closest.y);
| |
| }
| |
| }
| |
| }
| |
| }
| |
|
| |
| class luz {
| |
| float angle, x1, x2, y1, y2, x3, y3, x4, y4, t, u;
| |
| PVector dir, pos, poi;
| |
|
| |
| luz(float _angle) {
| |
| angle = _angle;
| |
| }
| |
|
| |
| void CollisionCheck(float _x1, float _y1, float _x2, float _y2 ) {
| |
| pos = part.pos;
| |
| dir = PVector.fromAngle(angle);
| |
| dir.add(pos);
| |
| x1 = _x1;
| |
| y1 = _y1;
| |
| x2 = _x2;
| |
| y2 = _y2;
| |
| x3 = pos.x;
| |
| y3 = pos.y;
| |
| x4 = dir.x;
| |
| y4 = dir.y;
| |
|
| |
| float dom = ((x1-x2)*(y3-y4))-((y1-y2)*(x3-x4));
| |
|
| |
| if (dom == 0) {
| |
| t = -1;
| |
| } else {
| |
| t = (((x1-x3)*(y3-y4))-((y1-y3)*(x3-x4)))/dom;
| |
| u = -((((x1-x2)*(y1-y3))-((y1-y2)*(x1-x3)))/dom);
| |
|
| |
| poi = new PVector(0, 0);
| |
|
| |
| if (0 <= t && t <= 1 && 0 <= u) {
| |
| float px = x1 + (t * (x2 - x1));
| |
| float py = y1 + (t * (y2 - y1));
| |
| poi = new PVector(px, py);
| |
| }
| |
| }
| |
| }
| |
|
| |
| void show() {
| |
| pos = part.pos;
| |
| dir = PVector.fromAngle(angle);
| |
| dir.mult(100000);
| |
| dir.add(pos);
| |
| line(pos.x, pos.y, dir.x, dir.y);
| |
| }
| |
| }
| |
|
| |
| class muro {
| |
| PVector p1, p2;
| |
|
| |
| muro(float x1, float y1, float x2, float y2) {
| |
| p1 = new PVector(x1, y1);
| |
| p2 = new PVector(x2, y2);
| |
| }
| |
|
| |
| void show() {
| |
| stroke(255, 50, 98);
| |
| strokeWeight(8);
| |
| line(p1.x, p1.y, p2.x, p2.y);
| |
| }
| |
| }
| |
| </pre>
| |