Skip to content

Instantly share code, notes, and snippets.

@sXakil
Created October 26, 2020 13:24
Show Gist options
  • Save sXakil/759e0d0814ed30cdc668e2c1093084a3 to your computer and use it in GitHub Desktop.
Save sXakil/759e0d0814ed30cdc668e2c1093084a3 to your computer and use it in GitHub Desktop.
Fighter-Bomber Simulation
const S = 30, reach = 30, timeout = 10;
let xb = [100, 110, 120, 130, 140, 149, 158, 168, 179, 188, 198, 209, 220],
yb = [0, 3, 6, 10, 15, 20, 26, 32, 37, 34, 30, 27, 23],
yf = [60],
xf = [0],
dist = [],
sinA = [],
cosA =[],
flag = false;
function run(time, intv = 1) {
let t = [];
for(let i = 0; i <= time; i += intv) {
t.push(i);
let d = Math.sqrt((yb[i] - yf[i]) ** 2 + (xb[i] - xf[i]) ** 2);
dist.push(d);
sin = (yb[i] - yf[i])/d;
sinA.push(sin);
cos = (xb[i] - xf[i])/d;
cosA.push(cos);
if(d <= reach && i <= timeout) {
flag = true;
break;
}
yf.push(yf[i] + sin * S);
xf.push(xf[i] + cos * S);
}
console.log(' t: ', t.map(r => `${r}`.padEnd(10)).join(''));
console.log(' yb(t): ', yb.slice(0, t.length).map(r => `${r}`.padEnd(10)).join(''));
console.log(' yf(t): ', yf.map(r => parseFloat(r).toFixed(3).padEnd(10)).join(''));
console.log(' xb(t): ', xb.slice(0, t.length).map(r => `${r}`.padEnd(10)).join(''));
console.log(' xf(t): ', xf.map(r => parseFloat(r).toFixed(3).padEnd(10)).join(''));
console.log('dist(t): ', dist.map(r => parseFloat(r).toFixed(3).padEnd(10)).join(''));
console.log(' sinA: ', sinA.map(r => parseFloat(r).toFixed(3).padEnd(10)).join(''));
console.log(' cosA: ', cosA.map(r => parseFloat(r).toFixed(3).padEnd(10)).join(''));
if(flag) {
console.log(`\n\nSucceded at ${t[t.length- 1]} unit time when the distance between them is ${parseFloat(dist[dist.length - 1]).toFixed(3)} unit`);
} else {
console.log('Failed!');
}
}
run(xb.length);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment