Graphics Reference
In-Depth Information
int
CModel::SelectBallAt(
float
x,
float
y) {
Source file.
Model.cpp
file
in the
Model
folder of the the
D3D
_
MultipleBalls
project.
m
_
selected
_
index = -1;
// assume none selected
for
(
int
i=0; i<m
_
AllBalls.Count(); i++)
// loop through all balls
CBall& ball = m
_
AllBalls[i];
distance = ball.GetCenter() to (x, y)
// distance from center of ball to (x,y)
if
(distance < ball.GetRadius())
// if distance is less than radius
m
_
selected
_
index = i;
// ball is selected
.
return
m
_
selected
_
index;
}
Listing 5.23.
CModel::SelectBallAt()
Function (Tutorial 5.5).
being defined to support the left mouse button definition of a new ball. The
m
_
AllBalls
and the
m
_
selected
_
index
define a collection of balls and the
support for a currently selected ball among the collection. We also see public
interface functions to support manipulating these new application states. The in-
teresting changes involved in the implementation of the new
CModel
class are
shown in Listing 5.23. Listing 5.23 shows that to select a ball at position
,
we must loop through all balls in the world collection and compute the distance
for each of the balls. This is an
O
(
x
,
y
)
operation, where
n
is the number of balls
in the world collection. In real-time applications, whenever we encounter re-
quirements of looping through all world elements, we must be acutely aware of
the complexities. In general, any computations involving more than linear-time
(
O
(
n
)
) complexity run the danger of sub-real-time responses. Listing 5.24 shows
the implementation of the
UpdateSimulation()
function. It is important to re-
alize that before we return from this function,
no
events of any kind can arrive
at our application. This means that as far as computing the application state is
concerned, it is guaranteed that nothing will change before we return from this
function. For example, before we return from this function, because the con-
trol of the process is within this function, a user will
not
be able to trigger any
events. Notice that in our implementation, we compute the new velocities, po-
sitions, and membership validity in three separate loops. Because no events can
interrupt this function, the order of these loops is not important. If we had chosen
to compute the membership validity before updating the velocities/positions, the
visible balls on the screen would be different by one update, or a difference of
25 msec.
With the definition of the
CModel
class, mouse button event services becomes
a simple calling of corresponding functions. For example, the right mouse button
down event can be serviced by calling
CModel::SelectBallAt()
, and so on.
(
n
)
Search WWH ::
Custom Search