Skip to content

Instantly share code, notes, and snippets.

@celestialphineas
Created October 3, 2017 13:00
Show Gist options
  • Save celestialphineas/d5f633859d40f16a5801864ab6e85fd1 to your computer and use it in GitHub Desktop.
Save celestialphineas/d5f633859d40f16a5801864ab6e85fd1 to your computer and use it in GitHub Desktop.
Draw a circle using Bresenham’s algorithm
r = 10;
it[{x_, y_}] :=
If[2 (r^2 - x^2 - y^2) - 4 x + 2 y - 3 > 0, {x + 1, y}, {x + 1,
y - 1}];
oct = NestWhileList[it, {0, r}, #[[1]] <= #[[2]] &][[1 ;; -2]];
showMatPlot[points_List] :=
MatrixPlot@SparseArray[# + {r + 1, r + 1} -> 1 & /@ points]
showMatPlot[
Join[oct, # /. {{x_, y_} -> {y, x}} & /@
oct, # /. {{x_, y_} -> {x, -y}} & /@
oct, # /. {{x_, y_} -> {-y, x}} & /@ oct,
-oct, -# /. {{x_, y_} -> {y, x}} & /@
oct, # /. {{x_, y_} -> {-x, y}} & /@
oct, # /. {{x_, y_} -> {y, -x}} & /@ oct]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment