|
[javascript] view plaincopyprint?
function setPixel(imageData, W, H, x, y, r, g, b)
{
var i = (y*W+x)*4;
imageData[i] = r;
imageData[i+1] = g;
imageData[i+2] = b;
}
/**
* Bresenham's draw line algorithm
*/
function Bresenham_drawLine(imageData, width, height, x1, y1, x2, y2, r, g, b)
{
var dx = Math.abs(x2 - x1),
dy = Math.abs(y2 - y1),
yy = 0,
t;
if (dx < dy) {
yy = 1;
t=x1; x1=y1; y1=t;
t=x2; x2=y2; y2=t;
t=dx; dx=dy; dy=t;
}
var ix = (x2 - x1) > 0 ? 1 : -1,
iy = (y2 - y1) > 0 ? 1 : -1,
cx = x1,
cy = y1,
n2dy = dy * 2,
n2dydx = (dy - dx) * 2,
d = dy * 2 - dx;
if(yy==1) {
while(cx != x2) {
if(d < 0) {
d += n2dy;
}
else {
cy += iy;
d += n2dydx;
}
setPixel(imageData, width, height, cy, cx, r, g, b);
cx += ix;
}
}
else {
while(cx != x2) {
if(d < 0) {
d += n2dy;
}
else {
cy += iy;
d += n2dydx;
}
setPixel(imageData, width, height, cx, cy, r, g, b);
cx += ix;
}
}
}
/**
* Bresenham's draw circle algorithm
*/
function _draw_circle_8(imageData, W, H, xc, yc, x, y, r, g, b)
{
setPixel(imageData, W, H, xc+x, yc+y, r, g, b);
setPixel(imageData, W, H, xc-x, yc+y, r, g, b);
setPixel(imageData, W, H, xc+x, yc-y, r, g, b);
setPixel(imageData, W, H, xc-x, yc-y, r, g, b);
setPixel(imageData, W, H, xc+y, yc+x, r, g, b);
setPixel(imageData, W, H, xc-y, yc+x, r, g, b);
setPixel(imageData, W, H, xc+y, yc-x, r, g, b);
setPixel(imageData, W, H, xc-y, yc-x, r, g, b);
}
function Bresenham_drawCircle(imageData, width, height, xc, yc, radius, fill, r, g, b)
{
var x = 0,
y = radius,
yi,
d = 3 - 2 * radius;
if (fill==1) {
while (x <= y) {
for (yi = x; yi <= y; yi ++) {
_draw_circle_8(imageData, width, height, xc, yc, x, yi, r, g, b);
}
if (d < 0) {
d = d + 4 * x + 6;
}
else {
d = d + 4 * (x - y);
y--;
}
x++;
}
}
else {
while (x <= y) {
_draw_circle_8(imageData, width, height, xc, yc, x, y, r, g, b);
if (d < 0) {
d = d + 4 * x + 6;
}
else {
d = d + 4 * (x - y);
y--;
}
x++;
}
}
} |