Skip to content

Commit 94b706a

Browse files
committed
Update bresenham
1 parent 1dab02e commit 94b706a

File tree

1 file changed

+42
-35
lines changed

1 file changed

+42
-35
lines changed
Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,45 @@
1-
/**
2-
* Bresenham's line drawing algorithm.
3-
* It has complexity O(n)
4-
* @param {number} x1 The first coordinate of the beginning of the line
5-
* @param {number} y1 The second coordinate of the beginning of the line
6-
* @param {number} x2 The first coordinate of the end of the line
7-
* @param {number} y2 The second coordinate of the end of the line
8-
*/
9-
function drawLine(x1, y1, x2, y2) {
10-
var dx = Math.abs(x2 - x1),
11-
dy = Math.abs(y2 - y1),
12-
cx = (x1 < x2) ? 1 : -1,
13-
cy = (y1 < y2) ? 1 : -1,
14-
error = dx - dy,
15-
doubledError;
16-
17-
while (x1 !== x2 || y1 !== y2) {
18-
drawPoint(x1, y1);
19-
doubledError = error + error;
20-
if (doubledError > -dy) {
21-
error -= dy;
22-
x1 += cx;
23-
}
24-
if (doubledError < dx) {
25-
error += dx;
26-
y1 += cy;
1+
(function (exports) {
2+
'use strict';
3+
4+
/**
5+
* Bresenham's line drawing algorithm.
6+
* It has complexity O(n)
7+
* @param {number} x1 The first coordinate of the beginning of the line
8+
* @param {number} y1 The second coordinate of the beginning of the line
9+
* @param {number} x2 The first coordinate of the end of the line
10+
* @param {number} y2 The second coordinate of the end of the line
11+
*/
12+
function drawLine(x1, y1, x2, y2) {
13+
var dx = Math.abs(x2 - x1),
14+
dy = Math.abs(y2 - y1),
15+
cx = (x1 < x2) ? 1 : -1,
16+
cy = (y1 < y2) ? 1 : -1,
17+
error = dx - dy,
18+
doubledError;
19+
20+
while (x1 !== x2 || y1 !== y2) {
21+
drawPoint(x1, y1);
22+
doubledError = error + error;
23+
if (doubledError > -dy) {
24+
error -= dy;
25+
x1 += cx;
26+
}
27+
if (doubledError < dx) {
28+
error += dx;
29+
y1 += cy;
30+
}
2731
}
2832
}
29-
}
3033

31-
/**
32-
* Draws (prints) the given coordinates
33-
* @param {number} x The first coordinate of the point
34-
* @param {number} y The second coordinate of the point
35-
*/
36-
function drawPoint(x, y) {
37-
console.log(x, y);
38-
}
34+
/**
35+
* Draws (prints) the given coordinates
36+
* @param {number} x The first coordinate of the point
37+
* @param {number} y The second coordinate of the point
38+
*/
39+
function drawPoint(x, y) {
40+
console.log(x, y);
41+
}
42+
43+
exports.drawLine = drawLine;
44+
45+
}(typeof exports === 'undefined' ? window : exports));

0 commit comments

Comments
 (0)