|
| 1 | +# App to demonstrate NearestNeighbor queries in action |
| 2 | +import Tkinter |
| 3 | +from kdtree import * |
| 4 | + |
| 5 | +class KDTreeApp: |
| 6 | + def __init__(self): |
| 7 | + """App for creating KD tree dynamically""" |
| 8 | + |
| 9 | + self.tree = KDTree() |
| 10 | + self.match = None |
| 11 | + self.redraw = False |
| 12 | + self.shortline = None |
| 13 | + |
| 14 | + self.master = Tkinter.Tk() |
| 15 | + self.w = Tkinter.Frame(self.master, width=410, height=410) |
| 16 | + self.canvas = Tkinter.Canvas(self.w, width=400, height=400) |
| 17 | + |
| 18 | + self.paint() |
| 19 | + |
| 20 | + self.canvas.bind("<Button-1>", self.click) |
| 21 | + self.canvas.bind("<Motion>", self.moved) |
| 22 | + self.w.pack() |
| 23 | + self.w.mainloop() |
| 24 | + |
| 25 | + def toCartesian(self, y): |
| 26 | + return self.w.winfo_height() - y |
| 27 | + |
| 28 | + def toTk(self,y): |
| 29 | + if y == maxValue: return 0 |
| 30 | + tk_y = self.w.winfo_height() |
| 31 | + if y != minValue: |
| 32 | + tk_y -= y |
| 33 | + return tk_y |
| 34 | + |
| 35 | + def view(self): |
| 36 | + """Show window with points""" |
| 37 | + |
| 38 | + def moved(self, event): |
| 39 | + """React to mouse move events""" |
| 40 | + p = (event.x, self.toCartesian(event.y)) |
| 41 | + |
| 42 | + match = self.tree.find(p) |
| 43 | + if match: |
| 44 | + self.redraw = True |
| 45 | + p = match.point |
| 46 | + self.canvas.create_rectangle(p[X_] - 4, self.toTk(p[Y_]) - 4, p[X_] + 4, self.toTk(p[Y_]) + 4, fill='Red') |
| 47 | + self.canvas.delete(self.shortline) |
| 48 | + self.shortline = None |
| 49 | + else: |
| 50 | + if self.redraw: |
| 51 | + self.paint() |
| 52 | + self.redraw = False |
| 53 | + n = self.tree.nearest(p) |
| 54 | + if n: |
| 55 | + pn = n.point |
| 56 | + if self.shortline is None: |
| 57 | + self.shortline = self.canvas.create_line(pn[X_], self.toTk(pn[Y_]), p[X_], self.toTk(p[Y_]), tags="shortline") |
| 58 | + else: |
| 59 | + self.canvas.coords("shortline", pn[X_], self.toTk(pn[Y_]), p[X_], self.toTk(p[Y_])) |
| 60 | + |
| 61 | + |
| 62 | + def click(self, event): |
| 63 | + """Add point to KDtree""" |
| 64 | + p = (event.x, self.toCartesian(event.y)) |
| 65 | + |
| 66 | + self.tree.add(p) |
| 67 | + if self.shortline: |
| 68 | + self.canvas.delete(self.shortline) |
| 69 | + self.shortline = None |
| 70 | + |
| 71 | + self.paint() |
| 72 | + |
| 73 | + def drawPartition (self, r, p, orient): |
| 74 | + if orient == VERTICAL: |
| 75 | + self.canvas.create_line(p[X_], self.toTk(r.y_min), p[X_], self.toTk(r.y_max)) |
| 76 | + else: |
| 77 | + xlow = r.x_min |
| 78 | + if r.x_min == minValue: xlow = 0 |
| 79 | + xhigh = r.x_max |
| 80 | + if r.x_max == maxValue: xhigh = self.w.winfo_width() |
| 81 | + |
| 82 | + self.canvas.create_line(xlow, self.toTk(p[Y_]), xhigh, self.toTk(p[Y_])) |
| 83 | + |
| 84 | + self.canvas.create_rectangle(p[X_] - 4, self.toTk(p[Y_]) - 4, p[X_] + 4, self.toTk(p[Y_]) + 4, fill='Black') |
| 85 | + |
| 86 | + def visit (self, n): |
| 87 | + if n == None: return |
| 88 | + |
| 89 | + self.drawPartition(n.region, n.point, n.orient) |
| 90 | + |
| 91 | + self.visit (n.below) |
| 92 | + self.visit (n.above) |
| 93 | + |
| 94 | + def prepare(self, event): |
| 95 | + """prepare to add points""" |
| 96 | + if self.label: |
| 97 | + self.label.destroy() |
| 98 | + self.label = None |
| 99 | + self.canvas.pack() |
| 100 | + |
| 101 | + def paint(self): |
| 102 | + if self.tree.root: |
| 103 | + for child in self.canvas.winfo_children(): |
| 104 | + child.destroy() |
| 105 | + self.visit(self.tree.root) |
| 106 | + else: |
| 107 | + self.label = Tkinter.Label(self.w, width=100, height = 40, text="Click To Add Points") |
| 108 | + self.label.bind("<Button-1>", self.prepare) |
| 109 | + self.label.pack() |
| 110 | + |
| 111 | + |
| 112 | +if __name__ == "__main__": |
| 113 | + KDTreeApp() |
| 114 | + |
| 115 | +""" |
| 116 | +Change Log |
| 117 | +
|
| 118 | +1. 2014.05.23 import changed to use KDTree from kdtree.py file |
| 119 | +
|
| 120 | + |
| 121 | +""" |
0 commit comments