249 lines
6.7 KiB
Plaintext
249 lines
6.7 KiB
Plaintext
/**
|
|
* Rasterization module for converting shapes to pixels/rects
|
|
* Used for software rendering of complex shapes
|
|
*/
|
|
|
|
var math = use('math')
|
|
|
|
var rasterize = {}
|
|
|
|
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 (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) {
|
|
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 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 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)
|
|
|
|
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
|
|
|
|
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 += 1; px += two_ry_sq
|
|
if (p < 0) p += ry_sq + px
|
|
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 -= 1; py -= two_rx_sq
|
|
if (p > 0) p += rx_sq - py
|
|
else { x += 1; px += two_ry_sq; p += rx_sq - py + px }
|
|
}
|
|
return {type: 'points', data: points}
|
|
}
|
|
|
|
var strips = []
|
|
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 (dy = -ry; dy <= ry; ++dy) {
|
|
yy = dy * dy
|
|
x_out = floor(rx * math.sqrt(1 - yy / ry_sq))
|
|
y_screen = cy + dy
|
|
|
|
x_in = hole ? floor(rx_i * math.sqrt(1 - yy / ry_i_sq)) : -1
|
|
|
|
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, wedge_info)) { run_start = null; continue }
|
|
|
|
if (run_start == null) run_start = cx + dx
|
|
|
|
last = (dx == x_out)
|
|
next_in_ring =
|
|
!last &&
|
|
!(hole && abs(dx+1) <= x_in) &&
|
|
within_wedge(dx+1, dy, wedge_info)
|
|
|
|
if (last || !next_in_ring) {
|
|
push(strips, {
|
|
x: run_start,
|
|
y: y_screen,
|
|
width: (cx + dx) - run_start + 1,
|
|
height: 1
|
|
})
|
|
run_start = null
|
|
}
|
|
}
|
|
}
|
|
return {type: 'rects', data: strips}
|
|
}
|
|
|
|
rasterize.circle = function circle(pos, radius, opt) {
|
|
return rasterize.ellipse(pos, [radius, radius], opt)
|
|
}
|
|
|
|
rasterize.outline_rect = function outline_rect(rect, thickness) {
|
|
if (thickness <= 0) {
|
|
return {type: 'rect', data: rect}
|
|
}
|
|
|
|
if ((thickness << 1) >= rect.width ||
|
|
(thickness << 1) >= rect.height) {
|
|
return {type: 'rect', data: rect}
|
|
}
|
|
|
|
var x0 = rect.x,
|
|
y0 = rect.y,
|
|
x1 = rect.x + rect.width,
|
|
y1 = rect.y + rect.height
|
|
|
|
return {type: 'rects', data: [
|
|
{ x:x0, y:y0, width:rect.width, height:thickness },
|
|
{ x:x0, y:y1-thickness, width:rect.width, height:thickness },
|
|
{ x:x0, y:y0+thickness, width:thickness,
|
|
height:rect.height - (thickness<<1) },
|
|
{ x:x1-thickness, y:y0+thickness, width:thickness,
|
|
height:rect.height - (thickness<<1) }
|
|
]}
|
|
}
|
|
|
|
rasterize.round_rect = function round_rect(rect, radius, thickness) {
|
|
var _thickness = thickness || 1
|
|
|
|
if (_thickness <= 0) {
|
|
return rasterize.fill_round_rect(rect, radius)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
var x0 = rect.x,
|
|
y0 = rect.y,
|
|
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 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) }
|
|
]
|
|
|
|
var strips = []
|
|
var dy = 0
|
|
var dy_sq = 0
|
|
var dx_out = 0
|
|
var dx_in = 0
|
|
var w = 0
|
|
|
|
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,
|
|
{ x:cx_l - dx_out, y:cy_t - dy, width:w, height:1 },
|
|
{ x:cx_r + dx_in + 1, y:cy_t - dy, width:w, height:1 },
|
|
{ x:cx_l - dx_out, y:cy_b + dy, width:w, height:1 },
|
|
{ x:cx_r + dx_in + 1, y:cy_b + dy, width:w, height:1 }
|
|
)
|
|
}
|
|
|
|
return {type: 'rects', data: array(rects, strips)}
|
|
}
|
|
|
|
rasterize.fill_round_rect = function fill_round_rect(rect, radius) {
|
|
var _radius = min(radius, rect.width >> 1, rect.height >> 1)
|
|
|
|
var x0 = rect.x,
|
|
y0 = rect.y,
|
|
x1 = rect.x + rect.width - 1,
|
|
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) }
|
|
]
|
|
|
|
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 (dy = 0; dy < _radius; ++dy) {
|
|
dx = floor(math.sqrt(_radius * _radius - dy * dy))
|
|
w = (dx << 1) + 1
|
|
|
|
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 },
|
|
{ x:cx_r - dx, y:cy_b + dy, width:w, height:1 }
|
|
)
|
|
}
|
|
|
|
return {type: 'rects', data: array(rects, caps)}
|
|
}
|
|
|
|
return rasterize |