Created
July 19, 2021 09:28
-
-
Save amaembo/aabca85b2c24cde7fd930fab936087e9 to your computer and use it in GitHub Desktop.
Bresenham circle algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Circle { | |
public static void main(String[] args) { | |
// Preparation | |
int radius = Integer.parseInt(args[0]); | |
int rasterSize = radius * 2 + 1; | |
boolean[][] raster = new boolean[rasterSize][rasterSize]; | |
// Bresenham algorithm | |
int y = radius; | |
int err = radius; | |
for (int x = 0; x <= y; x++) { | |
raster[radius+x][radius+y] = true; raster[radius+y][radius+x] = true; | |
raster[radius+x][radius-y] = true; raster[radius+y][radius-x] = true; | |
raster[radius-x][radius+y] = true; raster[radius-y][radius+x] = true; | |
raster[radius-x][radius-y] = true; raster[radius-y][radius-x] = true; | |
err -= 2 * x + 1; | |
if (err < 0) { | |
err += 2 * y - 1; | |
y--; | |
} | |
} | |
// Output | |
for (int i = 0; i < rasterSize; i++) { | |
for (int j = 0; j < rasterSize; j++) { | |
System.out.print(raster[i][j] ? "**" : " "); | |
} | |
System.out.println(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment