Instantly share code, notes, and snippets.

# anonymous/radial_gray_code_image.nb Created Feb 6, 2013

Short Mathematica code to render an arbitrary-sized radial gray code image (see http://imgur.com/rIgacv9 for example)
 (* 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 *)