public
Created

Short Mathematica code to render an arbitrary-sized radial gray code image (see http://imgur.com/rIgacv9 for example)

  • Download Gist
radial_gray_code_image.nb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
(* create an array representing the gray code, step-by-step *)
binRevGcStep[{}] := {{0}, {1}}
binRevGcStep[gc_List] :=
Join @@ {Prepend[#, 0] & /@ gc, Prepend[#, 1] & /@ Reverse@gc} /;
Length[gc] > 1
 
 
(* take a peek at a cartesian-displayed gray code image to check the above *)
rows = 4;
gc = Nest[binRevGcStep, {}, rows] ;
Image[#, ImageSize -> Medium] &@Transpose@gc
 
 
(* Turn an array into a list of disk segment start-and-stops in radians (plus other rendering infos) *)
startsAndEnds[gc_List] :=
Block[{rows, runs, starts, ends, total},
rows = Length[gc];
runs = (Length /@ # &) /@ Split /@ gc;
starts = Prepend[Accumulate[#], 0][[;; -2]] & /@ runs;
ends = starts + runs;
total = ends[[1, -1]];
Table[
Table[
{(2 \[Pi])/total starts[[row, col]], (* start of wedge,
in radians *)
(2 \[Pi])/total ends[[row, col]], (*
end of wedge, in radians *)
GrayLevel[gc[[row, 1 + starts[[row, col]]]]/Max[gc]] , (*
wedge color *)
If[row == rows, 2 rows, row] (*
wedge outer radius (last ring extends to cover more screen) *)
\
},
{col, 1, Length[starts[[row]]]}],
{row, 1, Length[gc]}]]
 
(* take a pair res = resolution (width, height) and an array and return a radial graphic of it *)
mkG[res_, gc_List] := Block[{aspect, offset, g, rows},
aspect = res[[2]]/res[[1]];
offset = 3;
rows = Dimensions[gc][[2]];
g = Graphics[({EdgeForm[#[[3]]], #[[3]],
Disk[{0, 0}, #[[4]], {#[[1]], #[[2]]}]} & /@ #) & /@
Reverse@startsAndEnds@Transpose@gc,
PlotRange -> {{-(rows + offset),
rows + offset}, {-aspect*(rows + offset),
aspect*(rows + offset)}}];
g]
 
(* make a sized image from an array *)
mkImage[res_, gc_List] :=
Image[mkG[gc], ImageSize -> res]
 
 
(* render an image to my screen resolution *)
res = {1440, 900};
g = mkG[res, Nest[binRevGcStep, {}, 10]]
i = Image[g, ImageSize -> res]
 
(* you can right click to save the image, or use the Export function if you like *)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.