-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDDA.html
More file actions
53 lines (47 loc) · 1.52 KB
/
DDA.html
File metadata and controls
53 lines (47 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>DDA Line Drawing Algorithm</title>
<style>
canvas {
border: 1px solid black;
margin-top: 10px;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
window.onload = function() {
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
canvas.width = 800;
canvas.height = 600;
// Hardcoded start and end points
const x1 = 50; // Start X
const y1 = 50; // Start Y
const x2 = 300; // End X
const y2 = 300; // End Y
ddaLine(ctx, x1, y1, x2, y2);
function ddaLine(ctx, x1, y1, x2, y2) {
const dx = x2 - x1;
const dy = y2 - y1;
const steps = Math.abs(dx) > Math.abs(dy) ? Math.abs(dx) : Math.abs(dy);
const xIncrement = dx / steps;
const yIncrement = dy / steps;
let x = x1;
let y = y1;
ctx.beginPath();
for (let i = 0; i <= steps; i++) {
ctx.rect(Math.round(x), Math.round(y), 1, 1);
console.log(`Point(${Math.round(x)}, ${Math.round(y)})`); // Log points to the console
x += xIncrement;
y += yIncrement;
}
ctx.fill();
}
};
</script>
</body>
</html>