Assignment 1 for Data Structures for Gaming Programming Applications | CMSC 420, Assignments of Data Structures and Algorithms

Material Type: Assignment; Professor: Samet; Class: Data Structures; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;

Typology: Assignments

Pre 2010

Uploaded on 02/13/2009

koofers-user-cdn
koofers-user-cdn 🇺🇸

9 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Fall 2005 CMSC 420
Hanan Samet
Programming Assignment 1:
A Data Structure For Game Programming Applications1
Abstract
Designing interactive games is a challenging task that requires good understanding of spatial
data structures. In this assignment you are required to handle data similar to that used in game
programming. In such an environment the primary entities are small rectangles and the problem
in which we are interested is how to manage a large collection ofthem. In the following we trace
the development of a variant of the quadtree data structure that can be used for such a problem.
Your task is to implement this data structure in such a way that a number of operations can be
efficiently handled. An example JAVA applet for the data structure can be found on the home
page of the class.
This assignment is dividedinto four parts. PASCAL is the preferred programminglanguage
although you may use C or C++. For the first two parts, you must read the attached description
of the problem and data structure. A detailed explanation of the assignment including the
specification of the operations which you are to implement is found at the end of the description.
After you have done this, you are to turn in a proposed implementation of the data structure
using PASCALs (or C or C++) record (structure) definition facility. One week later you must
turn in a PASCAL (or C or C++) program for the command decoder (i.e., scanner for the
commands corresponding to the operations which are to be performed on the data structure).
For the third part, you are to write a PASCAL (or C or C++) program to implement the data
structure and operations (1)-(8). For the fourth part, you are to implement operations (9)-(13).
Operations (14)-(16) are optional and you will get extra credit if you turn them in with part four.
1Copyright c
2005 by Hanan Samet. No part of this document may be reproduced, stored in a retrieval system, or
transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the
express prior permission of the author.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Assignment 1 for Data Structures for Gaming Programming Applications | CMSC 420 and more Assignments Data Structures and Algorithms in PDF only on Docsity!

Fall 2005 CMSC 420

Hanan Samet

Programming Assignment 1: A Data Structure For Game Programming Applications^1

Abstract Designing interactive games is a challenging task that requires good understanding of spatial data structures. In this assignment you are required to handle data similar to that used in game programming. In such an environment the primary entities are small rectangles and the problem in which we are interested is how to manage a large collection of them. In the following we trace the development of a variant of the quadtree data structure that can be used for such a problem. Your task is to implement this data structure in such a way that a number of operations can be efficiently handled. An example JAVA applet for the data structure can be found on the home page of the class. This assignment is divided into four parts. PASCAL is the preferred programming language although you may use C or C++. For the first two parts, you must read the attached description of the problem and data structure. A detailed explanation of the assignment including the specification of the operations which you are to implement is found at the end of the description. After you have done this, you are to turn in a proposed implementation of the data structure using PASCAL’s (or C or C++) record (structure) definition facility. One week later you must turn in a PASCAL (or C or C++) program for the command decoder (i.e., scanner for the commands corresponding to the operations which are to be performed on the data structure). For the third part, you are to write a PASCAL (or C or C++) program to implement the data structure and operations (1)-(8). For the fourth part, you are to implement operations (9)-(13). Operations (14)-(16) are optional and you will get extra credit if you turn them in with part four.

(^1) Copyright c 2005 by Hanan Samet. No part of this document may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the express prior permission of the author.

1 Region-Based Quadtrees

The quadtree is a member of a class of hierarchical data structures that are based on the principle of recursive decomposition. As an example, consider the point quadtree of Finkel and Bentley [1] which should be familiar to you as it is simply a multidimensional generalization of a binary search tree. In two dimensions each node has four subtrees corresponding to the directions  ,  ,  , and . Each subtree is commonly referred to as a quadrant or subquadrant. For example, see Figure 1.6^2 where a point quadtree of 8 nodes is presented. In our presentation we shall only discuss two- dimensional quadtrees although it should be clear that what we say can be easily generalized to more than two dimensions. For the point quadtree the points of decomposition are the data points themselves (i.e., in Figure 1.6, Chicago at location (35,40) subdivides the two dimensional space into four rectangular regions). Requiring the regions to be of equal size leads to the region quadtree of Klinger [4,5,6]. This data structure was developed for representing homogeneous spatial data and is used in computer graphics, image processing, geographical information systems, pattern recognition, and other applications. For a history and review of the quadtree representation, see pp. 1-16 in [5].

As an example of the region quadtree, consider the region shown in Figure 1.1a which is rep- resented by a 2^3  23 binary array in Figure 1.1b. Observe that 1’s correspond to picture elements (termed pixels) which are in the region and 0’s correspond to picture elements that are outside the region. The region quadtree representation is based on the successive subdivision of the array into four equal-size quadrants. If the array does not consist entirely of 1’s or 0’s (i.e., the region does not cover the entire array), then we subdivide it into quadrants, subquadrants, ... until we obtain blocks (possibly single pixels) that consist entirely of 1’s or entirely of 0’s. For example, the result- ing blocks for the region of Figure 1.1b are shown in Figure 1.1c. This process is represented by a quadtree in which the root node corresponds to the entire array, the four sons of the root node repre- sent the quadrants, and the leaf nodes correspond to those blocks for which no further subdivision is necessary. Leaf nodes are said to be BLACK or WHITE depending on whether their corresponding blocks are entirely within or outside of the region respectively. All non-leaf nodes are said to be GRAY. The region quadtree for Figure 1.1c is shown in Figure 1.1d.

2 PR Quadtrees

There are a number of ways of adapting the region quadtree to represent point data. If the domain of data points is discrete, then we can treat data points as if they are BLACK pixels in a region quadtree. If this is not the case, then the data points cannot be represented since the minimum separation between the data points is unknown. This leads us to an adaptation of the region quadtree to point data which associates data points (that need not be discrete) with quadrants. In order to avoid confusion with the point and region quadtrees we call the resulting data structure a PR quadtree (P for point and R for region).

The PR quadtree is organized in the same way as the region quadtree. The difference is that leaf nodes are either empty (i.e., WHITE) or contain a data point (i.e., BLACK) and the values of its coordinates. A quadrant contains at most one data point. For example, Figure 2.32 is the PR quadtree corresponding to the data of Figure 1.6. Note that, unlike the region quadtree, when a non-terminal node has four BLACK sons, they are not merged. This is natural since a merger of

(^2) All numbered figures of the form X.YY and page numbers refer to [5] while all numbered figures of the form ZZ are found in this document.

A

J

I

H^ G

F

B

C

E

D

       ! "#$% &('

A

J

I

H^ G

F

B

C

E

D

  ) ,+- .0/&12#&3!456789;:!=<>!!43?#4@30& ; A! B?:69C-DFEHGI<>!!43?# J3K;:!  A! L#$% &*5M #7 '

containing the rectangle and any r-pieces already present in the node, P is split, and the insertion process is recursively invoked to attempt to insert the elements of L in the four sons of P. For example, Figure 4a–e shows how the RECT quadtree for the collection of rectangles in Figure 1 is constructed in incremental fashion for rectangles  , N, O ,  , and . We assume that the empty collection is represented by a one node tree having no rectangles.

D D E 12 13 14 15

A C B B 3 4 5 6

C C D 8 9 10

I J 26 27 28 29

F G H 17 18 19 20

I I I 22 23 24

2 7 16 21

1

11 25

   GB# #00#& 0%$6  32 &364 % 8 ?:6 <!!4;# 4 @30& ;  ! ?:6 C-DFEHG <0!43?#=  ?:65 ; 9! L#86 & M # '

3.2 Deletion

Deletion of a rectangle, say R , from a RECT quadtree is analogous to the process used for PR quad- trees. The control structure is identical to that used in the insertion of a rectangle. Again, the tree is traversed in preorder and the rectangle is successively clipped against the blocks corresponding to the nodes. Once a leaf node is encountered in which rectangle R participates, say P , the rectangle is removed from P. Once the remaining brothers of P have been checked for the presence of r-pieces of R , we determine if nodes can be merged (termed collapsing ; for more details, see pp. 97-99 in [5]). Collapsing takes place if the brothers of P were either empty or contained the r-pieces of the same rectangle. The difference from deletion in a PR quadtree is that in a PR quadtree collapsing can only take place if two of the brothers of P are empty. On the other hand, in the RECT quadtree collapsing can take place as long as all of P ’s brothers contain r-pieces of the same rectangle. For example, when rectangle  is deleted from Figure 2, the result is that nodes 26, 27, 28, and 29 are merged to yield node 25, which is in turn merged with nodes 22, 23, and 24 to yield node 21 (see the resulting block decomposition in Figure 5).

3.3 Search

The most common search query is one that seeks to determine if a given rectangle overlaps (i.e., intersects) any of the existing rectangles. This operation is a prerequisite to the successful insertion of a rectangle. Range queries can also be performed. However, they are more usefully cast in terms of finding all the rectangles in a given area (i.e., a window query). Another popular query is one that seeks to determine if one collection of rectangles can be overlaid on another collection without any of the component rectangles intersecting one another.

These two operations can be implemented by using variants of algorithms developed for han- dling set operations (i.e., union and intersection) in region-based quadtrees [3,7]. The range query is answered by intersecting the query rectangle with the RECT quadtree. The overlay query is an-

A

I

H^ G

F

B

C

E

D

   CBDFE G <!!4;# #&  ! F40  %@#86  # ?:6  ,! #?!6 &

 !0M5M #7 '

structure is a BLACK BOX. Thus you need to specify your primitives in such a way that they are independent of the data structure finally chosen. You are strongly advised to begin implementing some of the operations. For example, you should implement an output routine so that you can see whether your program is working properly.

For the third and fourth parts of the assignment, you are to write a PASCAL (or C or C++) program to implement the data structure and the specified operations. Together they are worth 60 points. Part three consists of operations (1)-(8) given below. They are worth 30 points. Part four consists of operations (9)-(13) given below. They are worth 30 points. Operations (14)-(16) are for extra credit and are to be turned in with part four. They are worth up to 7 points apiece.

In order to facilitate grading and your task, you are to use the data structure implementation that will be given to you in class on the first meeting date after you turn in the first two parts of the

assignment. For any operation that is not implemented, say  , your command decoder must output

a message of the form  O 

In order to facilitate your program as well as lend some realism to your task you are to implement the RECT quadtree in a raster-based graphics environment. This means that you are dealing with a

world of pixels. The size of the world can be varied, and is a 2 w^^  2 w^ array of pixels. As a default,

you should assume w  7, i.e., a size of 128  128. The pixel at the lower left corner has coordinate

values (0,0) and the pixel at the upper right corner has coordinate values (2 w^^  1,2 w^^  1). Each pixel

serves as the center of a square of size 1  1. This is the smallest unit into which our quadtrees

will decompose the world. Note that the endpoints and widths of the rectangles will be restricted to

integers. All rectangles are of size  3  i    3  j , where 0  i  125 and 0  j  125. In other

words, the smallest rectangle is of size 3  3 and the largest is 128  128.

One class meeting date before the due date of each part of the project you will be informed of the availability of and name of the test data file which you are to use in exercising your program for grading purposes. You should also prepare your own test data. A sample file for this purpose will also be provided.

4.1 Data Structure Selection

You are to select a data structure to implement the RECT quadtree. Turn in a definition in the form of a set of PASCAL (or C or C++) records. In doing this part of the assignment you should bear in mind the type of data that is being represented and the type of operations that will be performed on it. In order to ease your task, remember that the primitive entity is the rectangle. We specify a rectangle by giving the x and y coordinate values of its lower left corner, and the horizontal and vertical distances to its borders (i.e., the lengths of its sides). The rest of your task is to build on this entity adding any other information that is necessary. The nature of the operations is described in Sections 4.3–4.5.

From the description of the operations you will see that a name is associated with each rectangle. At times, the operations are specified in terms of these names. Thus you will also need a mechanism to efficiently keep track of these names. It should be integrated with the rest of your data structures.

4.2 Command Decoder

You are to turn in a working command decoder written in PASCAL (or C or C++) for all the commands (including the optional ones) given in Sections 4.3–4.5. You are not expected to do error recovery and can assume that the commands are syntactically correct. All commands will fit on one line. Lengths of names are restricted to 6 characters or less and can be any combination of letters or digits (e.g.,

, N , etc.). However, for your own safety you may wish to incorporate some

primitive error handling. Test data for this part of the assignment will be found in a file specified by the Teaching Assistant.

The output for the command decoder consists of the number of the operation (e.g., “1” for com-

mand 

   ) and the actual values of the parameters if the command has any parameters

(e.g., the value of    for the  

  command).

4.3 Part Three: Basic Operations

(1) Initialize the quadtree. The command  

       is always the first command

in the input stream.    determines the length of each side of the square are covered by the

quadtree. Each side has the length 2 . It also has the effect of starting with a fresh data set.

(2) Generate a display of a 2 ^  2 ^ square from the RECT quadtree. It is invoked by the

command    

. To draw the RECT quadtree, you are to use the drawing routines provided.

An appendix to the project description covers their use, and the utilities  

 and    

that can be used to render the output of your programs on a screen or a printer. A dashed (broken) line should be used to draw quadrant lines, but the rectangles should be solid (i.e., not dashed). Rectangle names should be output somewhere near the rectangle or within the rectangle. When this convention causes the output of a quadrant line to coincide with the output of the boundary of a rectangle, then the output of the rectangle takes precedence and the coincident part of the quadrant line is not output.

(3) List all the rectangles in the data base in alphanumerical order. This means that letters come be- fore digits in the collating sequence. Similarly, shorter identifiers precede longer ones. For example, a sorted list is

N ,

, . It is invoked by the command   O 

  () and yields

for each rectangle its name, the x and y coordinate values of its lower left corner, and the horizontal and vertical distances to its borders from the lower left corner (i.e., the lengths of its sides). This is

4.4 Part Four: Advanced Operations

In order to facilitate grading of these operations as well as the optional operations in Section 4.5, please provide a trace output of the execution of the operations which lists the nodes (both leaf and nonleaf) that have been visited while executing the operation. This trace is initiated by the

command 

O  and is terminated by the command  

O . In order for the trace output to

be concise, you are to represent each node of the RECT quadtree that has been visited by a unique number which is formed as follows. The root of the quadtree is assigned the number 0. Given a

node with number N , its  ,  ,  ,and   children are labeled 4 N  1, 4 N  2, 4 N  3, and

4 N  4, respectively. For example, starting at the root, the  child is numbered 2, while the 

child of the  child of the root is numbered 4(40+1)+4=8.

(9) Determine all the rectangles in the RECT quadtree that touch (i.e., are adjacent along a side

or a corner) a given rectangle. This operation is invoked by the command  O   where

is the name of a rectangle. Since rectangle is referenced by name, thus must be in

the database for the operation to work but it need not necessarily be in the MX-CIF quadtree. The command returns the names of all the touched rectangles in conjunction with the following

message   

O   

  O

   O

   . Otherwise, the command returns . For each rect-

angle r that touches , display (i.e., highlight) the point in r for which the x and y coordinate values

are minimum (i.e., the lower-leftmost corner). It should be clear that the intersection of r with is

empty.

(10) Determine all of the rectangles in the RECT quadtree that lie within a given distance of a given

rectangle. This is the so-called ‘lambda’ problem. Given a distance  (an integer here although it

could also be a real number in the more general case), it is invoked by the command     

where is the name of the query rectangle. In essence, this operation constructs a query rectangle 

with the same centroid as and distances  ^  and 

 to the border. Now, the query returns the

identity of all rectangles whose intersection with the region formed by the difference of  and is

not empty (i.e,, any rectangle r that has at least one point in common with  ). In other words, we

have a shell of width  around and we want all the rectangles that have a point in common with

this shell. Rectangle need not necessarily be in the RECT quadtree. Note that for this operation

you must recursively traverse the tree to find the rectangles that overlap the query region. You will NOT be given credit for a solution that uses neighbor finding, such as one (but not limited to) that

starts at the centroid of and finds its neighbors in increasing order of distance. This is the basis of

another operation.

(11) Find the nearest neighboring rectangle in the horizontal and vertical directions, respectively, to

a given rectangle. To locate a horizontal neighbor, use the command    N   where

is the name of the query rectangle.

   N    locates a vertical neighbor. By “nearest”

horizontal (vertical) neighboring rectangle, it is meant the rectangle whose vertical (horizontal) side, or extension, is closest to a vertical (horizontal) side of the query rectangle. If the vertical (horizontal) extension of a side of rectangle r causes the extended side of r to intersect the query rectangle, then r is deemed to be at distance 0 and is thus not a candidate neighbor. In other words, the distance has to be greater than zero. The commands return as their value the name of the

neighboring rectangle if one exists and  otherwise as well as an appropriate message. Rectangle

need not necessarily be in the RECT quadtree. If more than one rectangle is at the same distance,

then return the name of just one of them. Note that rectangles that are inside are not considered

by this query. (12) Given a point, return the name of the nearest rectangle. By “nearest,” it is meant the rectangle whose side or corner is closest to the point. Note that this rectangle could also be a

rectangle that contains the point. In this case, the distance is zero. It is invoked by the command

    O         where   and   are the x and y coordinate values, respectively,

of the point. If no such rectangle exists (e.g., when the tree is empty), then output an appropriate message (i.e., that the tree is empty). If more than one rectangle is at the same distance, then return the name of just one of them.

(13) Find all rectangles in a rectangular window anchored at a given point. It is invoked by the

command     

 where  

and  

are the x and y coordinate values, respec-

tively, of the lower left corner of the window and   and 

are the horizontal and vertical distances, respectively, to its borders from the corner. Your output is a list of the names of the rectangles that are completely inside the window, and a display of the RECT quadtree that only shows the rect- angles that are in the window. This is similar to a clipping operation. Draw the boundary of the window using a dashed rectangle. Do not show quadrant lines within the window. All arguments to

    are integers (i.e., 

, and  

). Note that for this operation you must recursively traverse the tree to find the rectangles that overlap the query region. You will NOT be given credit for a solution that uses neighbor finding, such as one (but not limited to) that starts at the centroid of the window and finds its neighbors in increasing order of distance. This is the basis of another operation.

4.5 Optional Operations

(14) Find the nearest neighbor in all directions to the boundary of a given rectangle. It is invoked

by the command ^

     N  

where is the name of a rectangle. By “nearest,” it is

meant the rectangle C with a point on its side or corner, say P , such that the distance from P to a side

or corner of the query rectangle is a minimum. 

     N  returns as its value the name

of the neighboring rectangle if one exists and  otherwise as well as an appropriate message.

Rectangle need not necessarily be in the RECT quadtree. If more than one rectangle is at the same

distance, then return the name of just one of them. Note that rectangles that are inside are not

considered by this query.

(15) Given a rectangle, find its nearest neighbor with a name that is lexicographically greater. It is

invoked by the command   O                .N    where is the name of a

rectangle. By “lexically greater nearest” it is meant the rectangle C whose name is lexically greater

than that of with a point on O’s side, say P , such that the distance from P to a side of the query

rectangle is a minimum. 

0O

  N  returns as its value the name

of the neighboring rectangle if one exists and  otherwise as well as an appropriate message.

Rectangle need not necessarily be in the RECT quadtree. If more than one rectangle is at the same

distance, then return the name of just one of them. Note that rectangles that are inside are not

considered by this query. This operation should not examine more than the minimum number of rectangles that are necessary to determine the lexically greater nearest neighbor. Thus you should use an incremental nearest neighbor algorithm (e.g., [2]).

(16) Perform connected component labeling on the RECT quadtree. This means that all touching rectangles are assigned the same label. By “touching,” it is meant that the rectangles are adjacent

along a side or a corner. This is accomplished by the command 

N  . The result of the operation

is a display of the RECT quadtree where all touching rectangles are shown with the same label. Use integer labels.