Added drawing to QuadTree and quad tree test program
return o
else:
return None
+
+
+ def draw(self, rect = None):
+ glColor3f(1.0, 1.0, 1.0)
+ glBegin(GL_LINE_LOOP)
+ glVertex3f(self.bbox.left, self.bbox.top, 0)
+ glVertex3f(self.bbox.right, self.bbox.top, 0)
+ glVertex3f(self.bbox.right, self.bbox.bottom, 0)
+ glVertex3f(self.bbox.left, self.bbox.bottom, 0)
+ glEnd()
+
+ for (r, o) in self.rects:
+ if rect and r.colliderect(rect):
+ glColor3f(1.0, 1.0, 0.0)
+ else:
+ glColor3f(0.0, 0.0, 1.0)
+ glBegin(GL_LINE_LOOP)
+ glVertex3f(r.left, r.top, 0)
+ glVertex3f(r.right, r.top, 0)
+ glVertex3f(r.right, r.bottom, 0)
+ glVertex3f(r.left, r.bottom, 0)
+ glEnd()
+
+ if self.subregions:
+ for s in self.subregions:
+ s.draw(rect)
+
+ if rect:
+ glColor3f(1.0, 0.0, 0.0)
+ glBegin(GL_LINE_LOOP)
+ glVertex3f(rect.left, rect.top, 0)
+ glVertex3f(rect.right, rect.top, 0)
+ glVertex3f(rect.right, rect.bottom, 0)
+ glVertex3f(rect.left, rect.bottom, 0)
+ glEnd()
+#!/usr/bin/python
+
+import Richter.Engine as Engine
+from Richter.util import QuadTreeR
+import pygame
+from pygame.locals import *
+from random import randint
+
+r = QuadTreeR((0, 0, 640, 480))
+for i in xrange(0, 20):
+ x = randint(0, 640)
+ y = randint(0, 480)
+ w = randint(0, 640 - x)
+ h = randint(0, 480 - y)
+ r.add((x,y,w,h), None)
+
+mouse = pygame.Rect(0, 0, 10, 10)
+
+Engine.init((640, 480))
+
+def input(e):
+ if e.type == MOUSEMOTION:
+ mouse.center = (e.pos[0], 480 - e.pos[1])
+ if e.type == KEYDOWN:
+ if e.key == K_LEFT:
+ mouse.centerx -= 1
+ elif e.key == K_RIGHT:
+ mouse.centerx += 1
+ elif e.key == K_UP:
+ mouse.centery += 1
+ elif e.key == K_DOWN:
+ mouse.centery -= 1
+ elif e.key == K_ESCAPE:
+ Engine.stop()
+
+Engine.eventhandlers.append(input)
+
+def draw():
+ r.draw(mouse)
+
+Engine.drawers.append(draw)
+
+Engine.run()