def plot_field(function): '''Return a Gnuplot command to plot a field.''' s = "plot '-' with vectors head\n" separation = WORLDSIZE / SAMPLES end = WORLDSIZE / 2 - separation / 2 start = -end points = ((x, y) for x in linspace(start, end, SAMPLES) for y in linspace(start, end, SAMPLES)) for x, y in points: tank = Answer() tank.index = 0 tank.x = x tank.y = y tank.angle = 0 tank.flag = '-' vec = function(tank) plotvalues = gpi_point(x, y, vec[0], vec[1]) if plotvalues is not None: x1, y1, x2, y2 = plotvalues s += '%s %s %s %s\n' % (x1, y1, x2, y2) s += 'e\n' return s
def show_arrows(plot, potential_func, obstacles, xlim=(-400, 400), ylim=(-400, 400), res=20): """ Arguments: fns: a list of potential field functions xlim, ylim: the limits of the plot res: resolution for (spacing between) arrows """ plot.set_xlim(xlim) plot.set_ylim(ylim) for x in range(xlim[0], xlim[1] + res, res): for y in range(ylim[0], ylim[1] + res, res): tank = Answer() tank.x = x tank.y = y tank.index = 1 tank.goal = "-" dx, dy = potential_func(tank, obstacles) if dx + dy == 0: continue plot.arrow(x, y, dx, dy, head_width=res/7.0, color='red', linewidth=.3)