Skip to content

Commit

Permalink
fix bug in drawCircle, rewrite plotCirclePoints with correct naming o…
Browse files Browse the repository at this point in the history
…f variables
  • Loading branch information
creme332 committed Jun 11, 2024
1 parent 4276399 commit e43227d
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/main/java/com/github/creme332/algorithms/CircleAlgorithm.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
public class CircleAlgorithm {

private CircleAlgorithm() {

}

public static int[][] drawCircle(int centerX, int centerY, int radius) {
Expand All @@ -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--;
Expand All @@ -35,29 +36,31 @@ public static int[][] drawCircle(int centerX, int centerY, int radius) {
}

private static void plotCirclePoints(List<int[]> 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);
addUniquePixel(pixelList, centerX - x, centerY - y);
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<int[]> pixelList, int x, int y) {
Expand Down

0 comments on commit e43227d

Please sign in to comment.