Skip to content

Instantly share code, notes, and snippets.

@amaembo
Created July 19, 2021 09:28
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amaembo/aabca85b2c24cde7fd930fab936087e9 to your computer and use it in GitHub Desktop.
Save amaembo/aabca85b2c24cde7fd930fab936087e9 to your computer and use it in GitHub Desktop.
Bresenham circle algorithm
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