git @ Cat's Eye Technologies ALPACA / 3697ebc
Add --display-svg flag, which outputs each frame as SVG. Chris Pressey 7 years ago
2 changed file(s) with 31 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
7272 argparser.add_argument("--display-window", metavar='RANGE', default=None,
7373 help="A string in the form '(x1,y1)-(x2-y2)'; if given, every generation "
7474 "displayed will only display the cells within this fixed window"
75 )
76 argparser.add_argument("--display-svg", action="store_true",
77 help="Display each generation as SVG"
7578 )
7679 argparser.add_argument("--write-discrete-files-to", metavar='DIRNAME', default=None,
7780 help="If given, instead of displaying each generation on standard output, "
151154
152155 def output_frame(count, pf):
153156 if options.display_window:
154 rendered = pf.to_str(display_x1, display_y1, display_x2, display_y2)
157 if options.display_svg:
158 rendered = pf.to_svg(display_x1, display_y1, display_x2, display_y2)
159 else:
160 rendered = pf.to_str(display_x1, display_y1, display_x2, display_y2)
155161 else:
156 rendered = str(pf)
162 if options.display_svg:
163 rendered = pf.to_svg(pf.min_x, pf.min_y, pf.max_x, pf.max_y)
164 else:
165 rendered = str(pf)
157166
158167 if options.write_discrete_files_to:
159168 with open(os.path.join(options.write_discrete_files_to, "%08d.txt" % count), 'w') as f:
103103
104104 def __str__(self):
105105 return self.to_str(self.min_x, self.min_y, self.max_x, self.max_y)
106
107 def to_svg(self, min_x, min_y, max_x, max_y):
108 rects = []
109 y = min_y
110 while y is not None and y <= max_y:
111 x = min_x
112 while x <= max_x:
113 if self.get(x, y) != self.default:
114 rects.append(
115 '<rect x="{}" y="{}" width="1" height="1" fill="black" />'.format(x, y)
116 )
117 x += 1
118 y += 1
119 return """\
120 <?xml version="1.0" standalone="no"?>
121 <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
122 <svg viewBox="{} {} {} {}" version="1.1">
123 <rect width="100%" height="100%" fill="white" />
124 {}
125 </svg>""".format(min_x, min_y, max_x, max_y, '\n '.join(rects))