bresenham-zingl
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -15,4 +15,6 @@ const screenWidth = document.documentElement.clientWidth; | ||
function scaleGraphToFitBounds(G, bounds) { | ||
function scaleGraphToFitBounds(G, bounds, margin) { | ||
var min = Math.min, max = Math.max; | ||
bounds[0] += margin; bounds[1] += margin; | ||
bounds[2] -= margin; bounds[3] -= margin; | ||
var bbox = getBounds(G); | ||
@@ -37,2 +39,3 @@ var cx = (bounds[0] + bounds[2]) / 2, | ||
}); | ||
return G; | ||
} | ||
@@ -94,4 +97,2 @@ | ||
// nodes; | ||
ctx.beginPath(); | ||
@@ -116,2 +117,6 @@ ctx.fillStyle = 'orange'; | ||
const m = w / 5; | ||
console.log(w / 4); | ||
bresenham.cubicBezierAA(0 + m, 400, 0 + m, 0, w - m, 0, w - m, 400, setPixelAA); | ||
bresenham.cubicBezierAA(0 + m, h - 400, 0 + m, h, w - m, h, w - m, h - 400, setPixelAA); | ||
console.timeEnd('render'); | ||
@@ -122,3 +127,3 @@ } | ||
d3.json('data.json').then((json) => { | ||
data = json; | ||
data = scaleGraphToFitBounds(json, [0,0,w, h], 50); | ||
nodesMap = data.nodes.reduce((a, n) => { | ||
@@ -125,0 +130,0 @@ a[n.id] = n; |
@@ -597,3 +597,3 @@ (function (global, factory) { | ||
r = (y1 - y0) * (t - x0) / (x1 - x0) + y0; /* intersect P3 | P0 P1 */ | ||
quadBezierSegment$1(x0, y0, x, Math.floor(r + 0.5), x, y, setPixel); | ||
quadBezierSegment(x0, y0, x, Math.floor(r + 0.5), x, y, setPixel); | ||
r = (y1 - y2) * (t - x2) / (x1 - x2) + y2; /* intersect P4 | P1 P2 */ | ||
@@ -612,3 +612,3 @@ x0 = x1 = x; | ||
r = (x1 - x0) * (t - y0) / (y1 - y0) + x0; /* intersect P6 | P0 P1 */ | ||
quadBezierSegment$1(x0, y0, Math.floor(r + 0.5), y, x, y, setPixel); | ||
quadBezierSegment(x0, y0, Math.floor(r + 0.5), y, x, y, setPixel); | ||
r = (x1 - x2) * (t - y2) / (y1 - y2) + x2; /* intersect P7 | P1 P2 */ | ||
@@ -619,3 +619,3 @@ x0 = x; | ||
} | ||
quadBezierSegment$1(x0, y0, x1, y1, x2, y2, setPixel); /* remaining part */ | ||
quadBezierSegment(x0, y0, x1, y1, x2, y2, setPixel); /* remaining part */ | ||
} | ||
@@ -633,3 +633,3 @@ | ||
*/ | ||
function quadBezierSegment$1(x0, y0, x1, y1, x2, y2, setPixel) { | ||
function quadBezierSegment(x0, y0, x1, y1, x2, y2, setPixel) { | ||
var sx = x2 - x1, | ||
@@ -896,9 +896,7 @@ sy = y2 - y1; | ||
var exit = false; | ||
for (pxy = xy, fx = fy = f; x0 != x3 && y0 != y3;) { | ||
outer:for (pxy = xy, fx = fy = f; x0 != x3 && y0 != y3;) { | ||
setPixel(x0, y0); /* plot curve */ | ||
do { /* move sub-steps of one pixel */ | ||
if (dx > pxy || dy < pxy) { | ||
exit = true; | ||
break; | ||
break outer; | ||
} /* confuMath.sing values */ | ||
@@ -931,9 +929,9 @@ y1 = 2 * ex - dy; /* save value for test of y step */ | ||
} | ||
if (exit) { | ||
xx = x0; | ||
x0 = x3; | ||
x3 = xx; | ||
sx = -sx; | ||
xb = -xb; /* swap legs */ | ||
} | ||
//exit: | ||
xx = x0; | ||
x0 = x3; | ||
x3 = xx; | ||
sx = -sx; | ||
xb = -xb; /* swap legs */ | ||
yy = y0; | ||
@@ -950,14 +948,121 @@ y0 = y3; | ||
/** | ||
* plot any cubic Bezier curve | ||
* @param {number} x0 | ||
* @param {number} y0 | ||
* @param {number} x1 | ||
* @param {number} y1 | ||
* @param {number} x2 | ||
* @param {number} y2 | ||
* @param {number} x3 | ||
* @param {number} y3 | ||
* @param {setPixel} setPixel | ||
* Plot limited anti-aliased cubic Bezier segment | ||
* @param {Number} x0 | ||
* @param {Number} y0 | ||
* @param {Number} x1 | ||
* @param {Number} y1 | ||
* @param {Number} x2 | ||
* @param {Number} y2 | ||
* @param {Number} x3 | ||
* @param {Number} y3 | ||
* @param {setPixelAA} setPixelAA | ||
*/ | ||
function cubicBezier(x0, y0, x1, y1, x2, y2, x3, y3, setPixel) { | ||
function cubicBezierSegmentAA(x0, y0, x1, y1, x2, y2, x3, y3, setPixelAA) { | ||
var f, fx, fy, leg = 1; | ||
var sx = x0 < x3 ? 1 : -1, sy = y0 < y3 ? 1 : -1; /* step direction */ | ||
var xc = -Math.abs(x0 + x1 - x2 - x3), | ||
xa = xc - 4 * sx * (x1 - x2), | ||
xb = sx * (x0 - x1 - x2 + x3); | ||
var yc = -Math.abs(y0 + y1 - y2 - y3), | ||
ya = yc - 4 * sy * (y1 - y2), | ||
yb = sy * (y0 - y1 - y2 + y3); | ||
var ab, ac, bc, ba, xx, xy, yy, dx, dy, ex, px, py, ed, ip; | ||
var EP = 0.01; | ||
/* check for curve restrains */ | ||
/* slope P0-P1 == P2-P3 and (P0-P3 == P1-P2 or no slope change) */ | ||
assert((x1 - x0) * (x2 - x3) < EP && | ||
((x3 - x0) * (x1 - x2) < EP || xb * xb < xa * xc + EP)); | ||
assert((y1 - y0) * (y2 - y3) < EP && | ||
((y3 - y0) * (y1 - y2) < EP || yb * yb < ya * yc + EP)); | ||
if (xa === 0 && ya === 0) { /* quadratic Bezier */ | ||
sx = Math.floor((3 * x1 - x0 + 1) / 2); | ||
sy = Math.floor((3 * y1 - y0 + 1) / 2); /* new midpoint */ | ||
return quadBezierSegmentAA(x0,y0, sx,sy, x3,y3, setPixelAA); | ||
} | ||
x1 = (x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0) + 1; /* line lengths */ | ||
x2 = (x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3) + 1; | ||
do { /* loop over both ends */ | ||
ab = xa * yb-xb * ya; ac = xa * yc-xc * ya; bc = xb * yc-xc * yb; | ||
ip = 4 * ab * bc - ac * ac; /* self intersection loop at all? */ | ||
/* P0 part of self-intersection loop? */ | ||
ex = ab * (ab + ac - 3 * bc) + ac * ac; | ||
f = ex > 0 ? 1 : Math.sqrt(1 + 1024 / x1); /* calculate resolution */ | ||
ab *= f; ac *= f; bc *= f; ex *= f * f; /* increase resolution */ | ||
xy = 9 * (ab + ac + bc) / 8; | ||
ba = 8 * (xa - ya); /* init differences of 1st degree */ | ||
dx = 27 * (8 * ab * (yb * yb-ya * yc) + ex * (ya + 2 * yb + yc)) / 64 - ya * ya * (xy - ya); | ||
dy = 27 * (8 * ab * (xb * xb-xa * xc) - ex * (xa + 2 * xb + xc)) / 64 - xa * xa * (xy + xa); | ||
/* init differences of 2nd degree */ | ||
xx = 3 * (3 * ab * (3 * yb * yb - ya * ya - 2 * ya * yc) - ya * (3 * ac * (ya + yb) + ya * ba)) / 4; | ||
yy = 3 * (3 * ab * (3 * xb * xb - xa * xa - 2 * xa * xc) - xa * (3 * ac * (xa + xb) + xa * ba)) / 4; | ||
xy = xa * ya * (6 * ab + 6 * ac - 3 * bc + ba); | ||
ac = ya * ya; | ||
ba = xa * xa; | ||
xy = 3 * (xy + 9 * f * (ba * yb * yc-xb * xc * ac) - 18 * xb * yb * ab) / 8; | ||
if (ex < 0) { /* negate values if inside self-intersection loop */ | ||
dx = -dx; dy = -dy; xx = -xx; yy = -yy; xy = -xy; ac = -ac; ba = -ba; | ||
} /* init differences of 3rd degree */ | ||
ab = 6 * ya * ac; ac = -6 * xa * ac; bc = 6 * ya * ba; ba = -6 * xa * ba; | ||
dx += xy; ex = dx+dy; dy += xy; /* error of 1st step */ | ||
var exit = false; | ||
outer: for (fx = fy = f; x0 !== x3 && y0 !== y3; ) { | ||
y1 = Math.min(Math.abs(xy-dx), Math.abs(dy-xy)); | ||
/* approximate error distance */ | ||
ed = Math.max(Math.abs(xy-dx), Math.abs(dy-xy)); | ||
ed = f * (ed+2 * ed * y1 * y1 / (4 * ed * ed + y1 * y1)); | ||
y1 = 255 * Math.abs(ex - (f - fx + 1) * dx - (f - fy + 1) * dy + f * xy) / ed; | ||
if (y1 < 256) { setPixelAA(x0, y0, y1); } /* plot curve */ | ||
/* pixel intensity x move */ | ||
px = Math.abs(ex - (f - fx + 1) * dx + (fy - 1) * dy); | ||
/* pixel intensity y move */ | ||
py = Math.abs(ex + (fx - 1) * dx - (f - fy + 1) * dy); | ||
y2 = y0; | ||
do { /* move sub-steps of one pixel */ | ||
if (ip >= -EP) /* intersection possible? -> check.. */ | ||
{ if (dx+xx > xy || dy+yy < xy) { /* two x or y steps */ | ||
exit = true; break outer; | ||
} } | ||
y1 = 2 * ex+dx; /* save value for test of y step */ | ||
if (2 * ex+dy > 0) { /* x sub-step */ | ||
fx--; ex += dx += xx; dy += xy += ac; yy += bc; xx += ab; | ||
} else if (y1 > 0) { /* tiny nearly cusp */ | ||
exit = true; break outer; | ||
} | ||
if (y1 <= 0) { /* y sub-step */ | ||
fy--; ex += dy += yy; dx += xy += bc; xx += ac; yy += ba; | ||
} | ||
} while (fx > 0 && fy > 0); /* pixel complete? */ | ||
if (2 * fy <= f) { /* x+ anti-aliasing pixel */ | ||
if (py < ed) { setPixelAA(x0 + sx, y0, 255 * py / ed); }/* plot curve */ | ||
y0 += sy; fy += f; /* y step */ | ||
} | ||
if (2 * fx <= f) { /* y+ anti-aliasing pixel */ | ||
if (px < ed) { setPixelAA(x0, y2 + sy, 255 * px / ed); }/* plot curve */ | ||
x0 += sx; fx += f; /* x step */ | ||
} | ||
} | ||
if (exit) { | ||
if (2 * ex < dy && 2 * fy <= f+2) { /* round x+ approximation pixel */ | ||
if (py < ed) { setPixelAA(x0 + sx, y0, 255 * py / ed); } /* plot curve */ | ||
y0 += sy; | ||
} | ||
if (2 * ex > dx && 2 * fx <= f+2) { /* round y+ approximation pixel */ | ||
if (px < ed) { setPixelAA(x0, y2 + sy, 255 * px / ed); } /* plot curve */ | ||
x0 += sx; | ||
} | ||
xx = x0; x0 = x3; x3 = xx; sx = -sx; xb = -xb; /* swap legs */ | ||
yy = y0; y0 = y3; y3 = yy; sy = -sy; yb = -yb; x1 = x2; | ||
} | ||
break; /* finish curve by line */ | ||
} while (leg--); /* try other end */ | ||
/* remaining part in case of cusp or crunode */ | ||
lineAA(x0,y0, x3,y3, setPixelAA); | ||
} | ||
function cubicBezierGeneral(x0, y0, x1, y1, x2, y2, x3, y3, segment, setPixel) { | ||
var n = 0, i = 0; | ||
@@ -1025,3 +1130,3 @@ var xc = x0 + x1 - x2 - x3, | ||
if (x0 != x3 || y0 != y3) /* segment t1 - t2 */ | ||
{ cubicBezierSegment(x0, y0, x0 + fx1, y0 + fy1, x0 + fx2, y0 + fy2, x3, y3, setPixel); } | ||
{ segment(x0, y0, x0 + fx1, y0 + fy1, x0 + fx2, y0 + fy2, x3, y3, setPixel); } | ||
x0 = x3; | ||
@@ -1036,2 +1141,35 @@ y0 = y3; | ||
/** | ||
* plot any cubic Bezier curve | ||
* @param {number} x0 | ||
* @param {number} y0 | ||
* @param {number} x1 | ||
* @param {number} y1 | ||
* @param {number} x2 | ||
* @param {number} y2 | ||
* @param {number} x3 | ||
* @param {number} y3 | ||
* @param {setPixel} setPixel | ||
*/ | ||
function cubicBezier(x0, y0, x1, y1, x2, y2, x3, y3, setPixel) { | ||
cubicBezierGeneral(x0, y0, x1, y1, x2, y2, x3, y3, cubicBezierSegment, setPixel); | ||
} | ||
/** | ||
* plot any cubic Bezier curve | ||
* @param {number} x0 | ||
* @param {number} y0 | ||
* @param {number} x1 | ||
* @param {number} y1 | ||
* @param {number} x2 | ||
* @param {number} y2 | ||
* @param {number} x3 | ||
* @param {number} y3 | ||
* @param {setPixelAlpha} setPixelAA | ||
*/ | ||
function cubicBezierAA(x0, y0, x1, y1, x2, y2, x3, y3, setPixelAA) { | ||
cubicBezierGeneral(x0, y0, x1, y1, x2, y2, x3, y3, cubicBezierSegmentAA, setPixelAA); | ||
} | ||
/** | ||
* Bresenham rasterisation functions by Alois Zingl | ||
@@ -1067,3 +1205,3 @@ * @author Alois Zingl | ||
exports.quadBezier = quadBezier; | ||
exports.quadBezierSegment = quadBezierSegment$1; | ||
exports.quadBezierSegment = quadBezierSegment; | ||
exports.quadBezierAA = quadBezierAA; | ||
@@ -1075,3 +1213,5 @@ exports.quadBezierSegmentAA = quadBezierSegmentAA; | ||
exports.cubicBezierSegment = cubicBezierSegment; | ||
exports.cubicBezierSegmentAA = cubicBezierSegmentAA; | ||
exports.cubicBezier = cubicBezier; | ||
exports.cubicBezierAA = cubicBezierAA; | ||
@@ -1078,0 +1218,0 @@ Object.defineProperty(exports, '__esModule', { value: true }); |
{ | ||
"name": "bresenham-zingl", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Bresenham rasterisation algorithms by Alois Zingl", | ||
@@ -5,0 +5,0 @@ "main": "dist/bresenham.js", |
@@ -223,3 +223,3 @@ # Bresenham rasterisation functions by Alois Zingl | ||
### `quadBezierSegment$1(x0, y0, x1, y1, x2, y2, setPixel)` | ||
### `quadBezierSegment(x0, y0, x1, y1, x2, y2, setPixel)` | ||
@@ -296,3 +296,21 @@ plot a limited quadratic Bezier segment | ||
### `cubicBezierAA(x0, y0, x1, y1, x2, y2, x3, y3, setPixelAA)` | ||
plot any cubic Bezier curve | ||
### Parameters | ||
| parameter | type | description | | ||
| ---------- | ------------- | ----------- | | ||
| `x0` | number | | | ||
| `y0` | number | | | ||
| `x1` | number | | | ||
| `y1` | number | | | ||
| `x2` | number | | | ||
| `y2` | number | | | ||
| `x3` | number | | | ||
| `y3` | number | | | ||
| `setPixel` | setPixelAlpha | | | ||
## License | ||
@@ -299,0 +317,0 @@ |
import assert from '../assert'; | ||
import { | ||
quadBezierSegmentAA, | ||
quadBezierSegment | ||
} from './quadratic'; | ||
import { line, lineAA } from '../line'; | ||
@@ -85,9 +89,7 @@ | ||
var exit = false; | ||
for (pxy = xy, fx = fy = f; x0 != x3 && y0 != y3;) { | ||
outer:for (pxy = xy, fx = fy = f; x0 != x3 && y0 != y3;) { | ||
setPixel(x0, y0); /* plot curve */ | ||
do { /* move sub-steps of one pixel */ | ||
if (dx > pxy || dy < pxy) { | ||
exit = true; | ||
break; | ||
break outer; | ||
} /* confuMath.sing values */ | ||
@@ -120,9 +122,9 @@ y1 = 2 * ex - dy; /* save value for test of y step */ | ||
} | ||
if (exit) { | ||
xx = x0; | ||
x0 = x3; | ||
x3 = xx; | ||
sx = -sx; | ||
xb = -xb; /* swap legs */ | ||
} | ||
//exit: | ||
xx = x0; | ||
x0 = x3; | ||
x3 = xx; | ||
sx = -sx; | ||
xb = -xb; /* swap legs */ | ||
yy = y0; | ||
@@ -139,14 +141,121 @@ y0 = y3; | ||
/** | ||
* plot any cubic Bezier curve | ||
* @param {number} x0 | ||
* @param {number} y0 | ||
* @param {number} x1 | ||
* @param {number} y1 | ||
* @param {number} x2 | ||
* @param {number} y2 | ||
* @param {number} x3 | ||
* @param {number} y3 | ||
* @param {setPixel} setPixel | ||
* Plot limited anti-aliased cubic Bezier segment | ||
* @param {Number} x0 | ||
* @param {Number} y0 | ||
* @param {Number} x1 | ||
* @param {Number} y1 | ||
* @param {Number} x2 | ||
* @param {Number} y2 | ||
* @param {Number} x3 | ||
* @param {Number} y3 | ||
* @param {setPixelAA} setPixelAA | ||
*/ | ||
export function cubicBezier(x0, y0, x1, y1, x2, y2, x3, y3, setPixel) { | ||
export function cubicBezierSegmentAA(x0, y0, x1, y1, x2, y2, x3, y3, setPixelAA) { | ||
let f, fx, fy, leg = 1; | ||
let sx = x0 < x3 ? 1 : -1, sy = y0 < y3 ? 1 : -1; /* step direction */ | ||
let xc = -Math.abs(x0 + x1 - x2 - x3), | ||
xa = xc - 4 * sx * (x1 - x2), | ||
xb = sx * (x0 - x1 - x2 + x3); | ||
let yc = -Math.abs(y0 + y1 - y2 - y3), | ||
ya = yc - 4 * sy * (y1 - y2), | ||
yb = sy * (y0 - y1 - y2 + y3); | ||
let ab, ac, bc, ba, xx, xy, yy, dx, dy, ex, px, py, ed, ip; | ||
const EP = 0.01; | ||
/* check for curve restrains */ | ||
/* slope P0-P1 == P2-P3 and (P0-P3 == P1-P2 or no slope change) */ | ||
assert((x1 - x0) * (x2 - x3) < EP && | ||
((x3 - x0) * (x1 - x2) < EP || xb * xb < xa * xc + EP)); | ||
assert((y1 - y0) * (y2 - y3) < EP && | ||
((y3 - y0) * (y1 - y2) < EP || yb * yb < ya * yc + EP)); | ||
if (xa === 0 && ya === 0) { /* quadratic Bezier */ | ||
sx = Math.floor((3 * x1 - x0 + 1) / 2); | ||
sy = Math.floor((3 * y1 - y0 + 1) / 2); /* new midpoint */ | ||
return quadBezierSegmentAA(x0,y0, sx,sy, x3,y3, setPixelAA); | ||
} | ||
x1 = (x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0) + 1; /* line lengths */ | ||
x2 = (x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3) + 1; | ||
do { /* loop over both ends */ | ||
ab = xa * yb-xb * ya; ac = xa * yc-xc * ya; bc = xb * yc-xc * yb; | ||
ip = 4 * ab * bc - ac * ac; /* self intersection loop at all? */ | ||
/* P0 part of self-intersection loop? */ | ||
ex = ab * (ab + ac - 3 * bc) + ac * ac; | ||
f = ex > 0 ? 1 : Math.sqrt(1 + 1024 / x1); /* calculate resolution */ | ||
ab *= f; ac *= f; bc *= f; ex *= f * f; /* increase resolution */ | ||
xy = 9 * (ab + ac + bc) / 8; | ||
ba = 8 * (xa - ya); /* init differences of 1st degree */ | ||
dx = 27 * (8 * ab * (yb * yb-ya * yc) + ex * (ya + 2 * yb + yc)) / 64 - ya * ya * (xy - ya); | ||
dy = 27 * (8 * ab * (xb * xb-xa * xc) - ex * (xa + 2 * xb + xc)) / 64 - xa * xa * (xy + xa); | ||
/* init differences of 2nd degree */ | ||
xx = 3 * (3 * ab * (3 * yb * yb - ya * ya - 2 * ya * yc) - ya * (3 * ac * (ya + yb) + ya * ba)) / 4; | ||
yy = 3 * (3 * ab * (3 * xb * xb - xa * xa - 2 * xa * xc) - xa * (3 * ac * (xa + xb) + xa * ba)) / 4; | ||
xy = xa * ya * (6 * ab + 6 * ac - 3 * bc + ba); | ||
ac = ya * ya; | ||
ba = xa * xa; | ||
xy = 3 * (xy + 9 * f * (ba * yb * yc-xb * xc * ac) - 18 * xb * yb * ab) / 8; | ||
if (ex < 0) { /* negate values if inside self-intersection loop */ | ||
dx = -dx; dy = -dy; xx = -xx; yy = -yy; xy = -xy; ac = -ac; ba = -ba; | ||
} /* init differences of 3rd degree */ | ||
ab = 6 * ya * ac; ac = -6 * xa * ac; bc = 6 * ya * ba; ba = -6 * xa * ba; | ||
dx += xy; ex = dx+dy; dy += xy; /* error of 1st step */ | ||
let exit = false; | ||
outer: for (fx = fy = f; x0 !== x3 && y0 !== y3; ) { | ||
y1 = Math.min(Math.abs(xy-dx), Math.abs(dy-xy)); | ||
/* approximate error distance */ | ||
ed = Math.max(Math.abs(xy-dx), Math.abs(dy-xy)); | ||
ed = f * (ed+2 * ed * y1 * y1 / (4 * ed * ed + y1 * y1)); | ||
y1 = 255 * Math.abs(ex - (f - fx + 1) * dx - (f - fy + 1) * dy + f * xy) / ed; | ||
if (y1 < 256) setPixelAA(x0, y0, y1); /* plot curve */ | ||
/* pixel intensity x move */ | ||
px = Math.abs(ex - (f - fx + 1) * dx + (fy - 1) * dy); | ||
/* pixel intensity y move */ | ||
py = Math.abs(ex + (fx - 1) * dx - (f - fy + 1) * dy); | ||
y2 = y0; | ||
do { /* move sub-steps of one pixel */ | ||
if (ip >= -EP) /* intersection possible? -> check.. */ | ||
if (dx+xx > xy || dy+yy < xy) { /* two x or y steps */ | ||
exit = true; break outer; | ||
} | ||
y1 = 2 * ex+dx; /* save value for test of y step */ | ||
if (2 * ex+dy > 0) { /* x sub-step */ | ||
fx--; ex += dx += xx; dy += xy += ac; yy += bc; xx += ab; | ||
} else if (y1 > 0) { /* tiny nearly cusp */ | ||
exit = true; break outer; | ||
} | ||
if (y1 <= 0) { /* y sub-step */ | ||
fy--; ex += dy += yy; dx += xy += bc; xx += ac; yy += ba; | ||
} | ||
} while (fx > 0 && fy > 0); /* pixel complete? */ | ||
if (2 * fy <= f) { /* x+ anti-aliasing pixel */ | ||
if (py < ed) setPixelAA(x0 + sx, y0, 255 * py / ed);/* plot curve */ | ||
y0 += sy; fy += f; /* y step */ | ||
} | ||
if (2 * fx <= f) { /* y+ anti-aliasing pixel */ | ||
if (px < ed) setPixelAA(x0, y2 + sy, 255 * px / ed);/* plot curve */ | ||
x0 += sx; fx += f; /* x step */ | ||
} | ||
} | ||
if (exit) { | ||
if (2 * ex < dy && 2 * fy <= f+2) { /* round x+ approximation pixel */ | ||
if (py < ed) setPixelAA(x0 + sx, y0, 255 * py / ed); /* plot curve */ | ||
y0 += sy; | ||
} | ||
if (2 * ex > dx && 2 * fx <= f+2) { /* round y+ approximation pixel */ | ||
if (px < ed) setPixelAA(x0, y2 + sy, 255 * px / ed); /* plot curve */ | ||
x0 += sx; | ||
} | ||
xx = x0; x0 = x3; x3 = xx; sx = -sx; xb = -xb; /* swap legs */ | ||
yy = y0; y0 = y3; y3 = yy; sy = -sy; yb = -yb; x1 = x2; | ||
} | ||
break; /* finish curve by line */ | ||
} while (leg--); /* try other end */ | ||
/* remaining part in case of cusp or crunode */ | ||
lineAA(x0,y0, x3,y3, setPixelAA); | ||
} | ||
function cubicBezierGeneral(x0, y0, x1, y1, x2, y2, x3, y3, segment, setPixel) { | ||
var n = 0, i = 0; | ||
@@ -214,3 +323,3 @@ var xc = x0 + x1 - x2 - x3, | ||
if (x0 != x3 || y0 != y3) /* segment t1 - t2 */ | ||
cubicBezierSegment(x0, y0, x0 + fx1, y0 + fy1, x0 + fx2, y0 + fy2, x3, y3, setPixel); | ||
segment(x0, y0, x0 + fx1, y0 + fy1, x0 + fx2, y0 + fy2, x3, y3, setPixel); | ||
x0 = x3; | ||
@@ -223,1 +332,34 @@ y0 = y3; | ||
} | ||
/** | ||
* plot any cubic Bezier curve | ||
* @param {number} x0 | ||
* @param {number} y0 | ||
* @param {number} x1 | ||
* @param {number} y1 | ||
* @param {number} x2 | ||
* @param {number} y2 | ||
* @param {number} x3 | ||
* @param {number} y3 | ||
* @param {setPixel} setPixel | ||
*/ | ||
export function cubicBezier(x0, y0, x1, y1, x2, y2, x3, y3, setPixel) { | ||
cubicBezierGeneral(x0, y0, x1, y1, x2, y2, x3, y3, cubicBezierSegment, setPixel); | ||
} | ||
/** | ||
* plot any cubic Bezier curve | ||
* @param {number} x0 | ||
* @param {number} y0 | ||
* @param {number} x1 | ||
* @param {number} y1 | ||
* @param {number} x2 | ||
* @param {number} y2 | ||
* @param {number} x3 | ||
* @param {number} y3 | ||
* @param {setPixelAlpha} setPixelAA | ||
*/ | ||
export function cubicBezierAA(x0, y0, x1, y1, x2, y2, x3, y3, setPixelAA) { | ||
cubicBezierGeneral(x0, y0, x1, y1, x2, y2, x3, y3, cubicBezierSegmentAA, setPixelAA); | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
252641
2432
325