Bresenham&039;s线算法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Bresenham&039;s线算法相关的知识,希望对你有一定的参考价值。
Code obtained from a [stackoverflow question](http://stackoverflow.com/questions/4672279/bresenham-algorithm-in-javascript). This is a javascript implementation of the [Bresenham line algorithm](http://en.wikipedia.org/wiki/Bresenham's_line_algorithm). Given two points, this function will return an array of coordinates which go from point A to point B, one step at a time.
function calcStraightLine (startCoordinates, endCoordinates) { var coordinatesArray = new Array(); // Translate coordinates var x1 = startCoordinates.left; var y1 = startCoordinates.top; var x2 = endCoordinates.left; var y2 = endCoordinates.top; // Define differences and error check var dx = Math.abs(x2 - x1); var dy = Math.abs(y2 - y1); var sx = (x1 < x2) ? 1 : -1; var sy = (y1 < y2) ? 1 : -1; var err = dx - dy; // Set first coordinates coordinatesArray.push(new Coordinates(y1, x1)); // Main loop while (!((x1 == x2) && (y1 == y2))) { var e2 = err << 1; if (e2 > -dy) { err -= dy; x1 += sx; } if (e2 < dx) { err += dx; y1 += sy; } // Set coordinates coordinatesArray.push(new Coordinates(y1, x1)); } // Return the result return coordinatesArray; }
以上是关于Bresenham&039;s线算法的主要内容,如果未能解决你的问题,请参考以下文章
用C++如何实现bresenham画线算法?计算机图形学上面有个drawpixel的函数。不知道怎么用。