Пример #1
0
def main():
    kd = kdtree()
    '''kd.insert(6,8)
    kd.insert(3,4)
    kd.insert(5,6)
    kd.insert(4,2)
    kd.insert(8,9)
    kd.insert(9,4)
    kd.insert(9,10)
    kd.printkdtree(kd.root)
    kd.searchtree(4,2)
    print("The minimum in x direction  is ",kd.minimum('x').point[0])
    print("The minimum in y direction is " , kd.minimum('y').point[1])
    print("The maximum in x direction is " , kd.maximum('x'))
    print("The maximum in y direction is " , kd.maximum('y'))
    a=[5,6]
    kd.deletekdnode(a)
    kd.printkdtree(kd.root)
    a=[3,4]
    kd.deletekdnode(a)
    print()
    kd.printkdtree(kd.root)
    print()
    a=[100,50]
    w=kd.deletekdnode(a)
    kd.printkdtree(kd.root)'''
    import random
    ss = []
    scene = Scene('test')
    scene.add(Rectangle((100, 100), 200, 200, (255, 255, 255)))
    for i in range(10):
        m = random.randint(100, 300)
        n = random.randint(100, 300)
        kd.insert(m, n)
        scene.add(Circle((m, n), 3, (0, 255, 0)))
        ss.append(distance([m, n], [100, 150]))
    # kd.printkdtree(kd.root)
    ss.sort()
    print('List:', ss[0])
    p = kd.search_nearest([100, 150])
    # print(p)
    scene.add(Circle((100, 150), 3, (255, 0, 255)))
    x = distance([100, 150], p)
    print('Algo:', x)
    import time
    # time.sleep(1)
    scene.add(Circle((p[0], p[1]), 3, (0, 255, 255)))
    y = 'ETA : ' + str(round(x / 10, 2)) + ' min'
    scene.add(Text((50, 50), y))
    scene.write_svg()
    scene.display()
Пример #2
0
def main(in_file, out_file):
    #global im
    #global draw
    global scene
    start_time = parse_file(in_file)
    y = 20
    #im = Image.new("RGB", (WIDTH, HEIGHT), "black")
    #draw = ImageDraw.Draw(im)
    scene = Scene('fplot', HEIGHT, WIDTH)

    for key, values in data.items():
        draw_function(key, values, y)
        y += TRACE_SEPARATOR

    draw_help_lines(y, start_time)

    #f = open(out_file, 'w+')
    #im.save(f, "PNG")
    #f.close
    #print "Width " + str(WIDTH)
    scene.write_svg()
Пример #3
0
			rectangle_count += 1
			s.add(Rectangle((x,y),w,h,(255,255,255,1.0)))
		
	def __str__(self):
		return "Box(%d,%d => %d,%d)" % (self.x, self.y, self.x + self.w, self.y + self.h)
	
	def __repr__(self):
		return self.__str__()
		
	def partition_box(self):
		if self.filled:
			self.a = box(self.x, self.y, self.w/2, self.h/2)
			self.b = box(self.x + self.w/2, self.y, self.w/2, self.h/2)
			self.c = box(self.x + self.w/2, self.y + self.h/2, self.w/2, self.h/2)
			self.d = box(self.x, self.y + self.h/2, self.w/2, self.h/2)
			self.children = (self.a, self.b, self.c, self.d)
		
			if self.w/2 > 5 and self.h/2 > 5:
				for c in self.children:
					c.partition_box();
			else:
				s.add(Rectangle((self.x,self.y),self.w,self.h,(128,128,128,1.0)))
              
			return self.children
					
		
b = box(0, 0, w, h, 1)
b.partition_box()
s.write_svg()

print "Box (%d, %d) partitioned into %d rectangles." % (w, h, rectangle_count)
Пример #4
0
def main():
    print("Enter the value of k in which you waant to run data Structure")
    k = int(input())
    kd=kdtree(k)
    l=list()
    a=list()
    z=True
    scene = Scene('test')
    scene.add(Rectangle((50,50),300,300,(255,255,255)))
    if k>0:
        for i in range(10):
            for i in range(k):
                rand= random.randint(100,300)
                a.append(rand)

            if k==2:
                scene.add(Circle((a[0],a[1]),3,(0,255,0)))      #Adds circle when k=2
            kd.insert(a)
            #l.append(a)   #if want to do balanced kd trees
            a=[]
         #Testing nearest distance
        '''p=[]
        p=l
        x=[]
        for i in range(len(p)):
            x.append(distance(p[i],[100,100]))
        x.sort()
        print('List :',x[0])'''

        #Code for balanced kd trees
        '''height = 0
        while len(l) > 0:
            axis = height % 2
            if height==0:
                h=1
            else:
                h = height ** 2
            while h > 0 and len(l) != 0:
                l = sorted(l, key=lambda point: point[axis])
                if (len(l) % 2 != 0):
                    n = (len(l) - 1) // 2
                    kd.insert(l[n])
                else:
                    n = (len(l) // 2) - 1
                    kd.insert(l[n])
                l.pop(n)
                h -= -1
            height += 1'''

        print("The kd tree in pre Order Traversal is ")
        kd.printkdtree(kd.root)
        print()
        print()
        o=True
        while o:
            print("-----------------------------------------------------------------------------")
            print("Enter the choice of yours what you wanna do")
            print("1)Search for coordinates in KD tree")
            print("2)Find minimum in given dimension")
            print("3)Find maximum in given dimension")
            print("4)To delete node in KD tree")
            if k==2:
                print("5)To search the nearest neighbour of entered point")
            g=int(input())

            print("******************************************************************************")
            if g==1:
                print("Enter the coordinates you want to check in the tree are present or not")
                a=list((map(int,input().split(" "))))
                kd.searchtree(a)
                a=[]
            if g==2:
                print("Enter the Dimension number[0,k)? ")
                a = int(input())
                if a>=0 and a<k:
                    t=kd.minimum(a).point[a]
                    a = []
                    print("The minimum is :",end="  ")
                    print(t)
                else:
                    print("Please enter the correct dimension")

            if g==3:
                print("Enter the dimension number [0-k)? ")
                a = int(input())
                if a>=0 and a<k:
                    t = kd.maximum(a)
                    a = []
                    print("The maximum is :",end="  ")
                    print(t)
                else:
                    print("Enter the correct dimension")
            if g==4:
                print("Enter the coordinates of the node you want to delete")
                a = list(map(int,input().split()))
                t = kd.deletekdnode(a)
                a = []
                print("Tree after deletion is :")
                kd.printkdtree(kd.root)
                print()
        #Nearest Neighbour search in Two dimension
            if k==2 and g==5:
                print("Enter the point where you want to do the nearest search")
                p=[]
                p=list(map(int,input().split()))
                b=kd.search_nearest(p)
                print("The nearest Neighbour is present at the location ",b)
                x=distance(p,b)
                print("The distance of the nearest neighbour is ",x)
                scene.add(Circle((p[0],p[1]),3,(255,0,0)))
                scene.add(Circle((b[0],b[1]),3,(0,0,0)))
                # y='ETA : '+str(round(x/10,2))+' min' #ETA value ( Hypothetical)
                # scene.add(Text((100,20),y))
                scene.write_svg()
                scene.display()
                scene.add(Circle((b[0],b[1]),3,(0,255,0)))
                scene.add(Circle((p[0],p[1]),3,(255,255,255)))

            print("*****************************************************************")
            print("Continue operating on created Tree enter 1 else any other integer")
            ans=int(input())
            if ans!=1:
                o=False
    else:
        print("Enter correct value of k")