Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
📐 basic geometry 📏 solution in Clear category for Escape by vincent.tscherter
import assert from "assert";
var escape = ([W, H, d], [x, y, vx, vy], cx, cy, n = 20) => {
while (n--) {
cy = y + vy * ((cx = vx >= 0 ? W : 0) - x) / vx ;
if (0 <= cy && cy <= H) {
[x, y, vx, vy] = [cx, cy, -vx, (cy == 0 || cy == H ? -1 : 1) * vy];
continue;
}
cx = x + vx * ((cy = vy >= 0 ? H : 0) - y) / vy;
if (vy > 0 && cx > W / 2 - d / 2 && cx < W / 2 + d / 2) {
return true;
}
[x, y, vx, vy] = [cx, cy, vx, -vy];
} return false;
}
Nov. 22, 2020