def select_hex(self, x, y, use_hex): if use_hex: self.selected_hex = hexgrid.Hex(x, y) else: self.selected_hex = hex_at(x, y) self.selection_mesh.vertices = hex_vertices(self.grid, self.selected_hex)
def pixel_to_hex(self,p): M = self.orientation size = self.size origin =self.origin pt = Point((p.x - origin.x) / size.x, (p.y - origin.y) / size.y) q = M.b0 * pt.x + M.b1 * pt.y r = M.b2 * pt.x + M.b3 * pt.y return hexgrid.Hex(q,r)
def select_adjacent(self, x, y, use_hex): if use_hex: requested_hex = hexgrid.Hex(self.selected_hex[0] + x, self.selected_hex[1] + y) square_y = requested_hex[1] if square_y < 0 or square_y >= self.height: return else: self.select_hex(requested_hex[0], requested_hex[1], True) return else: self.select_hex( max(min(self.selected_hex[0] + x, self.width - 1), 0), max(min(self.selected_hex[1] + y, self.height - 1), 0), False)
def __init__(self, width, height, rows, columns, selection_mode, selection_color, grid_color, grid_width): super(HexApp, self).__init__() self.width = width self.height = height self.rows = rows self.columns = columns self.selection_mode = selection_mode self.selection_color = selection_color self.grid_color = grid_color self.grid_width = grid_width self.selected_hex = hexgrid.Hex(0, 0) self.selection_mesh = None self.grid = None self._keyboard = Window.request_keyboard(self._keyboard_closed, None) self._keyboard.bind(on_key_down=self._on_keyboard_down)
def aStar(request): global startGu, endGu center = hexgrid.Point((float(startX) + float(endX)) / 2, (float(startY) + float(endY)) / 2) rate = 110.574 / (111.320 * math.cos(37.55582994870823 * math.pi / 180)) grid = hexgrid.Grid(hexgrid.OrientationFlat, center, Point(rate * 0.00015, 0.00015), morton.Morton(2, 32)) sPoint = grid.hex_at(Point(float(startX), float(startY))) ePoint = grid.hex_at(Point(float(endX), float(endY))) map_size = max(abs(sPoint.q), abs(sPoint.r)) road1 = Roadtohexgrid.objects.filter(is_danger=1, hexgrid_gu=startGu).all() road2 = Roadtohexgrid.objects.filter(is_danger=1, hexgrid_gu=endGu).all() total_road = road1.union(road2, all=False) wall1 = Roadtohexgrid.objects.filter(is_danger=0, hexgrid_gu=startGu).all() wall2 = Roadtohexgrid.objects.filter(is_danger=0, hexgrid_gu=endGu).all() total_wall = wall1.union(wall2, all=False) wh = GridWithWeights(layout_flat, Point(rate * 0.00015, 0.00015), center, map_size + 5) for r in total_road: gis = Geometry(r.hexgrid_loc.hex()[8:]) h = grid.hex_at(shape(gis.geojson)) wh.weights[(h.q, h.r)] = 1 for w in total_wall: gis = Geometry(w.hexgrid_loc.hex()[8:]) h = grid.hex_at(shape(gis.geojson)) wh.weights[(h.q, h.r)] = 200 start, goal = (sPoint.q, sPoint.r), (ePoint.q, ePoint.r) came_from, cost_so_far = a_star_search(wh, start, goal) pointList = reconstruct_path(came_from, start=start, goal=goal) plist = [] for p in pointList: point = wh.hex_to_pixel(hexgrid.Hex(p[0], p[1])) plist.append([point.x, point.y]) crime_location = { "type": "Feature", "geometry": { "type": "LineString", "coordinates": plist } } pistes = {"type": "FeatureCollection", "features": [crime_location]} return HttpResponse(json.dumps({'pistes': pistes}), content_type="application/json")
# Event Loop while True: clock.tick(FPS) log.debug("\nNew loop at time {}.".format(clock)) # Find out what the mouse is pointing at. # Calculate where in the world the mouse is pointing. mouse_xy_pos = pygame.mouse.get_pos() mouse_hex_pos = hexgrid.pixel_to_hex(layout, Point(*mouse_xy_pos)) mouse_hex_cubic = hexgrid.hex_round(mouse_hex_pos) mouse_hex = Hex(mouse_hex_cubic.r, mouse_hex_cubic.q) # Calculate which segment of the hex the mouse is in. off = hexgrid.Hex(mouse_hex_pos.r - mouse_hex_cubic.r, mouse_hex_pos.q - mouse_hex_cubic.q, mouse_hex_pos.s - mouse_hex_cubic.s) diff = (off.r - off.q, off.q - off.s, off.s - off.r) m = -1.0 mi = None for i in range(len(diff)): if abs(diff[i]) > m: m = abs(diff[i]) mi = i d = [0, 0, 0] sign = 1 if diff[mi] > 0 else -1 d[mi] = sign d[(mi + 1) % 3] = -sign mouse_dir = hexgrid.Hex(*d) log.debug("Mouse at {}".format(mouse_hex))
def hex_at(x, y): return hexgrid.Hex(x, y - x // 2) # // is floor division (integer division)