commit:38796cc69dc516ecc26ef9b61942091ade677d3f
author:Chip Black
committer:Chip Black
date:Tue Jul 15 21:38:07 2008 -0500
parents:97b0a22977a4a5d66ca4695345bfa126909b5029
Added drawing to QuadTree and quad tree test program
diff --git a/Richter/util.py b/Richter/util.py
line changes: +35/-0
index 52ae449..47bcfbc
--- a/Richter/util.py
+++ b/Richter/util.py
@@ -70,3 +70,38 @@ class QuadTreeR:
 			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()

diff --git a/quadtreetest.py b/quadtreetest.py
line changes: +43/-0
index 0000000..fdc742c
--- /dev/null
+++ b/quadtreetest.py
@@ -0,0 +1,43 @@
+#!/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()