def pre_round(self): for pos in self.lighthouses: ##distribucion de la energia en puesto por cada faro for y in xrange(pos[1] - self.RDIST + 1, pos[1] + self.RDIST): for x in xrange(pos[0] - self.RDIST + 1, pos[0] + self.RDIST): ##USA ESTA FORMULA!!! dist = geom.dist(pos, (x, y)) delta = int(math.floor(self.RDIST - dist)) if delta > 0: self.island.energy[x, y] += delta player_posmap = dict() for player in self.players: if player.pos in player_posmap: player_posmap[player.pos].append(player) else: player_posmap[player.pos] = [player] if player.pos in self.lighthouses: player.keys.add(player.pos) for pos, players in player_posmap.iteritems(): energy = self.island.energy[pos] // len(players) for player in players: player.energy += energy self.island.energy[pos] = 0 for lh in self.lighthouses.itervalues(): lh.decay(10)
def __init__(self, island_map): self._island = island_map self.h = len(self._island) self.w = len(self._island[0]) self._energymap = [[0] * self.w for i in xrange(self.h)] self._horizonmap = [] dist = self.HORIZON for y in xrange(-dist, dist + 1): row = [] for x in xrange(-dist, dist + 1): row.append(geom.dist((0, 0), (x, y)) <= self.HORIZON) self._horizonmap.append(row) class _Energy(object): def __getitem__(unused, pos): x, y = pos if self[pos]: return self._energymap[y][x] else: return 0 def __setitem__(unused, pos, val): x, y = pos if val > self.MAX_ENERGY: val = self.MAX_ENERGY assert val >= 0 if self[pos]: self._energymap[y][x] = val self._energy = _Energy()
def get_near(self, s, distance): found = [] for b in self.boids: if b == s: continue if geom.dist(b.position, s.position) < distance: found.append(b) return found
def avoid_group(px, py, group): # returns a heading that avoids a group of objects and a distance to # the closes object res = [] fx = fy = 0.0 mind = -1 for l in group: hx = px - l.x hy = l.y - py v = Vector2(hx, hy).normalize() d = geom.dist((0,0), (hx, hy)) if d < mind or mind < 0: mind = d res.append((v.x, v.y, d)) if mind < 0: return 0, 10000000 for hx, hy, d in res: p = float(mind)/d #print "ENEMY:", d, degrees(geom.vector_angle(hx, hy)) fx += hx*p fy += hy*p #print "ESCAPE DIR", degrees(geom.vector_angle(fx, fy)), mind return degrees(geom.vector_angle(fx, fy)), mind
def __init__(self): super( TestLayer, self ).__init__() self.add_circle(center,radius) self.add_wpts(list(wps)) fn_visibles = visiblesball_factory(V2(center[0],center[1]),radius) self.wpnav = WaypointNav(wps,fn_visibles) for r,s in zip(wps,self.wpnav.points): assert(geom.dist(r,s)<1.0e-4) print 'self.wpnav.adj[0]:',self.wpnav.adj[0] self.last_i = 0 self.last_j = 0 #add all arcs wpnav = WaypointNav(wps,fn_visibles) points = wpnav.points for i,adj_i in enumerate(wpnav.adj): for j in adj_i: sprites = self.add_line(points[i],points[j]) self.add_named('L%d %d'%(i,j),sprites) self.old_start_i = -1 self.old_dest_i = -1 self.start_i = 0 self.dest_i = 1 self.colored_paths = set() self.update_best_path()
def test_point_and_dist(self): self.assertEqual("%s" % self.p1, "(0.0, 0.0)") self.assertEqual("%s" % self.p2, "(5.0, 5.0)") self.assertFalse(self.p1 == self.p2) self.assertTrue(self.p1 == self.p1) # Length should be sqrt(5^2 + 5^2) = 7.071 self.assertAlmostEqual(dist(self.p1, self.p2), 7.071, places=3) self.assertAlmostEqual(self.p1.dist(self.p2), 7.071, places=3) # Test distance between a point and a line (perpendicular dist) # Correct answer determined using AutoCAD software self.assertAlmostEqual(self.p3.dist(self.line1), 1.4142, places=4) self.assertTrue(self.p3.dist("Joe") is None)
def add_line(self,p0,p1,color=(255,255,255)): step = int(fstep*scale) x0 , y0 = p0 x1 , y1 = p1 d = float(geom.dist(p0,p1)) a = fstep sprites = [] while a<d: b = Sprite('data/npoint.png', color=color) b.x = (a*x0 + (d-a)*x1)*scale/d + offset b.y = (a*y0 + (d-a)*y1)*scale/d + offset b.scale = 1.0/16.0 self.add(b) sprites.append(b) a += fstep return sprites
def get_cut_ratios(vertices, colors, faces, connected, pattern, red_len, green_len, blue_len, min_dist): ratios = {} for (i, j) in pattern: vi, vj = vertices[i], vertices[j] d = dist(vi, vj) if colors: l = d - (colors[j][1] * (d - red_len) + colors[j][2] * (d - green_len) + colors[j][3] * (d - blue_len)) / 255 else: l = red_len for f in connected[undirected(i, j)]: a = cos_angle(vj, vi, vertices[get_opposing_vertex(faces[f], i, j)]) if a > 0: l = max(l, min_dist / sqrt(1 - a**2)) ratios[i, j] = min(1, l / d) return ratios
def pre_round(self): for pos in self.lighthouses: for y in range(pos[1] - self.RDIST + 1, pos[1] + self.RDIST): for x in range(pos[0] - self.RDIST + 1, pos[0] + self.RDIST): dist = geom.dist(pos, (x, y)) delta = int(math.floor(self.RDIST - dist)) if delta > 0: self.island.energy[x, y] += delta player_posmap = dict() for player in self.players: if player.pos in player_posmap: player_posmap[player.pos].append(player) else: player_posmap[player.pos] = [player] if player.pos in self.lighthouses: player.keys.add(player.pos) for pos, players in player_posmap.items(): energy = self.island.energy[pos] // len(players) for player in players: player.energy += energy self.island.energy[pos] = 0 for lh in self.lighthouses.values(): lh.decay(10)
def pre_round(self): for pos in self.lighthouses: for y in xrange(pos[1] - self.RDIST + 1, pos[1] + self.RDIST): for x in xrange(pos[0] - self.RDIST + 1, pos[0] + self.RDIST): dist = geom.dist(pos, (x, y)) delta = int(math.floor(self.RDIST - dist)) if delta > 0: self.island.energy[x, y] += delta player_posmap = dict() for player in self.players: if player.pos in player_posmap: player_posmap[player.pos].append(player) else: player_posmap[player.pos] = [player] if player.pos in self.lighthouses: player.keys.add(player.pos) for pos, players in player_posmap.iteritems(): energy = self.island.energy[pos] // len(players) for player in players: player.energy += energy self.island.energy[pos] = 0 for lh in self.lighthouses.itervalues(): lh.decay(10)
def spin_product(c, r_max = 10., dr = 0.4): nat = len(c.evol.geom[0].atoms) nr = int(r_max / dr) sp = [[] for _ in range(nr)] for es in c.evol.geom: n = es.filter('label','Fe') spin = es[n]['up'] - es[n]['dn'] # spin product spin2 = np.outer(spin, spin) # dist r = dist(es['crd'], es.vc, [n,n]) r2 = np.sum(r*r, axis = 1) ri = np.floor(np.sqrt(r2.reshape(len(n[0]), len(n[0]))) / dr) for i in range(len(n[0])): for j in range(len(n[0])): if ri[i,j] < nr: sp[int(ri[i,j])].append(spin2[i,j]) sp_mean = [0. for _ in sp] for i, spi in enumerate(sp): if len(spi) != 0: sp_mean[i] += sum(spi)/len(spi) return np.arange(0., r_max, dr), sp_mean
def get_matching_vertices(vertices, others): return [min((dist(vertex, other), v) for v, other in enumerate(others))[1] for vertex in vertices]
def point(ncd_name, lon, lat): ########################## # Inputs # ncd = path to netcdf file # lon = geographical longitude # lat = geographical latitude # Outputs # dq = convective heat flux [W m-2] ############################# # constants cps = 1000 # J kg-1 K-1 heat capacity of the air # read grid variables ncd = Dataset(ncd_name, 'r') rlon = ncd.variables['rlon'][:] rlat = ncd.variables['rlat'][:] vcoord = ncd.variables['vcoord'] # indexes from lon and lat [lon_r, lat_r] = geo2rot.g2r(lon, lat) idx = np.abs(rlon - (lon_r)).argmin() idy = np.abs(rlat - (lat_r)).argmin() # read variables u = ncd.variables['U'][0, -1, :, :] v = ncd.variables['V'][0, -1, :, :] t = ncd.variables['T'][0, -1, :, :] #w = ncd.variables['W'][0,-1,:,:] only horizontal tup = ncd.variables['T'][0, -2, :, :] rho = ncd.variables['RHO'][0, -1, :, :] rhoup = ncd.variables['RHO'][0, -2, :, :] # additional variables lat = ncd.variables['lat'][:, :] lon = ncd.variables['lon'][:, :] # cell size dx1 = abs( geom.dist(lon[idy, idx], lat[idy, idx], lon[idy, idx - 1], lat[idy, idx - 1])) dx2 = abs( geom.dist(lon[idy, idx], lat[idy, idx], lon[idy, idx + 1], lat[idy, idx + 1])) dx = (dx1 + dx2) / 2 dy1 = abs( geom.dist(lon[idy, idx], lat[idy, idx], lon[idy - 1, idx], lat[idy - 1, idx])) dy2 = abs( geom.dist(lon[idy, idx], lat[idy, idx], lon[idy + 1, idx], lat[idy + 1, idx])) dy = (dy1 + dy2) / 2 # convert to horizontal h = vcoord[-2] - vcoord[-1] Ax1 = dx1 * h Ax2 = dx2 * h Ay1 = dy1 * h Ay2 = dy2 * h # advective flux q_in_x = Ax1 * cps * ((rho[idy,idx]+rho[idy,idx-1])/2) * \ u[idy,idx] * ((t[idy,idx]+t[idy,idx-1])/2) q_out_x = Ax2 * cps * ((rho[idy,idx]+rho[idy,idx+1])/2) * \ u[idy,idx+1] * ((t[idy,idx]+t[idy,idx+1])/2) q_in_y = Ay1 * cps * ((rho[idy,idx]+rho[idy-1,idx])/2) * \ v[idy,idx] * ((t[idy,idx]+t[idy-1,idx])/2) q_out_y = Ay2 * cps * ((rho[idy,idx]+rho[idy+1,idx])/2) * \ v[idy+1,idx] * ((t[idy,idx]+t[idy+1,idx])/2) Dq_tot = q_in_x - q_out_x + q_in_y - q_out_y # in W Dq_flux = Dq_vol * (dx * dy ) # W / m2 as other turbulent (unresolved) fluxes # vertical component not considered #q_out_z = cps * ((rhoup[idy,idx]+rho[idy,idx])/2) * \ # w[idy+1,idx] * ((tup[idy,idx]+t[idy,idx])/2) return Dq_flux
def towards(p0, p1, d): d = min(dist(p1, p0) / 2, d) return p0 + normalize(p1 - p0) * d