fix syntax
This commit is contained in:
166
rasterize.cm
166
rasterize.cm
@@ -7,88 +7,106 @@ var math = use('math')
|
||||
|
||||
var rasterize = {}
|
||||
|
||||
function within_wedge(dx, dy, start, end, full_circle) {
|
||||
if (full_circle) return true
|
||||
function within_wedge(dx, dy, wedge) {
|
||||
if (wedge.full) return true
|
||||
|
||||
var ang = math.arc_tangent(dy, dx)
|
||||
if (ang < 0) ang += pi * 2
|
||||
var t = ang / (pi * 2)
|
||||
|
||||
if (start <= end) return t >= start && t <= end
|
||||
return t >= start || t <= end
|
||||
if (wedge.start <= wedge.end) return t >= wedge.start && t <= wedge.end
|
||||
return t >= wedge.start || t <= wedge.end
|
||||
}
|
||||
|
||||
rasterize.ellipse = function ellipse(pos, radii, opt) {
|
||||
opt = opt || {}
|
||||
var _opt = opt || {}
|
||||
var rx = radii[0], ry = radii[1]
|
||||
if (rx <= 0 || ry <= 0) return []
|
||||
|
||||
var cx = pos[0], cy = pos[1]
|
||||
var raw_start = opt.start || 0
|
||||
var raw_end = opt.end || 1
|
||||
var raw_start = _opt.start || 0
|
||||
var raw_end = _opt.end || 1
|
||||
var full_circle = abs(raw_end - raw_start) >= 1 - 1e-9
|
||||
var start = (raw_start % 1 + 1) % 1
|
||||
var end = (raw_end % 1 + 1) % 1
|
||||
var thickness = max(1, opt.thickness || 1)
|
||||
var thickness = max(1, _opt.thickness || 1)
|
||||
var wedge_info = {start: start, end: end, full: full_circle}
|
||||
|
||||
var rx_i = rx - thickness,
|
||||
ry_i = ry - thickness
|
||||
var hole = (rx_i > 0 && ry_i > 0)
|
||||
|
||||
if (!hole && thickness == 1) {
|
||||
var points = []
|
||||
var rx_sq = rx * rx, ry_sq = ry * ry
|
||||
var two_rx_sq = rx_sq << 1, two_ry_sq = ry_sq << 1
|
||||
var x = 0, y = ry, px = 0, py = two_rx_sq * y
|
||||
var p = ry_sq - rx_sq * ry + 0.25 * rx_sq
|
||||
var points = []
|
||||
var rx_sq = rx * rx, ry_sq = ry * ry
|
||||
var two_rx_sq = null
|
||||
var two_ry_sq = null
|
||||
var x = 0, y = 0, px = 0, py = 0
|
||||
var p = 0
|
||||
|
||||
function add_pts(x, y) {
|
||||
var pts = [
|
||||
[cx + x, cy + y], [cx - x, cy + y],
|
||||
[cx + x, cy - y], [cx - x, cy - y]
|
||||
]
|
||||
points = array(points, filter(pts, pt => within_wedge(pt[0]-cx, pt[1]-cy, start, end, full_circle)))
|
||||
}
|
||||
var add_pts = function(ax, ay) {
|
||||
var pts = [
|
||||
[cx + ax, cy + ay], [cx - ax, cy + ay],
|
||||
[cx + ax, cy - ay], [cx - ax, cy - ay]
|
||||
]
|
||||
points = array(points, filter(pts, pt => within_wedge(pt[0]-cx, pt[1]-cy, wedge_info)))
|
||||
}
|
||||
|
||||
if (!hole && thickness == 1) {
|
||||
two_rx_sq = rx_sq << 1
|
||||
two_ry_sq = ry_sq << 1
|
||||
x = 0
|
||||
y = ry
|
||||
px = 0
|
||||
py = two_rx_sq * y
|
||||
p = ry_sq - rx_sq * ry + 0.25 * rx_sq
|
||||
|
||||
while (px < py) {
|
||||
add_pts(x, y)
|
||||
++x; px += two_ry_sq
|
||||
x += 1; px += two_ry_sq
|
||||
if (p < 0) p += ry_sq + px
|
||||
else { --y; py -= two_rx_sq; p += ry_sq + px - py }
|
||||
else { y -= 1; py -= two_rx_sq; p += ry_sq + px - py }
|
||||
}
|
||||
p = ry_sq*(x+.5)*(x+.5) + rx_sq*(y-1)*(y-1) - rx_sq*ry_sq
|
||||
while (y >= 0) {
|
||||
add_pts(x, y)
|
||||
--y; py -= two_rx_sq
|
||||
y -= 1; py -= two_rx_sq
|
||||
if (p > 0) p += rx_sq - py
|
||||
else { ++x; px += two_ry_sq; p += rx_sq - py + px }
|
||||
else { x += 1; px += two_ry_sq; p += rx_sq - py + px }
|
||||
}
|
||||
return {type: 'points', data: points}
|
||||
}
|
||||
|
||||
var strips = []
|
||||
var rx_sq = rx * rx, ry_sq = ry * ry
|
||||
var rx_i_sq = rx_i * rx_i, ry_i_sq = ry_i * ry_i
|
||||
var dy = -ry
|
||||
var yy = 0
|
||||
var x_out = 0
|
||||
var y_screen = 0
|
||||
var x_in = 0
|
||||
var run_start = null
|
||||
var dx = 0
|
||||
var last = false
|
||||
var next_in_ring = false
|
||||
|
||||
for (var dy = -ry; dy <= ry; ++dy) {
|
||||
var yy = dy * dy
|
||||
var x_out = floor(rx * math.sqrt(1 - yy / ry_sq))
|
||||
var y_screen = cy + dy
|
||||
for (dy = -ry; dy <= ry; ++dy) {
|
||||
yy = dy * dy
|
||||
x_out = floor(rx * math.sqrt(1 - yy / ry_sq))
|
||||
y_screen = cy + dy
|
||||
|
||||
var x_in = hole ? floor(rx_i * math.sqrt(1 - yy / ry_i_sq)) : -1
|
||||
x_in = hole ? floor(rx_i * math.sqrt(1 - yy / ry_i_sq)) : -1
|
||||
|
||||
var run_start = null
|
||||
for (var dx = -x_out; dx <= x_out; ++dx) {
|
||||
run_start = null
|
||||
for (dx = -x_out; dx <= x_out; ++dx) {
|
||||
if (hole && abs(dx) <= x_in) { run_start = null; continue }
|
||||
if (!within_wedge(dx, dy, start, end, full_circle)) { run_start = null; continue }
|
||||
if (!within_wedge(dx, dy, wedge_info)) { run_start = null; continue }
|
||||
|
||||
if (run_start == null) run_start = cx + dx
|
||||
|
||||
var last = (dx == x_out)
|
||||
var next_in_ring =
|
||||
last = (dx == x_out)
|
||||
next_in_ring =
|
||||
!last &&
|
||||
!(hole && abs(dx+1) <= x_in) &&
|
||||
within_wedge(dx+1, dy, start, end, full_circle)
|
||||
within_wedge(dx+1, dy, wedge_info)
|
||||
|
||||
if (last || !next_in_ring) {
|
||||
push(strips, {
|
||||
@@ -134,18 +152,18 @@ rasterize.outline_rect = function outline_rect(rect, thickness) {
|
||||
}
|
||||
|
||||
rasterize.round_rect = function round_rect(rect, radius, thickness) {
|
||||
thickness = thickness || 1
|
||||
|
||||
if (thickness <= 0) {
|
||||
var _thickness = thickness || 1
|
||||
|
||||
if (_thickness <= 0) {
|
||||
return rasterize.fill_round_rect(rect, radius)
|
||||
}
|
||||
|
||||
radius = min(radius, rect.width >> 1, rect.height >> 1)
|
||||
var _radius = min(radius, rect.width >> 1, rect.height >> 1)
|
||||
|
||||
if ((thickness << 1) >= rect.width ||
|
||||
(thickness << 1) >= rect.height ||
|
||||
thickness >= radius) {
|
||||
return rasterize.fill_round_rect(rect, radius)
|
||||
if ((_thickness << 1) >= rect.width ||
|
||||
(_thickness << 1) >= rect.height ||
|
||||
_thickness >= _radius) {
|
||||
return rasterize.fill_round_rect(rect, _radius)
|
||||
}
|
||||
|
||||
var x0 = rect.x,
|
||||
@@ -153,27 +171,32 @@ rasterize.round_rect = function round_rect(rect, radius, thickness) {
|
||||
x1 = rect.x + rect.width - 1,
|
||||
y1 = rect.y + rect.height - 1
|
||||
|
||||
var cx_l = x0 + radius, cx_r = x1 - radius
|
||||
var cy_t = y0 + radius, cy_b = y1 - radius
|
||||
var r_out = radius
|
||||
var r_in = radius - thickness
|
||||
var cx_l = x0 + _radius, cx_r = x1 - _radius
|
||||
var cy_t = y0 + _radius, cy_b = y1 - _radius
|
||||
var r_out = _radius
|
||||
var r_in = _radius - _thickness
|
||||
|
||||
var rects = [
|
||||
{ x:x0 + radius, y:y0, width:rect.width - (radius << 1), height:thickness },
|
||||
{ x:x0 + radius, y:y1 - thickness + 1, width:rect.width - (radius << 1), height:thickness },
|
||||
{ x:x0, y:y0 + radius, width:thickness, height:rect.height - (radius << 1) },
|
||||
{ x:x1 - thickness + 1, y:y0 + radius, width:thickness, height:rect.height - (radius << 1) }
|
||||
{ x:x0 + _radius, y:y0, width:rect.width - (_radius << 1), height:_thickness },
|
||||
{ x:x0 + _radius, y:y1 - _thickness + 1, width:rect.width - (_radius << 1), height:_thickness },
|
||||
{ x:x0, y:y0 + _radius, width:_thickness, height:rect.height - (_radius << 1) },
|
||||
{ x:x1 - _thickness + 1, y:y0 + _radius, width:_thickness, height:rect.height - (_radius << 1) }
|
||||
]
|
||||
|
||||
var strips = []
|
||||
var dy = 0
|
||||
var dy_sq = 0
|
||||
var dx_out = 0
|
||||
var dx_in = 0
|
||||
var w = 0
|
||||
|
||||
for (var dy = 0; dy < radius; ++dy) {
|
||||
var dy_sq = dy * dy
|
||||
var dx_out = floor(math.sqrt(r_out * r_out - dy_sq))
|
||||
var dx_in = (r_in > 0 && dy < r_in)
|
||||
? floor(math.sqrt(r_in * r_in - dy_sq))
|
||||
: -1
|
||||
var w = dx_out - dx_in
|
||||
for (dy = 0; dy < _radius; ++dy) {
|
||||
dy_sq = dy * dy
|
||||
dx_out = floor(math.sqrt(r_out * r_out - dy_sq))
|
||||
dx_in = (r_in > 0 && dy < r_in)
|
||||
? floor(math.sqrt(r_in * r_in - dy_sq))
|
||||
: -1
|
||||
w = dx_out - dx_in
|
||||
if (w <= 0) continue
|
||||
|
||||
push(strips,
|
||||
@@ -188,7 +211,7 @@ rasterize.round_rect = function round_rect(rect, radius, thickness) {
|
||||
}
|
||||
|
||||
rasterize.fill_round_rect = function fill_round_rect(rect, radius) {
|
||||
radius = min(radius, rect.width >> 1, rect.height >> 1)
|
||||
var _radius = min(radius, rect.width >> 1, rect.height >> 1)
|
||||
|
||||
var x0 = rect.x,
|
||||
y0 = rect.y,
|
||||
@@ -196,20 +219,23 @@ rasterize.fill_round_rect = function fill_round_rect(rect, radius) {
|
||||
y1 = rect.y + rect.height - 1
|
||||
|
||||
var rects = [
|
||||
{ x:x0 + radius, y:y0, width:rect.width - (radius << 1), height:rect.height },
|
||||
{ x:x0, y:y0 + radius, width:radius, height:rect.height - (radius << 1) },
|
||||
{ x:x1 - radius + 1, y:y0 + radius, width:radius, height:rect.height - (radius << 1) }
|
||||
{ x:x0 + _radius, y:y0, width:rect.width - (_radius << 1), height:rect.height },
|
||||
{ x:x0, y:y0 + _radius, width:_radius, height:rect.height - (_radius << 1) },
|
||||
{ x:x1 - _radius + 1, y:y0 + _radius, width:_radius, height:rect.height - (_radius << 1) }
|
||||
]
|
||||
|
||||
var cx_l = x0 + radius, cx_r = x1 - radius
|
||||
var cy_t = y0 + radius, cy_b = y1 - radius
|
||||
var cx_l = x0 + _radius, cx_r = x1 - _radius
|
||||
var cy_t = y0 + _radius, cy_b = y1 - _radius
|
||||
var caps = []
|
||||
var dy = 0
|
||||
var dx = 0
|
||||
var w = 0
|
||||
|
||||
for (var dy = 0; dy < radius; ++dy) {
|
||||
var dx = floor(math.sqrt(radius * radius - dy * dy))
|
||||
var w = (dx << 1) + 1
|
||||
for (dy = 0; dy < _radius; ++dy) {
|
||||
dx = floor(math.sqrt(_radius * _radius - dy * dy))
|
||||
w = (dx << 1) + 1
|
||||
|
||||
push(caps,
|
||||
push(caps,
|
||||
{ x:cx_l - dx, y:cy_t - dy, width:w, height:1 },
|
||||
{ x:cx_r - dx, y:cy_t - dy, width:w, height:1 },
|
||||
{ x:cx_l - dx, y:cy_b + dy, width:w, height:1 },
|
||||
|
||||
Reference in New Issue
Block a user