diff --git a/src/main/java/com/github/creme332/algorithms/CircleAlgorithm.java b/src/main/java/com/github/creme332/algorithms/CircleAlgorithm.java index b1428a57..b277fcc0 100644 --- a/src/main/java/com/github/creme332/algorithms/CircleAlgorithm.java +++ b/src/main/java/com/github/creme332/algorithms/CircleAlgorithm.java @@ -6,6 +6,7 @@ public class CircleAlgorithm { private CircleAlgorithm() { + } public static int[][] drawCircle(int centerX, int centerY, int radius) { @@ -19,11 +20,11 @@ public static int[][] drawCircle(int centerX, int centerY, int radius) { int y = radius; int decisionParameter = 1 - radius; - while (y >= x) { + while (x <= y) { plotCirclePoints(pixelList, centerX, centerY, x, y); x++; - if (decisionParameter <= 0) { + if (decisionParameter < 0) { decisionParameter += 2 * x + 1; } else { y--; @@ -35,13 +36,15 @@ public static int[][] drawCircle(int centerX, int centerY, int radius) { } private static void plotCirclePoints(List pixelList, int centerX, int centerY, int x, int y) { - // For radius 1, only plot the points exactly at distance 1 from the center - if (x == 0 && y == 1) { + if (x == 0) { + addUniquePixel(pixelList, centerX + x, centerY + y); addUniquePixel(pixelList, centerX + y, centerY + x); + addUniquePixel(pixelList, centerX + x, centerY - y); addUniquePixel(pixelList, centerX - y, centerY + x); - addUniquePixel(pixelList, centerX + y, centerY - x); - addUniquePixel(pixelList, centerX - y, centerY - x); + return; + } + if (x == y) { addUniquePixel(pixelList, centerX + x, centerY + y); addUniquePixel(pixelList, centerX - x, centerY + y); addUniquePixel(pixelList, centerX + x, centerY - y); @@ -49,15 +52,15 @@ private static void plotCirclePoints(List pixelList, int centerX, int cen return; } - addUniquePixel(pixelList, centerX + y, centerY + x); - addUniquePixel(pixelList, centerX - y, centerY + x); - addUniquePixel(pixelList, centerX + y, centerY - x); - addUniquePixel(pixelList, centerX - y, centerY - x); - addUniquePixel(pixelList, centerX + x, centerY + y); addUniquePixel(pixelList, centerX - x, centerY + y); addUniquePixel(pixelList, centerX + x, centerY - y); addUniquePixel(pixelList, centerX - x, centerY - y); + + addUniquePixel(pixelList, centerX + y, centerY + x); + addUniquePixel(pixelList, centerX - y, centerY + x); + addUniquePixel(pixelList, centerX + y, centerY - x); + addUniquePixel(pixelList, centerX - y, centerY - x); } private static void addUniquePixel(List pixelList, int x, int y) {