From 4706587abb57e5751ce6040b4fecb2041ff92356 Mon Sep 17 00:00:00 2001 From: Smie Date: Sat, 26 Nov 2016 19:16:46 -0700 Subject: [PATCH] Rewrites Circle drawing tool to behave better with large pen sizes. --- src/js/tools/drawing/Circle.js | 46 +++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/src/js/tools/drawing/Circle.js b/src/js/tools/drawing/Circle.js index 05cfb1e..787ec70 100644 --- a/src/js/tools/drawing/Circle.js +++ b/src/js/tools/drawing/Circle.js @@ -20,35 +20,45 @@ * @override */ ns.Circle.prototype.draw = function (col, row, color, targetFrame, penSize) { - var circlePixels = this.getCirclePixels_(this.startCol, this.startRow, col, row); - pskl.PixelUtils.resizePixels(circlePixels, penSize).forEach(function (point) { + this.getCirclePixels_(this.startCol, this.startRow, col, row, penSize).forEach(function (point) { 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 xC = (coords.x0 + coords.x1) / 2; - var yC = (coords.y0 + coords.y1) / 2; + var xC = Math.round((coords.x0 + coords.x1) / 2); + var yC = Math.round((coords.y0 + coords.y1) / 2); var rX = coords.x1 - xC; 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 x, y, angle; - for (x = coords.x0 ; x < coords.x1 ; x++) { - angle = Math.acos((x - xC) / rX); - y = Math.round(rY * Math.sin(angle) + yC); - pixels.push({'col': x, 'row': y}); - pixels.push({'col': 2 * xC - x, 'row': 2 * yC - y}); - } - - for (y = coords.y0 ; y < coords.y1 ; y++) { - angle = Math.asin((y - yC) / rY); - x = Math.round(rX * Math.cos(angle) + xC); - pixels.push({'col': x, 'row': y}); - pixels.push({'col': 2 * xC - x, 'row': 2 * yC - y}); + var x, y, angle, r; + for (x = 0 ; x <= rX ; x++) { + for (y = 0 ; y <= rY ; y++) { + angle = Math.atan(y / x); + r = Math.sqrt(x * x + y * 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) && + r < rX * rY / Math.sqrt(rY * rY * Math.pow(Math.cos(angle), 2) + rX * rX * Math.pow(Math.sin(angle), 2)) + + 0.5) { + pixels.push([xC + x, yC + y]); + pixels.push([xC - x, yC + y]); + pixels.push([xC + x, yC - y]); + pixels.push([xC - x, yC - y]); + } + } } return pixels;