From e43227d5dfd87e5527483fa2ba2adbdbc7ea489e Mon Sep 17 00:00:00 2001 From: creme332 <65414576+creme332@users.noreply.github.com> Date: Tue, 11 Jun 2024 13:20:00 +0400 Subject: [PATCH] fix bug in drawCircle, rewrite plotCirclePoints with correct naming of variables --- .../creme332/algorithms/CircleAlgorithm.java | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) 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) {