Rewrites Circle drawing tool to behave better with large pen sizes.

This commit is contained in:
Smie 2016-11-26 19:16:46 -07:00 committed by Julian Descottes
parent c0182952c9
commit 4706587abb

View File

@ -20,35 +20,45 @@
* @override * @override
*/ */
ns.Circle.prototype.draw = function (col, row, color, targetFrame, penSize) { ns.Circle.prototype.draw = function (col, row, color, targetFrame, penSize) {
var circlePixels = this.getCirclePixels_(this.startCol, this.startRow, col, row); this.getCirclePixels_(this.startCol, this.startRow, col, row, penSize).forEach(function (point) {
pskl.PixelUtils.resizePixels(circlePixels, penSize).forEach(function (point) {
targetFrame.setPixel(point[0], point[1], color); targetFrame.setPixel(point[0], point[1], color);
}); });
}; };
ns.Circle.prototype.getCirclePixels_ = function (x0, y0, x1, y1) { ns.Circle.prototype.getCirclePixels_ = function (x0, y0, x1, y1, penSize) {
var coords = pskl.PixelUtils.getOrderedRectangleCoordinates(x0, y0, x1, y1); var coords = pskl.PixelUtils.getOrderedRectangleCoordinates(x0, y0, x1, y1);
var xC = (coords.x0 + coords.x1) / 2; var xC = Math.round((coords.x0 + coords.x1) / 2);
var yC = (coords.y0 + coords.y1) / 2; var yC = Math.round((coords.y0 + coords.y1) / 2);
var rX = coords.x1 - xC; var rX = coords.x1 - xC;
var rY = coords.y1 - yC; var rY = coords.y1 - yC;
var iX = rX - penSize;
var iY = rY - penSize;
if (iX < 0) {
iX = 0;
}
if (iY < 0) {
iY = 0;
}
var pixels = []; var pixels = [];
var x, y, angle; var x, y, angle, r;
for (x = coords.x0 ; x < coords.x1 ; x++) { for (x = 0 ; x <= rX ; x++) {
angle = Math.acos((x - xC) / rX); for (y = 0 ; y <= rY ; y++) {
y = Math.round(rY * Math.sin(angle) + yC); angle = Math.atan(y / x);
pixels.push({'col': x, 'row': y}); r = Math.sqrt(x * x + y * y);
pixels.push({'col': 2 * xC - x, 'row': 2 * yC - y}); if ((rX <= penSize || rY <= penSize ||
} r > iX * iY / Math.sqrt(iY * iY * Math.pow(Math.cos(angle), 2) + iX * iX * Math.pow(Math.sin(angle), 2)) +
0.5) &&
for (y = coords.y0 ; y < coords.y1 ; y++) { r < rX * rY / Math.sqrt(rY * rY * Math.pow(Math.cos(angle), 2) + rX * rX * Math.pow(Math.sin(angle), 2)) +
angle = Math.asin((y - yC) / rY); 0.5) {
x = Math.round(rX * Math.cos(angle) + xC); pixels.push([xC + x, yC + y]);
pixels.push({'col': x, 'row': y}); pixels.push([xC - x, yC + y]);
pixels.push({'col': 2 * xC - x, 'row': 2 * yC - y}); pixels.push([xC + x, yC - y]);
pixels.push([xC - x, yC - y]);
}
}
} }
return pixels; return pixels;