Skip to content

Commit 69bce24

Browse files
committed
Fix #234.
1 parent 2a4adeb commit 69bce24

File tree

4 files changed

+33
-31
lines changed

4 files changed

+33
-31
lines changed

book/spatial-partition.markdown

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,10 @@ spatial partitions used in games.
139139

140140
### A sheet of graph paper
141141

142-
Here's the basic idea: imagine the entire field of battle. Now superimpose a
143-
grid of fixed-size squares onto it like a sheet of graph paper. Instead of
144-
storing our units in a single array, we put them in the cells of this grid. Each
145-
cell stores the list of units whose positions are within that cell's boundary.
142+
Imagine the entire field of battle. Now superimpose a grid of fixed-size squares
143+
onto it like a sheet of graph paper. Instead of storing our units in a single
144+
array, we put them in the cells of this grid. Each cell stores the list of units
145+
whose positions are within that cell's boundary.
146146

147147
<img src="images/spatial-partition-grid.png" alt="A grid with Units occupying different cells. Some cells have multiple Units." />
148148

@@ -474,7 +474,7 @@ to solve.
474474
performance, but *consistent* performance: if each partition has the
475475
same number of objects, you ensure that all queries in the world will
476476
take about the same amount of time. When you need to maintain a stable
477-
frame-rate, this consistency may be more important than raw performance.
477+
frame rate, this consistency may be more important than raw performance.
478478

479479
* *It's more efficient to partition an entire set of objects at once.*
480480
When the *set* of objects affects where boundaries are, it's best to
@@ -491,8 +491,8 @@ to solve.
491491

492492
<aside name="quad">
493493

494-
A quadtree partitions 2D space. Its 3D analogue is the *octree*: it takes a
495-
*volume* and partitions it into eight *cubes*. Aside from the extra
494+
A quadtree partitions 2D space. Its 3D analogue is the *octree*, which takes
495+
a *volume* and partitions it into eight *cubes*. Aside from the extra
496496
dimension, it works the same as its flatter sibling.
497497

498498
</aside>

code/cpp/spatial-partition.h

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,7 @@ namespace SpatialPartition
164164
class Unit
165165
{
166166
public:
167-
//^unit-ctor
168-
Unit(Grid* grid, double x, double y)
169-
: grid_(grid),
170-
x_(x),
171-
y_(y),
172-
prev_(NULL),
173-
next_(NULL)
174-
{
175-
grid_->add(this);
176-
}
177-
//^unit-ctor
167+
Unit(Grid* grid, double x, double y);
178168

179169
// Previous code...
180170
//^omit
@@ -190,6 +180,18 @@ namespace SpatialPartition
190180
//^omit
191181
};
192182

183+
//^unit-ctor
184+
Unit::Unit(Grid* grid, double x, double y)
185+
: grid_(grid),
186+
x_(x),
187+
y_(y),
188+
prev_(NULL),
189+
next_(NULL)
190+
{
191+
grid_->add(this);
192+
}
193+
//^unit-ctor
194+
193195
//^add
194196
void Grid::add(Unit* unit)
195197
{
@@ -389,9 +391,9 @@ namespace SpatialPartition
389391
//^grid-melee
390392
void Grid::handleMelee()
391393
{
392-
for (int y = 0; y < NUM_CELLS; y++)
394+
for (int x = 0; x < NUM_CELLS; x++)
393395
{
394-
for (int x = 0; x < NUM_CELLS; x++)
396+
for (int y = 0; y < NUM_CELLS; y++)
395397
{
396398
handleCell(cells_[x][y]);
397399
}

html/bytecode.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1199,7 +1199,7 @@ <h3><a href="#how-is-the-bytecode-generated" name="how-is-the-bytecode-generated
11991199
<p>When you&#8217;re building a UI, you have to choose which framework to use,
12001200
and many of those are specific to one OS. There are cross-platform UI
12011201
toolkits too, but those often get ubiquity at the expense of
1202-
familiarity&#8202;&mdash;&#8202;they feel equally weird on all of platforms.</p>
1202+
familiarity&#8202;&mdash;&#8202;they feel equally foreign on all of platforms.</p>
12031203
</li>
12041204
</ul>
12051205
</li>

html/spatial-partition.html

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,10 @@ <h2><a href="#sample-code" name="sample-code">Sample Code</a></h2>
148148
</aside>
149149

150150
<h3><a href="#a-sheet-of-graph-paper" name="a-sheet-of-graph-paper">A sheet of graph paper</a></h3>
151-
<p>Here&#8217;s the basic idea: imagine the entire field of battle. Now superimpose a
152-
grid of fixed-size squares onto it like a sheet of graph paper. Instead of
153-
storing our units in a single array, we put them in the cells of this grid. Each
154-
cell stores the list of units whose positions are within that cell&#8217;s boundary.</p>
151+
<p>Imagine the entire field of battle. Now superimpose a grid of fixed-size squares
152+
onto it like a sheet of graph paper. Instead of storing our units in a single
153+
array, we put them in the cells of this grid. Each cell stores the list of units
154+
whose positions are within that cell&#8217;s boundary.</p>
155155
<p><img src="images/spatial-partition-grid.png" alt="A grid with Units occupying different cells. Some cells have multiple Units." /></p>
156156
<p>When we handle combat, we only consider units within the same cell. Instead of
157157
comparing each unit in the game with every other unit, we&#8217;ve <em>partitioned</em> the
@@ -242,7 +242,7 @@ <h3><a href="#entering-the-field-of-battle" name="entering-the-field-of-battle">
242242
<p>The first thing we need to do is make sure new units are actually placed into
243243
the grid when they are created. We&#8217;ll make <code>Unit</code> handle this in its
244244
constructor:</p>
245-
<div class="codehilite"><pre><span class="n">Unit</span><span class="p">(</span><span class="n">Grid</span><span class="o">*</span> <span class="n">grid</span><span class="p">,</span> <span class="kt">double</span> <span class="n">x</span><span class="p">,</span> <span class="kt">double</span> <span class="n">y</span><span class="p">)</span>
245+
<div class="codehilite"><pre><span class="n">Unit</span><span class="o">::</span><span class="n">Unit</span><span class="p">(</span><span class="n">Grid</span><span class="o">*</span> <span class="n">grid</span><span class="p">,</span> <span class="kt">double</span> <span class="n">x</span><span class="p">,</span> <span class="kt">double</span> <span class="n">y</span><span class="p">)</span>
246246
<span class="o">:</span> <span class="n">grid_</span><span class="p">(</span><span class="n">grid</span><span class="p">),</span>
247247
<span class="n">x_</span><span class="p">(</span><span class="n">x</span><span class="p">),</span>
248248
<span class="n">y_</span><span class="p">(</span><span class="n">y</span><span class="p">),</span>
@@ -291,9 +291,9 @@ <h3><a href="#a-clash-of-swords" name="a-clash-of-swords">A clash of swords</a><
291291
this:</p>
292292
<div class="codehilite"><pre><span class="kt">void</span> <span class="n">Grid</span><span class="o">::</span><span class="n">handleMelee</span><span class="p">()</span>
293293
<span class="p">{</span>
294-
<span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">y</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">y</span> <span class="o">&lt;</span> <span class="n">NUM_CELLS</span><span class="p">;</span> <span class="n">y</span><span class="o">++</span><span class="p">)</span>
294+
<span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">x</span> <span class="o">&lt;</span> <span class="n">NUM_CELLS</span><span class="p">;</span> <span class="n">x</span><span class="o">++</span><span class="p">)</span>
295295
<span class="p">{</span>
296-
<span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">x</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">x</span> <span class="o">&lt;</span> <span class="n">NUM_CELLS</span><span class="p">;</span> <span class="n">x</span><span class="o">++</span><span class="p">)</span>
296+
<span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">y</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">y</span> <span class="o">&lt;</span> <span class="n">NUM_CELLS</span><span class="p">;</span> <span class="n">y</span><span class="o">++</span><span class="p">)</span>
297297
<span class="p">{</span>
298298
<span class="n">handleCell</span><span class="p">(</span><span class="n">cells_</span><span class="p">[</span><span class="n">x</span><span class="p">][</span><span class="n">y</span><span class="p">]);</span>
299299
<span class="p">}</span>
@@ -646,7 +646,7 @@ <h3><a href="#does-the-partitioning-depend-on-the-set-of-objects" name="does-the
646646
performance, but <em>consistent</em> performance: if each partition has the
647647
same number of objects, you ensure that all queries in the world will
648648
take about the same amount of time. When you need to maintain a stable
649-
frame-rate, this consistency may be more important than raw performance.</p>
649+
frame rate, this consistency may be more important than raw performance.</p>
650650
</li>
651651
<li>
652652
<p><em>It&#8217;s more efficient to partition an entire set of objects at once.</em>
@@ -665,8 +665,8 @@ <h3><a href="#does-the-partitioning-depend-on-the-set-of-objects" name="does-the
665665
name="quad">quadtrees</span>.</p>
666666
<aside name="quad">
667667

668-
<p>A quadtree partitions 2D space. Its 3D analogue is the <em>octree</em>: it takes a
669-
<em>volume</em> and partitions it into eight <em>cubes</em>. Aside from the extra
668+
<p>A quadtree partitions 2D space. Its 3D analogue is the <em>octree</em>, which takes
669+
a <em>volume</em> and partitions it into eight <em>cubes</em>. Aside from the extra
670670
dimension, it works the same as its flatter sibling.</p>
671671
</aside>
672672

0 commit comments

Comments
 (0)