def extents(vertices): min_ = vec2(*vertices[0][1:]) max_ = vec2(*vertices[0][1:]) for id_, x, y in vertices: if x < min_.x: min_.x = x if x > max_.x: max_.x = x if y < min_.y: min_.y = y if y > max_.y: max_.y = y size = max_ - min_ return ui.rect(min_.x, min_.y, size.x, size.y)
def world_extents(self): if not hasattr(self, '_world_extents'): id_, x, y = self.vertices.flat_list[0] min_ = vec2(x, y) max_ = vec2(x, y) for id_, x, y in self.vertices.flat_list: if x < min_.x: min_.x = x if x > max_.x: max_.x = x if y < min_.y: min_.y = y if y > max_.y: max_.y = y self._world_extents = min_, max_ return self._world_extents
def frontside(strip, direction): behind = mid_coord - direction * strip_breadth * 2 backyard_eaters = [vec2(0, 0), vec2(0, 0)] backyard_eaters[0][vertical] = behind backyard_eaters[0][not vertical] = strip[0][not vertical] backyard_eaters[1][vertical] = behind backyard_eaters[1][not vertical] = strip[-1][not vertical] ret = convex_hull(strip + backyard_eaters) for bogus in backyard_eaters: try: ret.remove(bogus) except ValueError: # Special case: we had only one vertex, or several aligned # vertices. assert len(ret) == 1 # convex_hull() messes with the ordering of our lists! # I need to re-sort here :/ ret.sort(key=(lambda v: v[not vertical])) return ret
def load_network(filename): d = pickle.load(file(filename)) vertices = dict((id, vec2(*coords)) for id, coords in d['vertices'].iteritems()) edges = [(vertices[a], vertices[b]) for a, b in d['edges']] return app_state(vertices[d['start']], vertices[d['goal']], vertices.values(), edges)
def layout_children(self): "Fit graph to screen, with some margin." self.remove_closest_display() window_rect = self.absolute_rect = self.find_absolute_rect() min_, max_ = self.world_extents() range_ = max_ - min_ margin = .02 # at each border ww = window_rect.width * (1 - margin * 2) wh = window_rect.height * (1 - margin * 2) # Aspect is larger if something is wider in relation to its height. world_aspect = range_.x / range_.y screen_aspect = ww / wh if world_aspect < screen_aspect: # We are limited by vertical height (most common case). pixels_per_world_unit = wh / range_.y else: pixels_per_world_unit = ww / range_.x world_center = vec2((min_.x + max_.x) / 2, (min_.y + max_.y) / 2) screen_size_in_world_units = ( vec2(window_rect.width, window_rect.height) / pixels_per_world_unit) wleft, wbottom = (world_center - screen_size_in_world_units / 2) wwidth, wheight = screen_size_in_world_units self.world_rect = self.zoom_rect = ui.rect( wleft, wbottom, wwidth, wheight)
def on_mouse_release(x, y, button, *etc): ch.send(obj(type='release', pos=vec2(x, y), button=button))
def on_mouse_drag(x, y, *etc): aps.mouse_pos = vec2(x, y) ch.send(obj(type='motion', pos=vec2(x, y)))
def on_mouse_press(x, y, button, *etc): ch.send(obj(type='press', pos=vec2(x, y), button=button))
w = pyglet.window.Window() def squaredist(v1, v2): dx = v1.x - v2.x dy = v1.y - v2.y return dx * dx + dy * dy class edge: def __init__(self, endpoint1, endpoint2): self.endpoints = endpoint1, endpoint2 start_poly = [vec2(x, y) for x, y in (-5, -8), (8, 0), (-5, 8)] goal_poly = [vec2(x, y) for x, y in (-4, -8), (4, -8), (4, 8), (-4, 8)] # App state. def app_state(start, goal, vertices=None, edges=None): if vertices is None: vertices = [start, goal] if edges is None: edges = [] return obj(start=start, goal=goal, vertices=vertices, edges=edges, mouse_pos=None,
def get_center(self): return vec2(self.left + self.width / 2, self.bottom + self.height / 2)