Суть задания в следующем: в произвольной точке взрывается бомба, взрывная волна распространяется со скоростью 1 клетка в секунду. Зеленые точки - это датчики, которые фиксируют время попадания в зону поражения. Необходимо рассчитать координаты точки взрыва, зная координаты датчиков и расстояния от эпицентра до датчиков. Функция getBombCoordinates() на данный момент только считает расстояния.
let canvas = document.querySelector('canvas')
let ctx = canvas.getContext('2d')
let w = canvas.width = 400
let h = canvas.height = 400
let colNum = 50
let rowNum = 50
let cell_w = w / colNum
let cell_h = h / rowNum
let isExploding = false
let probeTime1 = 0
let probeTime2 = 0
let probeTime3 = 0
let button = document.getElementById("button")
button.onclick = function () {
document.getElementById("result").innerHTML = "Результат: " + getBombCoordinates(probeTime1,probeTime2,probeTime3)
}
canvas.addEventListener('click', setBomb)
drawNet()
function drawNet() {
for (let x = 0; x < w +1; x += 8) {
ctx.moveTo(x, 0)
ctx.lineTo(x, 400)
}
for (let y = 0; y < h+1; y += 8) {
ctx.moveTo(0, y)
ctx.lineTo(400, y)
}
ctx.stroke()
ctx.beginPath()
}
function explode(x, y, blowRadius) {
isExploding = true
let probe1 = ctx.getImageData(128, 32, 8, 8).data
let probe2 = ctx.getImageData(80, 336, 8, 8).data
let probe3 = ctx.getImageData(344, 296, 8, 8).data
let top_left = ctx.getImageData(1, 1, 1, 1).data
let top_right = ctx.getImageData(399, 1, 1, 1).data
let bot_left = ctx.getImageData(1, 399, 1, 1).data
let bot_right = ctx.getImageData(399, 399, 1, 1).data
ctx.fillStyle = 'red'
ctx.arc(x, y, blowRadius, 0, getRadians(360))
ctx.fill()
function getRadians(degrees) {
return (Math.PI / 180) * degrees
}
if(probe1[0] !== 255){
probeTime1++
}
if(probe2[0] !== 255){
probeTime2++
}
if(probe3[0] !== 255){
probeTime3++
}
if((top_left[0] !== 255) || (top_right[0] !== 255) || (bot_left[0] !== 255) || (bot_right[0] !== 255)) {
blowRadius += 4
setTimeout(() => explode(x, y, blowRadius), 1000)
} else {
isExploding = false
setTimeout(() => {
ctx.clearRect(0, 0, w, h)
ctx.beginPath()
drawNet()
}, 2000)
}
}
function setBomb(e) {
probeTime1 = 0
probeTime2 = 0
probeTime3 = 0
if (!isExploding) {
let x = e.offsetX
let y = e.offsetY
let c_x = Math.floor(x / cell_w)
let c_y = Math.floor(y / cell_h)
let f_x = c_x * cell_w + 4
let f_y = c_y * cell_h + 4
ctx.clearRect(0, 0, w, h)
ctx.beginPath()
drawNet()
ctx.fillStyle = 'green'
ctx.fillRect(128, 32, 8, 8)
ctx.fillRect(80, 336, 8, 8)
ctx.fillRect(344, 296, 8, 8)
console.log(c_x)
console.log(c_y)
explode(f_x, f_y, 4)
}
}
function getBombCoordinates(probeTime1,probeTime2, probeTime3){
let distance1 = probeTime1 * 0.5
let distance2 = probeTime2 * 0.5
let distance3 = probeTime3 * 0.5
let probe1X = Math.floor(128 / cell_w)
let probe1Y = Math.floor(32 / cell_h)
let probe2X = Math.floor(80 / cell_w)
let probe2Y = Math.floor(336 / cell_h)
let probe3X = Math.floor(344 / cell_w)
let probe3Y = Math.floor(296 / cell_h)
let distance = [distance1,distance2,distance3,probe1X,probe1Y,probe2X,probe2Y,probe3X,probe3Y]
return distance
}