本文整理汇总了C#中QuadTree.Insert方法的典型用法代码示例。如果您正苦于以下问题:C# QuadTree.Insert方法的具体用法?C# QuadTree.Insert怎么用?C# QuadTree.Insert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QuadTree
的用法示例。
在下文中一共展示了QuadTree.Insert方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Search_AreaWithOnePoint_ReturnsOnePoint
public void Search_AreaWithOnePoint_ReturnsOnePoint()
{
var center = new Point(0, 0);
var boundary = new Boundary(center, 1, 1);
var quad = new QuadTree<Point>(boundary);
// insert one point in the second quadrant
var point = new Point(-0.75, +0.75);
quad.Insert(point);
// then insert one point in each remaining quadrant
var points = new List<Point>
{
new Point(+0.75, +0.75), // first quadrant
new Point(-0.75, -0.75), // third quadrant
new Point(+0.75, -0.75) // fourth quadrant
};
points.ForEach(p => quad.Insert(p));
// search second quadrant
var searchArea = new Boundary(new Point(-0.5, 0.5), 0.5, 0.5);
var results = quad.Search(searchArea);
Assert.IsTrue(results.Count == 1);
Assert.IsTrue(results.Contains(point));
}
示例2: Insert_WithinBoundary_ReturnsTrue
public void Insert_WithinBoundary_ReturnsTrue()
{
var center = new Point(0, 0);
var boundary = new Boundary(center, 1, 1);
var quad = new QuadTree<Point>(boundary);
Assert.IsTrue(quad.Insert(new Point(0, 0)));
Assert.IsTrue(quad.Insert(new Point(-0.5, 0.5)));
}
示例3: Insert_OutOfBoundary_ReturnsFalse
public void Insert_OutOfBoundary_ReturnsFalse()
{
var center = new Point(0, 0);
var boundary = new Boundary(center, 1, 1);
var quad = new QuadTree<Point>(boundary);
Assert.IsFalse(quad.Insert(new Point(2, 0)));
Assert.IsFalse(quad.Insert(new Point(0, 2)));
}
示例4: Test1000ItemsWithQuery
public void Test1000ItemsWithQuery()
{
Random rand = new Random ();
var bounds = new RectangleF (0f,0f,100f,100f);
QuadTree<RectangleF> tree = new QuadTree<RectangleF>(bounds) {
GetRect = r => r
};
//add 10 items, assert no subnodes.
for (int i = 0; i < 1000; i++)
{
var rect = new RectangleF ( rand.Next (0, 80), rand.Next (0, 80), rand.Next (1, 10), rand.Next (1, 10));
tree.Insert (rect);
}
//insert a rect at 25, 25, 5, 5, and query for it.
var searchRect = new RectangleF (25f, 25f, 5f, 5f);
tree.Insert (searchRect);
//now try and find it.
bool found = false;
int count = 0;
foreach (var rect in tree.Query (new RectangleF (20f, 20f, 10f, 10f)))
{
count++;
Console.WriteLine (rect);
if (rect == searchRect)
{
found = true;
break;
}
}
Console.WriteLine ("count: " + count);
Assert.That (found, Is.True);
}
示例5: TestSubdivision
public void TestSubdivision()
{
Random rand = new Random ();
var bounds = new RectangleF (0f,0f,100f,100f);
QuadTree<RectangleF> tree = new QuadTree<RectangleF>(bounds) {
GetRect = r => r
};
//add 10 items, assert no subnodes.
for (int i = 0; i < 10; i++)
{
var rect = new RectangleF ( rand.Next (0, 80), rand.Next (0, 80), rand.Next (1, 10), rand.Next (1, 10));
tree.Insert (rect);
}
Assert.That (tree.Nodes.Count (), Is.EqualTo (1));
//add 11th item, assert there are subnodes.
var lastRect = new RectangleF (rand.Next (0, 80), rand.Next (0, 80), rand.Next (1, 10), rand.Next (1, 10));
tree.Insert (lastRect);
Assert.That (tree.Nodes.Count (), Is.EqualTo (5));
}
示例6: BuildIndex
public ISpatialIndex BuildIndex(IQueryable<ISpatialDocument> documents)
{
if (!documents.Any()) return new EmptySpatialIndex();
var mercator = new Boundary(new Point(0, 0), 180, 90);
var quadTree = new QuadTree<SpatialDocument>(mercator);
foreach (var doc in documents)
{
quadTree.Insert(new SpatialDocument(doc));
}
var index = new SpatialIndex(quadTree);
return index;
}
示例7: initializeWorld
// For now, just generates rectangular buildings
// width/height in ... pixels?
private void initializeWorld(float mapWidth, float mapHeight, int numBuildings)
{
worldWidth = mapWidth;
worldHeight = mapHeight;
agentTree = new QuadTree<Agent>(agentTreeSplit, agentDepthLimit, 0, 0, worldWidth, worldHeight);
buildingTree = new QuadTree<Rect>(buildingTreeSplit, buildingDepthLimit, 0, 0, worldWidth, worldHeight);
// Probably get something closer to a city by carving streets out
// rather than trying to fill with boxes.
float streetWidth = Mathf.Max(minimumStreetWidth, Mathf.Floor(worldWidth * 0.01f));
// But for current testing, just making some random rectangles.
for(int i=0; i<numBuildings; i++)
{
float xDim = Random.Range(3.0f*streetWidth, 10.0f*streetWidth);
float yDim = Random.Range(3.0f*streetWidth, 10.0f*streetWidth);
float xPos = Random.Range(0.0f, worldWidth-xDim);
float yPos = Random.Range(0.0f, worldHeight-yDim);
// For now just letting these be redundant
Quad tempQuad = new Quad(xPos, yPos, xPos+xDim, yPos+yDim);
Rect tempRect = new Rect(xPos, yPos, xDim, yDim);
buildingTree.Insert(tempRect, ref tempQuad);
structures.Add(tempRect);
}
}
示例8: InitQuadTree
/// <summary>
/// Направи нов QuadTree и пополни го со сите објекти кои моментално постојат во играта.
/// Се повикува при секој повик на OnUpdate
/// </summary>
/// <param name="gameObjects"></param>
private void InitQuadTree(IList<IGameObject> gameObjects)
{
quadtree = new QuadTree<IGameObject>(new RectangleF(0, 0, (float)Game.VirtualGameWidth + 0.1f,
(float)Game.VirtualGameHeight + 0.1f));
foreach (IGameObject obj in gameObjects)
quadtree.Insert(obj);
}
示例9: Search_Area_ReturnsAreaPoints
public void Search_Area_ReturnsAreaPoints()
{
var center = new Point(0, 0);
var boundary = new Boundary(center, 1, 1);
var quad = new QuadTree<Point>(boundary);
var firstQuadrant = new Boundary(new Point(+0.5, +0.5), 0.5, 0.5);
var secondQuadrant = new Boundary(new Point(-0.5, +0.5), 0.5, 0.5);
var thirdQuadrant = new Boundary(new Point(-0.5, -0.5), 0.5, 0.5);
var fourthQuadrant = new Boundary(new Point(+0.5, -0.5), 0.5, 0.5);
var pointsPerQuadrant = 10;
var generator = new RandomGenerator();
// insert points in the third quadrant
var pointsInThirdQuadrant = generator.GeneratePointsWithin(thirdQuadrant, pointsPerQuadrant);
pointsInThirdQuadrant.ForEach(p => quad.Insert(p));
// insert points in other quadrants
var pointsInOtherQuadrants = new List<Point>();
pointsInOtherQuadrants.AddRange(generator.GeneratePointsWithin(firstQuadrant, pointsPerQuadrant));
pointsInOtherQuadrants.AddRange(generator.GeneratePointsWithin(secondQuadrant, pointsPerQuadrant));
pointsInOtherQuadrants.AddRange(generator.GeneratePointsWithin(fourthQuadrant, pointsPerQuadrant));
pointsInOtherQuadrants.ForEach(p => quad.Insert(p));
// search third quadrant
var results = quad.Search(thirdQuadrant);
// assert all result points are in third quadrant
Assert.IsTrue(results.Count == pointsPerQuadrant);
results.ForEach(p => Assert.IsTrue(thirdQuadrant.Contains(p)));
}
示例10: TestIncreaseCapacityDuringInsertion
public void TestIncreaseCapacityDuringInsertion() {
QuadTree<TestItem> quadTree = new QuadTree<TestItem>(
new BoundingRectangle(-100.0f, -100.0f, 100.0f, 100.0f)
);
quadTree.MaxItemsPerNode = 2;
TestItem[] items = new TestItem[] {
new TestItem(new BoundingRectangle(-10.0f, -10.0f, 10.0f, 10.0f)),
new TestItem(new BoundingRectangle(-20.0f, -20.0f, 20.0f, 20.0f)),
new TestItem(new BoundingRectangle(-30.0f, -30.0f, 30.0f, 30.0f)),
new TestItem(new BoundingRectangle(-40.0f, -40.0f, 40.0f, 40.0f))
};
foreach(TestItem item in items) {
quadTree.Insert(item);
}
}
示例11: CreateEnvironment
private void CreateEnvironment()
{
treeCollision = new QuadTree<Tree>(new RectangleF(new Vector2(0, 0), new Vector2(512 * scaleHorizontal, 512 * scaleHorizontal)));
for (int i = 0; i < treeAmount; i++)
{
var x = G.r.Next((int)(512 * scaleHorizontal));
var z = G.r.Next((int)(512 * scaleHorizontal));
var height = CheckHeightCollision(new Vector3(x, 0, z));
while (height > treemaxy || height < treeminy)
{
x = G.r.Next((int)(512 * scaleHorizontal));
z = G.r.Next((int)(512 * scaleHorizontal));
height = CheckHeightCollision(new Vector3(x, 0, z));
}
var tree = new Tree(this, new Vector3(x, height - 8, z));
treeCollision.Insert(tree);
plantsandshit.Add(tree);
}
for (int i = 0; i < flowerAmount; i++)
{
var x = G.r.Next((int)(512 * scaleHorizontal));
var z = G.r.Next((int)(512 * scaleHorizontal));
var height = CheckHeightCollision(new Vector3(x, 0, z));
while (height > flowermaxy || height < flowerminy)
{
x = G.r.Next((int)(512 * scaleHorizontal));
z = G.r.Next((int)(512 * scaleHorizontal));
height = CheckHeightCollision(new Vector3(x, 0, z));
}
var flower = new Flower(this, new Vector3(x, height, z));
plantsandshit.Add(flower);
}
player = new Player(this, new Vector2(3, 8), new Vector3(512, 0, 512));
entities.Add(player);
for (int i = 0; i < birdCount; i++)
{
var x = G.r.Next((int)(512 * scaleHorizontal));
var z = G.r.Next((int)(512 * scaleHorizontal));
var bird = G.g.bf.CreateBird(this, new Vector3(x, 32, z));
entities.Add(bird);
}
}
示例12: Search_PointAtBoundaryCenter_IsReturnedOnlyOnce
public void Search_PointAtBoundaryCenter_IsReturnedOnlyOnce()
{
var center = new Point(0, 0);
var boundary = new Boundary(center, 1, 1);
var quad = new QuadTree<Point>(boundary);
// insert a point in each quadrant
var points = new List<Point>
{
new Point(+0.75, +0.75),
new Point(-0.75, +0.75),
new Point(-0.75, -0.75),
new Point(+0.75, -0.75)
};
points.ForEach(p => quad.Insert(p));
// insert an additional point at the center
var point = new Point(0, 0);
quad.Insert(point);
// search center
var searchArea = new Boundary(center, 0.5, 0.5);
var results = quad.Search(searchArea);
Assert.IsTrue(results.Count == 1);
Assert.IsTrue(results.Contains(point));
}
示例13: Search_PartialBoundary_ReturnResults
public void Search_PartialBoundary_ReturnResults()
{
var center = new Point(0, 0);
var boundary = new Boundary(center, 1, 1);
var quad = new QuadTree<Point>(boundary);
var point = new Point(0.75, 0.75);
var searchArea = new Boundary(new Point(1, 1), 1, 1);
Assert.IsTrue(boundary.Contains(point));
Assert.IsTrue(searchArea.Contains(point));
quad.Insert(point);
var results = quad.Search(searchArea);
Assert.IsTrue(results.Count == 1);
Assert.IsTrue(results.Contains(point));
}
示例14: TestInsertSplitting
public void TestInsertSplitting() {
QuadTree<TestItem> quadTree = new QuadTree<TestItem>(
new BoundingRectangle(-100.0f, -100.0f, 100.0f, 100.0f)
);
quadTree.MaxItemsPerNode = 6;
TestItem[] items = new TestItem[] {
// Unsplittable node to test FirstSplittableItemIndex
new TestItem(new BoundingRectangle(-10.0f, -10.0f, 10.0f, 10.0f)),
// Four corners
new TestItem(new BoundingRectangle(-90.0f, -90.0f, -10.0f, -10.0f)),
new TestItem(new BoundingRectangle(10.0f, -90.0f, 90.0f, -10.0f)),
new TestItem(new BoundingRectangle(-90.0f, 10.0f, -10.0f, 90.0f)),
new TestItem(new BoundingRectangle(10.0f, 10.0f, 90.0f, 90.0f)),
// Unsplittable node to test split skipping
new TestItem(new BoundingRectangle(-15.0f, -15.0f, 15.0f, 15.0f)),
// Causes the node to split
new TestItem(new BoundingRectangle(-90.0f, -90.0f, 90.0f, 90.0f)),
// To test insertion into child nodes
new TestItem(new BoundingRectangle(-5.0f, -5.0f, 5.0f, 5.0f)),
new TestItem(new BoundingRectangle(-90.0f, -90.0f, -85.0f, -85.0f)),
new TestItem(new BoundingRectangle(85.0f, -90.0f, 90.0f, -85.0f)),
new TestItem(new BoundingRectangle(-90.0f, 85.0f, -85.0f, 90.0f)),
new TestItem(new BoundingRectangle(85.0f, 85.0f, 90.0f, 90.0f))
};
foreach(TestItem item in items) {
quadTree.Insert(item);
}
// Remember the current bounding rectangle lookups for the items which
// should be situated in other quad-tree nodes by now
int lookupCount2 = items[2].BoundRectangleLookups;
int lookupCount3 = items[3].BoundRectangleLookups;
int lookupCount4 = items[4].BoundRectangleLookups;
// Perform the query
List<TestItem> result = new List<TestItem>();
quadTree.Query(new BoundingRectangle(-80.0f, -80.0f, -20.0f, -20.0f), result);
// Make sure that the quad-tree didn't check the items that should have
// been placed into other nodes
Assert.AreEqual(lookupCount2, items[2].BoundRectangleLookups);
Assert.AreEqual(lookupCount3, items[3].BoundRectangleLookups);
Assert.AreEqual(lookupCount4, items[4].BoundRectangleLookups);
// Make sure the query results are correct
Assert.AreEqual(2, result.Count);
Assert.Contains(items[1], result);
Assert.Contains(items[6], result);
}
示例15: TestIncreaseCapacityDuringSplit
public void TestIncreaseCapacityDuringSplit() {
QuadTree<TestItem> quadTree = new QuadTree<TestItem>(
new BoundingRectangle(-100.0f, -100.0f, 100.0f, 100.0f)
);
quadTree.MaxItemsPerNode = 5;
TestItem[] items = new TestItem[] {
new TestItem(new BoundingRectangle(-60.0f, -60.0f, -40.0f, -40.0f)),
new TestItem(new BoundingRectangle(-70.0f, -70.0f, -30.0f, -30.0f)),
new TestItem(new BoundingRectangle(-80.0f, -80.0f, -20.0f, -20.0f)),
new TestItem(new BoundingRectangle(-90.0f, -90.0f, -10.0f, -10.0f)),
new TestItem(new BoundingRectangle(-160.0f, -160.0f, -140.0f, -140.0f)),
new TestItem(new BoundingRectangle(-170.0f, -170.0f, -130.0f, -130.0f)),
new TestItem(new BoundingRectangle(-180.0f, -180.0f, -120.0f, -120.0f)),
new TestItem(new BoundingRectangle(-190.0f, -190.0f, -110.0f, -110.0f))
};
foreach(TestItem item in items) {
quadTree.Insert(item);
}
}