xxxxxxxxxx
139
let walls = [];
let ray;
let particle;
function setup() {
createCanvas(400, 400);
for (let i = 0; i < 5; i++) {
let x1 = random(width);
let x2 = random(width);
let y1 = random(height);
let y2 = random(height);
walls[i] = new Boundary(x1, y1, x2, y2);
}
wall = new Boundary(300, 100, 100, 300);
ray = new Ray (100, 200);
particle = new Particle();
}
function draw() {
background(0);
for (let wall of walls) {
wall.show();
}
particle.update(mouseX, mouseY);
particle.show();
particle.look(walls);
// ray.show();
// ray.lookAt(mouseX, mouseY)
// let pt = ray.cast(wall);
// if (pt) {
// fill(255);
// ellipse(pt.x, pt.y, 8, 8);
// }
}
class Particle {
constructor() {
this.pos = createVector(width / 2, height /2);
this.rays = [];
for (let a = 0; a < 360; a += 5) {
this.rays.push(new Ray(this.pos, radians(a)));
}
}
update(x, y) {
this.pos.set(x, y);
}
look(walls) {
for (let ray of this.rays) {
let closest = null;
let record = Infinity;
for (let wall of walls) {
const pt = ray.cast(wall);
if (pt) {
const d = p5.Vector.dist(this.pos, pt);
if (d < record) {
record = d;
closest = pt;
}
}
if (closest) {
line(this.pos.x, this.pos.y, closest.x, closest.y);
}
}
}
}
show() {
fill(255);
ellipse(this.pos.x, this.pos.y, 4);
for (let ray of this.rays) {
ray.show();
}
}
}
class Ray {
constructor(pos, angle) {
this.pos = pos;
this.dir = p5.Vector.fromAngle(angle);
}
lookAt(x, y) {
this.dir.x = x - this.pos.x;
this.dir.y = y - this.pos.y;
this.dir.normalize();
}
show() {
stroke(255);
push()
translate(this.pos.x, this.pos.y);
line(0, 0, this.dir.x * 10, this.dir.y * 10);
pop();
}
cast(wall) {
const x1 = wall.a.x;
const y1 = wall.a.y;
const x2 = wall.b.x;
const y2 = wall.b.y;
const x3 = this.pos.x;
const y3 = this.pos.y;
const x4 = this.pos.x + this.dir.x;
const y4 = this.pos.y + this.dir.y;
const den = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
if (den == 0) {
return;
}
const t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / den;
const u = -((x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3)) / den;
if (t > 0 && t < 1 && u > 0) {
const pt = createVector();
pt.x = x1 + t * (x2 - x1);
pt.y = y1 + t * (y2 - y1);
return pt;
} else {
return;
}
}
}
class Boundary {
constructor(x1, y1, x2, y2) {
this.a = createVector(x1, y1);
this.b = createVector(x2, y2);
}
show() {
stroke(255);
line(this.a.x, this.a.y, this.b.x, this.b.y);
}
}