本文整理汇总了C#中Geom.GetNearestDistance方法的典型用法代码示例。如果您正苦于以下问题:C# Geom.GetNearestDistance方法的具体用法?C# Geom.GetNearestDistance怎么用?C# Geom.GetNearestDistance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Geom
的用法示例。
在下文中一共展示了Geom.GetNearestDistance方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateDistanceGrid
/// <summary>
/// Computes the grid.
/// </summary>
/// <param name="geom">The geometry.</param>
/// <exception cref="ArgumentNullException"><c>geometry</c> is null.</exception>
public void CreateDistanceGrid(Geom geom)
{
if (geom == null)
throw new ArgumentNullException("geom", "Geometry can't be null");
//Don't create distancegrid for geometry that already have one.
//NOTE: This should be used to -update- the geometry's distance grid (if grid cell size has changed).
if (_distanceGrids.ContainsKey(geom.id))
return;
//By default, calculate the gridcellsize from the AABB
if (geom.GridCellSize <= 0)
geom.GridCellSize = CalculateGridCellSizeFromAABB(ref geom.AABB);
//Prepare the geometry. Reset the geometry matrix
Matrix old = geom.Matrix;
geom.Matrix = Matrix.Identity;
//Create data needed for gridcalculations
AABB aabb = new AABB(ref geom.AABB);
float gridCellSizeInv = 1 / geom.GridCellSize;
//Note: Physics2d have +2
int xSize = (int)Math.Ceiling((double)(aabb.Max.X - aabb.Min.X) * gridCellSizeInv) + 1;
int ySize = (int)Math.Ceiling((double)(aabb.Max.Y - aabb.Min.Y) * gridCellSizeInv) + 1;
float[,] nodes = new float[xSize, ySize];
Vector2 vector = aabb.Min;
for (int x = 0; x < xSize; ++x, vector.X += geom.GridCellSize)
{
vector.Y = aabb.Min.Y;
for (int y = 0; y < ySize; ++y, vector.Y += geom.GridCellSize)
{
nodes[x, y] = geom.GetNearestDistance(ref vector); // shape.GetDistance(vector);
}
}
//restore the geometry
geom.Matrix = old;
DistanceGridData distanceGridData = new DistanceGridData();
distanceGridData.AABB = aabb;
distanceGridData.GridCellSize = geom.GridCellSize;
distanceGridData.GridCellSizeInv = gridCellSizeInv;
distanceGridData.Nodes = nodes;
_distanceGrids.Add(geom.id, distanceGridData);
}
示例2: ComputeGrid
/// <summary>
/// Computes the grid.
/// </summary>
/// <param name="geometry">The geometry.</param>
/// <param name="gridCellSize">Size of the grid cell.</param>
public void ComputeGrid(Geom geometry, float gridCellSize)
{
//Prepare the geometry.
Matrix old = geometry.Matrix;
//TODO: Assign geometry.Matrix to Matrix.Identity directly
Matrix identity = Matrix.Identity;
geometry.Matrix = identity;
//Copy the AABB to the grid field
_aabb = new AABB(geometry.AABB);
_gridCellSize = gridCellSize;
_gridCellSizeInv = 1/gridCellSize;
//NOTE: Using double cast instead of converting.
int xSize = (int) Math.Ceiling((double) (_aabb.Max.X - _aabb.Min.X)*_gridCellSizeInv) + 1;
int ySize = (int) Math.Ceiling((double) (_aabb.Max.Y - _aabb.Min.Y)*_gridCellSizeInv) + 1;
//TODO: Possible optimization (normal)! If the shape is symmetric in X and Y axis, don't calculate the points, replicate them.
_nodes = new float[xSize,ySize];
_points = new Vector2[xSize*ySize];
int i = 0;
Vector2 vector = _aabb.Min;
for (int x = 0; x < xSize; ++x, vector.X += gridCellSize)
{
vector.Y = _aabb.Min.Y;
for (int y = 0; y < ySize; ++y, vector.Y += gridCellSize)
{
_nodes[x, y] = geometry.GetNearestDistance(vector); // shape.GetDistance(vector);
_points[i] = vector;
i += 1;
}
}
//restore the geometry
geometry.Matrix = old;
}