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
*/
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});
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]);
}
}
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});
}
return pixels;