xxxxxxxxxx
49
class Ray {
constructor(first, second) {
this.anchor = first;
this.t = second.copy();
this.ray = [];
}
cast() {
this.ray.push(this.anchor);
this.ray.push(this.t);
while (
this.t.x < width &&
this.t.x > 0 &&
this.t.y < height/2 &&
this.t.y > 0
) {
let min_d = Infinity;
let d = 0;
for (let i = 0; i < shapes.length; ++i) {
let d = shapes[i].distance(this.t);
if (d < min_d) min_d = d;
}
if (min_d < Epsilon) {
return dist(this.anchor.x, this.anchor.y, this.t.x, this.t.y);
}
let s = this.t.copy();
s.sub(this.anchor);
let angle = s.heading();
let next_t = createVector(min_d, 0);
next_t.setHeading(angle);
next_t.add(this.t);
this.t = next_t;
this.ray.push(this.t);
}
return Infinity;
}
show() {
noFill();
beginShape();
for (let i = 0; i < this.ray.length; ++i) {
vertex(this.ray[i].x, this.ray[i].y);
noFill();
}
endShape();
}
}