Skip to content

Instantly share code, notes, and snippets.

@GabrielModog
Created October 15, 2024 20:05
Show Gist options
  • Save GabrielModog/dabff3dfc6e52a6e8ea04e4336cbb64d to your computer and use it in GitHub Desktop.
Save GabrielModog/dabff3dfc6e52a6e8ea04e4336cbb64d to your computer and use it in GitHub Desktop.
function drawLine(start, end) {
const points = []
if(start.x === end.x && start.y === end.y) {
return [start]
}
const dx = Math.abs(end.x - start.x)
const dy = Math.abs(end.y - start.y)
const distance = Math.max(dx, dy)
for(let i = 0; i < distance; i++) {
const x = Math.floor(start.x + i * (end.x - start.x) / distance)
const y = Math.floor(start.y + i * (end.y - start.y) / distance)
points.push({ x, y })
}
points.push({ x: end.x, y: end.y })
return points
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment