Exemple #1
0
    def divide(self):
        self.divided = True
        cx = self.boundary.center.x
        cy = self.boundary.center.y
        w = self.boundary.w
        h = self.boundary.h

        ul_rect = Shapes.Rect(Shapes.Point(cx - w / 4., cy - h / 4.), w / 2.,
                              h / 2.)
        self.ul = QuadTree(ul_rect, self.capacity)
        ur_rect = Shapes.Rect(Shapes.Point(cx + w / 4., cy - h / 4.), w / 2.,
                              h / 2.)
        self.ur = QuadTree(ur_rect, self.capacity)
        ll_rect = Shapes.Rect(Shapes.Point(cx - w / 4., cy + h / 4.), w / 2.,
                              h / 2.)
        self.ll = QuadTree(ll_rect, self.capacity)
        lr_rect = Shapes.Rect(Shapes.Point(cx + w / 4., cy + h / 4.), w / 2.,
                              h / 2.)
        self.lr = QuadTree(lr_rect, self.capacity)

        for object in self.objects:
            self.ul.insert(object)
            self.ur.insert(object)
            self.ll.insert(object)
            self.lr.insert(object)

        self.objects = []
Exemple #2
0
def listtopoints(points):
    Pts = []
    for cs in points:
        try:
            x, y, z = cs
            Pts.append(Sh.Point(x, y, z))
        except:
            Pts.append([])
    return Pts
Exemple #3
0
    def insert(self, object):
        if self.keys != None:
            point = Shapes.Point(object.__dict__[self.keys[0]],
                                 object.__dict__[self.keys[1]])
        else:
            point = object

        if not self.boundary.pointInside(point): return

        if (len(self.objects) < self.capacity and self.divided == False):
            self.objects += [object]
        else:
            if (self.divided == False): self.divide()

            self.ul.insert(object)
            self.ur.insert(object)
            self.ll.insert(object)
            self.lr.insert(object)
Exemple #4
0
    def getLocal(self, center, radius, lpoints=[], keys=None):
        if keys == None: keys = self.keys
        to_add = []
        if not self.boundary.circleInside(center, radius): return to_add
        else:
            for object in self.objects:
                lpoint = Shapes.Point(object.__dict__[keys[0]],
                                      object.__dict__[keys[1]])
                if lpoint.dist(center) < radius and self.keys == None:
                    to_add += [object]

            if self.divided:

                to_add += self.ul.getLocal(center, radius, lpoints, keys)
                to_add += self.ur.getLocal(center, radius, lpoints, keys)
                to_add += self.ll.getLocal(center, radius, lpoints, keys)
                to_add += self.lr.getLocal(center, radius, lpoints, keys)

        return to_add
Exemple #5
0
def coll_line_line(line1, line2, eps):
    """Line-line collision detection."""

    #Get eq line1
    m1, b1, is_vert1 = line1.slope_intercept()  #pylint:disable-msg=C0103

    #Get eq line 2
    m2, b2, is_vert2 = line2.slope_intercept()  #pylint:disable-msg=C0103

    if is_vert1 and is_vert2:
        #Both vertical lines
        if abs(line1.p1.x - line2.p1.x) > eps:
            #Too far apart
            return False

        #Check for either endpoint colliding with the other line
        return (coll_line_point(line1, line2.p1, eps) or
                coll_line_point(line1, line2.p2, eps))

    elif is_vert1:
        #Can't use line 1 for the slope
        coll_x = line1.p1.x
        coll_y = m2 * coll_x + b2
    elif is_vert2:
        #Can't use line 2 for the slope
        coll_x = line2.p1.x
        coll_y = m1 * coll_x + b1
    else:
        #Both slopes ok, solve linear equation w/substitution
        coll_x = (b2 - b1) / (m1 - m2)
        coll_y = m1 * coll_x + b1
    
    #Get the supposed collision point
    coll_pt = Shapes.Point(coll_x, coll_y)

    #Check that the collision point interacts with both line segments
    return (coll_line_point(line1, coll_pt, eps) and
            coll_line_point(line2, coll_pt, eps))
Exemple #6
0
def decision(player2, qTree):
    if not player2.alive: return (None, None, None, None)
    #    print('Making Decision...')
    locals = qTree.getLocal(Shapes.Point(player2.x, player2.y), player2.rad)
    avg_x = 0
    avg_y = 0
    avg_vx = 0
    avg_vy = 0
    for local in locals:
        #pygame.draw.line(player2.canvas, (0,255,0), (player2.x, player2.y), (local.x, local.y), 4)
        avg_x += local.x
        avg_y += local.y
        avg_vx += local.vx
        avg_vy += local.vy

    if float(len(locals)) != 0:
        avg_x /= float(len(locals))
        avg_y /= float(len(locals))
        avg_vx /= float(len(locals))
        avg_vy /= float(len(locals))


#    print('Decision Made!')
    return (avg_x, avg_y, avg_vx, avg_vy)
Exemple #7
0
    def Print(self):
        print self.Lx, self.Ly, self.Rx, self.Ry


#Set-Up
RESOLUTION = (800, 600)
TARGFPS = 60

#Create Camera (very basic for now)
cam = Sh.Camera()
#Create Renderer Object: Renderer(Camera,Resolution)
rend = Rd.Renderer(cam, RESOLUTION)

if False:
    #Create 4 points: Point(x,y,z,(R,G,B))
    p1 = Sh.Point(-2, -2, 4, (0, 255, 255))
    p2 = Sh.Point(4, 0.1, 4, (255, 255, 0))
    p3 = Sh.Point(0.1, 4, 4, (255, 0, 0))
    p4 = Sh.Point(-0.1, -0.1, 0, (100, 100, 100))
    #Create a Tetrahedron
    TH1 = Sh.Tetra(p1, p2, p3, p4)
    #Create a Transformation Set
    TS1 = Tf.Transform()
    #Add several transformations to the set
    TS1.AddTransf(Tf.Rotation(130, 3))
    TS1.AddTransf(Tf.Translation(2, 2))
    TS1.AddTransf(Tf.Translation(1, 3))
    TS1.AddTransf(Tf.Translation(1, 1))
    #Create a new Tetrahedron by applying the
    #transformation set to the first tetrahedron
    TH2 = TH1.ApplyTransf(TS1)
Exemple #8
0
                to_add += self.ll.getLocal(center, radius, lpoints, keys)
                to_add += self.lr.getLocal(center, radius, lpoints, keys)

        return to_add

    def show(self, canvas):
        self.boundary.show(canvas, stroke=1)
        if (self.divided):
            self.ul.show(canvas)
            self.ur.show(canvas)
            self.ll.show(canvas)
            self.lr.show(canvas)


if __name__ == '__main__':
    p1 = Shapes.Point(400, 400)
    r1 = Shapes.Rect(p1, 800, 800)
    q1 = QuadTree(r1, 4)
    rad = 50

    pygame.init()
    gameDisplay = pygame.display.set_mode((800, 800))
    gameDisplay.fill((255, 255, 255))
    clock = pygame.time.Clock()

    points = [Shapes.Point(rx=800, ry=800, rvx=10, rvy=10) for i in range(200)]
    running = True
    while running:
        pygame.display.update()
        gameDisplay.fill((255, 255, 255))
Exemple #9
0
                    write_header(data_path + '/parameters.txt', header)

                sample.save(data_path + '/samples/sample' + str(i) + '.png',
                            data_path + '/parameters.txt')
    else:  # to_generate == "f"
        h = conf['height']
        w = conf['width']
        obj_shape = conf['object']
        obj_color = conf['obj_color']
        motion_type = conf['motion_type']
        dof = conf['dof']
        data_dir = conf['root'] + "Frames_dataset/" + motion_type + '_' + obj_shape + '_' + str(obj_color) + '_' + dof \
                    + '_' + str(n_samples)
        if obj_shape == 'point':
            shape = Shapes.Point(obj_color)
        else:
            circle_parameters = conf['circle_parameters']
            shape = Shapes.Circle(obj_color, circle_parameters['radius'])
            data_dir = data_dir + '_' + str(circle_parameters['radius'])

        data_dir = data_dir + "_" + str(h) + "_" + str(w)
        check_dirs(data_dir, True)
        print(data_dir)

        for i in range(n_samples):
            if i % 1 == 0 or i == n_samples - 1:
                print(i)

            if motion_type == 'URM':
                if i == 0:
Exemple #10
0
nPlayers = 1000
width = 900
length = 900
pygame.init()
gameDisplay = pygame.display.set_mode((width, length))
gameDisplay.fill((255, 255, 255))
clock = pygame.time.Clock()
rad = 100

players = [player(gameDisplay, radius=rad, rpos=True) for i in range(nPlayers)]
opponents = [
    player(gameDisplay, color=(255, 0, 0), rpos=True, rvel=True, max_vel=2)
    for i in range(100)
]

p1 = Shapes.Point(width / 2., length / 2.)
r1 = Shapes.Rect(p1, width, length)

running = True
gens = 0
while running:
    gens += 1
    start_zone = Shapes.Rect(p1, w=100, h=100)
    for opponent in opponents:
        pos_i = Shapes.Point(opponent.x, opponent.y)
        while (start_zone.pointInside(pos_i)):
            opponent.x = random.uniform(0, width)
            opponent.y = random.uniform(0, length)
            pos_i = Shapes.Point(opponent.x, opponent.y)

    running1 = True