Tengo un programa en JavaScript, pero no puedo mostrar los puntos del gráfico como (2,1), (3,2), (4,2), (5,3), (6,4), (7, 4), (8,5).
function dda(x1, y1, x2, y2) { var dx = x2 - x1; var dy = y2 - y1; if (Math.abs(dx) > Math.abs(dy)) { var step = Math.abs(dx); } else { var step = Math.abs(dy); } var x_inc = dx / step; var y_inc = dy / step; var x = x1; var y = y1; for (var k = 1; k < step; k++) { x = x + x_inc; y = y + y_inc; //I'm confused about this part } return x; } console.log(dda(2, 1, 8, 5));Necesita recopilar los valores de x e y .
Además de esto, debe comenzar con k = 0 e incrementar los valores después de usar los valores.
function dda(x1, y1, x2, y2) { const dx = x2 - x1, dy = y2 - y1, step = Math.abs(dx) > Math.abs(dy) ? Math.abs(dx) : Math.abs(dy), x_inc = dx / step, y_inc = dy / step, result = []; for (let k = 0, x = x1, y = y1; k < step; k++) { result.push([x, y]); x += x_inc; y += y_inc; } return result; } console.log(dda(2, 1, 8, 5)); .as-console-wrapper { max-height: 100% !important; top: 0; }