def test_get_theta(): v = Vector(30, 0) assert math.isclose(v.theta, 0.0) v = Vector(30, 30) assert math.isclose(v.theta, math.pi / 4.0) v = Vector(-30, 0) assert math.isclose(v.theta, math.pi)
def handle_searching(self, dt): block = self.target_block if block is None or block.is_broken(): block = self.best_block() if block is None: # move randomly self.move() return self.block = block index = block.index p, q = index if not self.in_range(p, q): # move towards the block # target_index should be the position right before the block, # not the block's index. spos = Vector(p, q) dpos = Vector(*self.index) angle = spos.angle(dpos) index = next_cell(p, q, angle) self.target_index = index self.move() else: self.mine() return
def giterate(self, fn): ''' Iterate over the generated vectors This method is simple, but the generated vector order is really inefficient. A simplifier should improve the result :type fn: func(srcx, srcy, dstx, dsty) :param fn: function to call on each iteration ''' # # for each 2 adjacent vectors # for i in range(len(self._points)): s = self._points[i] d = self._points[(i + 1) % len(self._points)] self._l.debug('main vectors: %s' % Vector(s, d)) sxst = s.x / self._npoints # source x step syst = s.y / self._npoints # source y step dxst = d.x / self._npoints # dest x step dyst = d.y / self._npoints # dest y step # # generate each internal vector # for p in range(1, self._npoints): sp = p # current point in source dp = self._npoints - p # current points in dest sx = sxst * sp sy = syst * sp dx = dxst * dp dy = dyst * dp s = Point(sx, sy) d = Point(dx, dy) fn(Vector(s, d))
def find_ship_orientation(self): #head left, back, right #Returns the unit direction that the ship is oriented ship_facing_vector = Vector(self.point_list[0]) - Vector( self.point_list[2]) ship_facing_vector._normalize() return ship_facing_vector
def handle_moving(self, dt): i, j = self.index if self.target_index is None: # if there is no target block, move randomly self.target_index = i + randrange(-1, 2), j + randrange(-1, 2) if not self.in_range(*self.target_index): # if it's not in range, move closer. spos = Vector(*self.index) dpos = Vector(*self.target_index) angle = spos.angle(dpos) index = next_cell(*self.index, angle) will_reach = False else: index = self.target_index will_reach = True if self.board.block_exists(*index, self.level): self.block = self.board.get_block(*index, self.level) self.mine() return cont = self.stumble(*index, dt) if not cont: self.move_time = 0. if will_reach: self.target_index = None self.stop() return
def test_scalar_mult(): x = Vector(1, 2) y1 = 2*x y2 = x*2 z = Vector(2, 4) nt.assert_equals(y1, z) nt.assert_equals(y2, z)
def loop_position(self, loop_offset): ''' This method calculates the center point of the ship by finding the midpoint between the head of the ship and the back indent of the ship Since the self.point_list is all tuples and not vectors (because the pygame.polygon method needs just tuples), we have to first convert them all to vectors ''' vector_list = [] for point_pair in self.point_list: vector_list.append(Vector(point_pair)) center_vector = vector_list[0] - vector_list[2] center_vector.x, center_vector.y = center_vector.x / 2, center_vector.y / 2 midpoint = vector_list[0] - center_vector #print(midpoint) loop = False if midpoint.x >= GAME_WIDTH + loop_offset: # Works self.location = Vector((loop_offset * -1, self.location.y)) loop = True elif midpoint.x <= loop_offset * -1: self.location = Vector((GAME_WIDTH, self.location.y)) loop = True elif midpoint.y <= loop_offset * -1: self.location = Vector((self.location.x, GAME_HEIGHT)) loop = True elif midpoint.y >= GAME_HEIGHT + loop_offset: # Works self.location = Vector((self.location.x, loop_offset * -1)) loop = True if loop: self.calculate_vertices()
def getFaceNormal(facepath): verts = getWindingOrder(facepath, False) if len(verts) < 3: return (1, 0, 0 ) #if there are less than 3 verts we have no uv area so bail #get edge vectors and cross them to get the uv face normal vertAPos = Vector( cmd.xform(verts[0], query=True, worldSpace=True, absolute=True, translation=True)) vertBPos = Vector( cmd.xform(verts[1], query=True, worldSpace=True, absolute=True, translation=True)) vertCPos = Vector( cmd.xform(verts[2], query=True, worldSpace=True, absolute=True, translation=True)) vecAB = vertBPos - vertAPos vecBC = vertCPos - vertBPos faceNormal = (vecAB ^ vecBC).normalize() return faceNormal
def drawScreenGrid(self, e1, e2, start, range1=[-25, 25], range2=[-25, 25], color="#d0d0d0", width=None): if not width: width = self.LINE_WIDTH / 3 start = Vector(start) e1 = Vector(e1) - start e2 = Vector(e2) - start for i in range(*range1): s = start + i * e2 self.drawScreenLine(s, s + e1, color=color, width=width if i != 0 else width * 3) for i in range(*range2): s = start + i * e1 self.drawScreenLine(s, s + e2, color=color, width=width if i != 0 else width * 3)
def Bathroom_Window(input): a = json.loads(input) v_acc = Vector(a["x"], a["y"], a["z"]) v_earth = Vector(0, 0, -1) angle = v_acc.angle(v_earth) log.debug("Bathroom Window>input(%s) =>output(%f)" % (input, angle)) return angle
def test_modulo(): v = Vector(3, 4) assert v.mod == 5.0 v = Vector(30, 0) assert v.mod == 30.0 v = Vector(0, 30) assert v.mod == 30.0
def generate(): a = Vector(int(x1.get()), int( y1.get())) # Vector A is sampled from left column of inputs b = Vector(int(x2.get()), int( y2.get())) # Vector B is sampled from right column of inputs #print(a, b) c = Convex_Combination(a, b) C = c.randomVectorSample(4) try: plt.clf() except: pass for x in C: x.plot() a.plot(colour="b") b.plot(colour="b") plt.plot([a.x, b.x], [a.y, b.y], "b--") plt.title("Result") plt.show()
def fn(x): vec = x if type(x) == Vector else Vector({x: 1}) for op in reversed(args): ans = Vector(printer=vec.printer) for mu in vec: ans += vec[mu] * op(mu) vec = ans return vec
def set_vector(self, x1, y1) : self.pos = Vector(self.x, self.y) self.vel = Vector(0, 0) self.acc = Vector(0, 0) self.target = Vector(x1, y1) self.max_speed = 15 self.max_force = 8
def test_GQ_pieri_slow(): # noqa for mu in Partition.all(10, strict=True): for p in [1, 2, 3]: ans = Vector() for nu, tabs in Tableau.KLG_by_shape(p, mu).items(): ans += Vector( {nu: utils.beta**(sum(nu) - sum(mu) - p) * len(tabs)}) f = utils.GQ(len(mu) + 1, mu) * utils.GQ(len(mu) + 1, (p, )) assert ans == utils.GQ_expansion(f)
def Bathroom_Heating(input): a = json.loads(input) v_acc = Vector(a["x"], a["y"], a["z"]) v_closed = Vector(0.213, -0.998, -0.166) #zero reference angle = safe_angle(v_acc, v_closed) if (type(angle) == float): #angle = v_acc.angle(v_closed) log.debug("Bathroom Heating>input(%s) =>output(%f)" % (input, angle)) return angle
def op(mu): for j in range(len(mu)): if mu[j] == i - 1: nu = mu[:j] + (i, ) + mu[j + 1:] return Vector({nu: 1}) if i == 1: nu = mu + (1, ) return Vector({nu: 1}) return Vector()
def GP_expansion(cls, f): # noqa if f: t = max(f.lowest_degree_terms()) n = t.n c = f[t] mu = t.index() ans = cls.GP_expansion(f - c * cls.stable_grothendieck_p(n, mu)) return ans + Vector({mu: c}) else: return Vector()
def P_expansion(cls, f): # noqa if f: t = max(f.lowest_degree_terms()) n = t.n c = f[t] mu = t.index() ans = cls.P_expansion(f - c * cls.schur_p(n, mu)) return ans + Vector({mu: c}) else: return Vector()
def __init__(self): middle_x = GAME_WIDTH / 2 middle_y = GAME_HEIGHT / 2 self.location = Vector((middle_x, middle_y)) self.momentum_direction = Vector((0.0, 0.0)) self.ship_direction = Vector((0.0, -1.0)) # potentially useless self.acceleration = Vector((0.0, 0.0)) self.point_list = [] self.calculate_vertices() self.crashed = False
def __init__(self, x=WIDTH/2, y=HEIGHT/2): self.x = x self.y = y self.width = GRID - 2 self.vel = GRID self.dir = Vector(self.vel, 0, 0) self.body = [Vector(self.x, self.y, 0), Vector(self.x + GRID, self.y, 0), Vector(self.x + GRID * 2, self.y, 0)] self.dirs = {"left": False, "right": True, "up": False, "down": False}
def __init__(self, *args, **kwargs): super(Asteroid, self).__init__(*args, **kwargs) OrientationMixin.__init__(self, *args, **kwargs) self.mass = kwargs.get('mass') self.current_vector = kwargs.get('vector', Vector(0, 0, 0)) # TODO(Austin) - Density of materials Determines size according to mass self.height = self.mass/250 * normalvariate(1, 0.2) self.width = self.mass/250 * normalvariate(1, 0.2) self.depth = self.mass/250 * normalvariate(1, 0.2) self.current_acceleration = Vector(0, 0, 0)
def schur_expansion(cls, f): if f: t = max(f) n = t.n c = f[t] mu = t.index() ans = cls.schur_expansion(f - c * cls.schur(n, mu)) return ans + Vector({mu: c}) else: return Vector()
def __init__(self, x, y, x0, y0): self.pos = Vector(x, y) self.vel = Vector(0, 0) self.acc = Vector(0, 0) self.target = Vector(x0, y0) self.r = 4 # self.color = (255,255,255) self.color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) self.max_speed = 10 self.max_force = 1
def op(mu): shape = {(a + 1, a + b) for a in range(len(mu)) for b in range(1, mu[a] + 1)} cells = {(a, b) for (a, b) in shape if abs(a - b) == abs(i)} if len(cells) == 0: return Vector() a, b = max(cells) if (a + 1, b) in shape or (a, b + 1) in shape: return Vector() shape -= {(a, b)} return Vector({symmetric_undouble(shape): 1})
def loop_position(self, loop_offset=3): if self.location.x >= GAME_WIDTH + loop_offset: # Works self.location = Vector((loop_offset * -1, self.location.y)) loop = True elif self.location.x <= loop_offset * -1: self.location = Vector((GAME_WIDTH, self.location.y)) loop = True elif self.location.y <= loop_offset * -1: self.location = Vector((self.location.x, GAME_HEIGHT)) loop = True elif self.location.y >= GAME_HEIGHT + loop_offset: # Works self.location = Vector((self.location.x, loop_offset * -1))
def gp_expansion(cls, f): # noqa if f: t = max(f.highest_degree_terms()) n = t.n c = f[t] mu = t.index() g = cls.dual_stable_grothendieck_p(n, mu) assert g[t] == 1 ans = cls.gp_expansion(f - c * g) return ans + Vector({mu: c}) else: return Vector()
def clear(self): self.straight_pairs = [] self.interpolated_pairs = [] self.translate = Vector(0, 0, 0) self._axis = Vector(0, 0, 0) self._angle = 1 self.extrude = 2 self.color = False self.fixed = False self.joint = False self.showAxis = False self.id = ''
def op(vector): ans = 0 for mu, coefficient in vector.items(): row = Partition.find_shifted_inner_corner(mu, abs(index)) if row is not None: ans += Vector({mu: beta * coefficient}) continue row = Partition.find_shifted_outer_corner(mu, abs(index)) if row is not None: nu = Partition.add(mu, row) ans += Vector({nu: coefficient}) return ans
def findNearestRoofPoint(polygon,a,b):#return min_distance.index(min(min_distance)) - Finds the point on a polygon which is closest to a line segment given by two points Va=Vector(a[0],a[1],0) Vb=Vector(b[0],b[1],0) Vab=Vb.sum(Va.multiply(-1)) min_distance=[] for point in polygon: p=Vector(point[0],point[1],0) ap=p.sum(Va.multiply(-1)) min_distance.append((Vab.cross(ap).magnitude())/Vab.magnitude()) #print min(min_distance), "in roof point" return min_distance.index(min(min_distance))