function findNextSquare(w, h) {
let dist = 0.2
let offset = 0
let break_check = false
while (true) {
let ne = [offset, -offset]
let se = [offset, offset]
let sw = [-offset, offset]
let nw = [-offset, -offset]
let round = []
if (offset === 0) {
round = [[setFirstX(w), 0]]
}
for (let y = ne[1]; y < se[1]; y = y + dist) {
round.push([offset, y])
}
for (let x = se[0]; x > sw[0]; x = x - dist) {
round.push([x, offset])
}
for (let y = sw[1]; y > nw[1]; y = y - dist) {
round.push([-offset, y])
}
for (let x = nw[0]; x < ne[0]; x = x + dist) {
round.push([x, -offset])
}
let candidates = []
for (let coord of round) {
let check = openCheck(coord[0] - w/2, coord[1] - h/2, w, h)
if (check) {
let boundary_check = boundaryCheck(check)
if (boundary_check) candidates.push(check)
}
}
if (candidates.length > 0) {
let distances = candidates.map(candidate => {
let distance = getDistance(candidate, { x: 0 - candidate.w/2, y: 0 - candidate.h/2 })
return distance
})
let min_index = getMinIndex(distances)
state.pictures.push(candidates[min_index])
break
}
offset = offset + dist
}
}