diff --git a/lindenmayer.py b/lindenmayer.py index 8567190..a91be96 100644 --- a/lindenmayer.py +++ b/lindenmayer.py @@ -138,29 +138,6 @@ class DumpTurtleLSystem(TurtleLSystem): return dump -def plot_segments( segments ): - - import matplotlib.pyplot as plot - from matplotlib.path import Path - import matplotlib.patches as patches - - fig = plot.figure() - ax = fig.add_subplot(111) - - for segment in segments: - start,end = segment - verts = [start,end,(0,0)] - codes = [Path.MOVETO,Path.LINETO,Path.STOP] - path = Path(verts, codes) - patch = patches.PathPatch(path, facecolor='none', lw=1) - ax.add_patch(patch) - - ax.set_xlim(-50,50) - ax.set_ylim(-50,50) - - plot.show() - - if __name__=="__main__": import sys diff --git a/uberplot.py b/uberplot.py new file mode 100644 index 0000000..95d82e0 --- /dev/null +++ b/uberplot.py @@ -0,0 +1,55 @@ + +import sys +import argparse + +import matplotlib.pyplot as plot +from matplotlib.path import Path +import matplotlib.patches as patches + + +def parse_segments( filename ): + segments = [] + with open( filename ) as fd: + for line in fd: + edge = [ float(i) for i in line.split() ] + if len(edge) == 4: + start = (edge[0],edge[1]) + end = (edge[2],edge[3]) + segments.append( (start,end) ) + return segments + + +def plot_segments( ax, segments, **kwargs ): + for start,end in segments: + verts = [start,end,(0,0)] + codes = [Path.MOVETO,Path.LINETO,Path.STOP] + path = Path(verts, codes) + patch = patches.PathPatch(path, facecolor='none', **kwargs ) + ax.add_patch(patch) + + +def scatter_segments( ax, segments, color="black", alpha=1.0, linewidth=1.0 ): + xy = [ ((i[0],j[0]),(i[1],j[1])) for (i,j) in segments] + x = [i[0] for i in xy] + y = [i[1] for i in xy] + ax.scatter( x,y, s=20, marker='o', color=color, alpha=alpha, linewidth=linewidth) + + +if __name__=="__main__": + + parser = argparse.ArgumentParser() + + parser.add_argument('-s', "--segments", default=[None], action='store', type=str, nargs='*') + + args = parser.parse_args() + + fig = plot.figure() + ax = fig.add_subplot(111) + + if args.segments != [None]: + for filename in args.segments: + seg = parse_segments(filename) + scatter_segments( ax, seg, edgecolor="red" ) + plot_segments( ax, seg, edgecolor="blue" ) + + plot.show()