aboutsummaryrefslogtreecommitdiff
path: root/Util.py
blob: 2973e339631a98ecfd2959853a6f0658ee448446 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import pdb
from xml.etree.ElementTree import ElementTree
import math,struct
from bisect import *
#import json # json.loads() to decode string; json.dumps() to encode data
import socket
import random
from pygame.locals import *
from pixelevents.StepEvent import *

classArgsMem = {}
UNI = 0
colorByteMem = {}
CONFIG_PATH = 'config/'
kinetDict = {'flags': 0, 'startcode': 0, 'pad':0}
componentDict = {}
#Only for rough estimates.  Kindof lazy on specifics.
def pointWithinBoundingBox(point, bb): #this could be in 4 lines, but I'm lazy.
    return sum([(point[i % 2] <= bb[i]) == (i>1) for i in range(4)]) == 4 
print pointWithinBoundingBox((118,21), (10,8,298,42))
def addLocations(l1,l2):
    return tuple([l1[i]+l2[i] for i in range(len(l1))])
def setScreen(screen):
    globals()["screen"] = screen
def getScreen():
    return screen
def setComponentDict(componentDictRef):
    globals()["componentDict"] = componentDictRef
def getComponentById(cid):
    if cid in componentDict:
        return componentDict[cid]
    else:
        return None
def addPixelEventIfMissing(responseDict):
    if not 'PixelEvent' in responseDict:
        if 'Color' in responseDict:
            color = responseDict['Color']
        else:
            raise Exception('Need Color.  Probably')
        responseDict['PixelEvent'] = StepEvent.generate(300, color)
def gaussian(x,height,center,width):
    a=height
    b=center
    c=width
    return a*math.exp(-((x-b)**2)/(2*c**2))
def dist(l1, l2):
    return math.sqrt(sum([(l1[i]-l2[i])**2 for i in range(len(l1))]))
def find_le(a, x):
    'Find rightmost value less than or equal to x'
    return bisect_right(a, x)-1

def find_ge(a, x):
    'Find leftmost value greater than x'
    return bisect_left(a, x)
#Given a dictionary of connections, returns their topological ordering -- (the
#order in which they can be visited such that all parents have been visited
#before their children.  Returns the order or None if no such ordering exists
#(the graph contains a cycle).
def topologicalSort(adjacencyDict):
    def dfsVisit(vertex):
        gray[vertex] = 1
        for child in adjacencyDict[vertex]:
            if not child in visited:
                if child in gray: #We have a cycle.  No topological ordering
                    #exists!
                    raise Exception('Cycle!') 
                dfsVisit(child)
        orderedList.insert(0, vertex)
        visited[vertex] = 1
    orderedList = []
    visited = {}
    gray = {}
    for vertex in adjacencyDict:
        try:
            if not vertex in visited:
                dfsVisit(vertex)
        except:
            return None #cycle
    return orderedList
def topoTest():
    adj = {'a':['d','c'], 'b':['c'], 'c':['e'], 'd':['e'], 'e':[]}
    print topologicalOrdering(adj)
def testXMLParse(fileName):
    #pdb.set_trace()
    config = ElementTree()
    config.parse(fileName)
    print generateArgDict(config.find('ChildElement'))
    print generateArgDict(config.find('Renderer'))
##CONSTANTS##
location = 'Location'