Created
October 15, 2024 20:05
-
-
Save GabrielModog/dabff3dfc6e52a6e8ea04e4336cbb64d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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