class Ray {
constructor([x, y, deg], environment, context) {
this.history = [[x, y]];
this.pos = [x, y];
this.r = 3;
this.deg = deg;
this.context = context;
this.environment = environment;
this.stepSize = 2;
}
run() {
this.intersection();
this.step();
this.draw();
}
intersection() {
const { pos, r, environment, history } = this;
for (let obsticle of environment) {
for (let segment of obsticle.lines) {
const intersect = intersectionCL([...pos, 1], segment);
const notOnPoint = dist2D(history[history.length - 1], pos) > 10;
if (intersect && notOnPoint) {
const beam = [history[history.length - 1], pos];
const angle = degBetweenLines(beam, segment);
const segAngle = degBetweenLines(segment, [[0, 0], [width, 0]]);
this.history.push(pos);
this.deg = this.deg + angle * 2;
}
}
}
}
step() {
const { pos, deg, history, stepSize } = this;
const nextPos = coordsFromDeg(deg, stepSize, pos);
this.pos = nextPos;
}
draw() {
const { history, pos, context, r } = this;
const c = "rgba(150, 150, 150, 1)";
if (showTrace) {
line(history[history.length - 1], pos, context, { color: c });
for (let i = history.length - 1; i > 0; --i) {
line(history[i], history[i - 1], context, { color: c });
}
}
circle([...pos, r], context);
}
}