Example #1
0
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
Example #2
0
    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
Example #3
0
    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 ang(v1, v2):  #返回v1至v2旋转的角度,正为顺时针,负为逆时针
    v = Vector(0, 1, 0)
    x = v.angle(v1)
    y = v.angle(v2)
    a = v1.angle(v2)
    if min(y - x, y + 360 - x) > 0:
        return a
    else:
        return -a
Example #5
0
    def stumble(self, i, j, dt):
        abs_pos = self.board.get_abs_pos
        k, m = self.index

        # check if it is a valid position
        if not 0 <= i < self.board_width:
            return False

        if not 0 <= j < self.board_height:
            return False

        if not self.board.get_block(i, j, self.level).is_broken():
            return False

        self.move_time += dt

        x, y = self.pos
        cpos = Vector(x, y)  # current pos
        spos = Vector(*abs_pos(k * size, m * size))  # start position
        dpos = Vector(*abs_pos(i * size, j * size))  # destination pos
        p, q = dpos

        total_dist = distance(spos, dpos)
        angle = spos.angle(dpos)

        required_dist = (self.move_time * total_dist) / self.MOVE_TIME
        move = required_dist - distance(cpos, spos)

        mpos = Vector(*from_polar(move, angle))
        cpos += mpos
        x, y = cpos
        self.sprite.update(x, y)
        self.pos = x, y

        # how to tell if the destination has been reached?
        # if ((j > 0 and y >= q) or (j < 0 and y <= q) or (not j))
        x_satisfied = (i > k and x >= p) or (i < k and x <= p) or i == k
        y_satisfied = (j > m and y >= q) or (j < m and y <= q) or j == m
        if x_satisfied and y_satisfied:
            self.index = i, j
            self.pos = p, q
            self.sprite.update(p, q)
            return False
        return True
Example #6
0
import numpy as np
from vectors import Point, Vector
import functools

v1 = Vector(1, 2, 3)
v2 = Vector(10, 20, 30)
print(v1.add(10))
print(v1.sum(v2))  # displays <1 22 33>

print(v1.magnitude())

#We can multiply a vector by a real number.

print(v2.multiply(4))  #=> Vector(4.0, 8.0, 12.0)
print(v1.dot(v2))
print(v1.dot(v2, 180))
print(v1.cross(v2))

print(v1.angle(v2))

print(v1.parallel(v2))
print(v1.perpendicular(v2))
#print(v1.non_parallel(v2))