-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBresenham_Line_Drawing_Algo.html
More file actions
59 lines (54 loc) · 1.73 KB
/
Bresenham_Line_Drawing_Algo.html
File metadata and controls
59 lines (54 loc) · 1.73 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
54
55
56
57
58
59
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Bresenham 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
bresenhamLine(ctx, x1, y1, x2, y2);
function bresenhamLine(ctx, x1, y1, x2, y2) {
let dx = Math.abs(x2 - x1);
let dy = -Math.abs(y2 - y1);
let sx = (x1 < x2) ? 1 : -1;
let sy = (y1 < y2) ? 1 : -1;
let err = dx + dy; // error value e_xy
let e2; // error value doubled
ctx.beginPath();
while (true) {
ctx.rect(x1, y1, 1, 1); // Set pixel
console.log(`Point(${x1}, ${y1})`); // Log points to the console
if (x1 === x2 && y1 === y2) break;
e2 = 2 * err;
if (e2 >= dy) {
err += dy;
x1 += sx;
}
if (e2 <= dx) {
err += dx;
y1 += sy;
}
}
ctx.fill();
}
};
</script>
</body>
</html>