本文整理汇总了C#中Polygon.MergeParallelEdges方法的典型用法代码示例。如果您正苦于以下问题:C# Polygon.MergeParallelEdges方法的具体用法?C# Polygon.MergeParallelEdges怎么用?C# Polygon.MergeParallelEdges使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Polygon
的用法示例。
在下文中一共展示了Polygon.MergeParallelEdges方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PolygonizeTriangles
/**
* Turns a list of triangles into a list of convex polygons. Very simple
* method - start with a seed triangle, keep adding triangles to it until
* you can't add any more without making the polygon non-convex.
*
* Returns an integer telling how many polygons were created. Will fill
* polys array up to polysLength entries, which may be smaller or larger
* than the return value.
*
* Takes O(N*P) where P is the number of resultant polygons, N is triangle
* count.
*
* The final polygon list will not necessarily be minimal, though in
* practice it works fairly well.
*/
public static int PolygonizeTriangles(List<Triangle> triangulated, List<Polygon> polys, int polysLength)
{
int polyIndex = 0;
if (triangulated.Count <= 0)
return 0;
else
{
int[] covered = new int[triangulated.Count];
for (int i = 0; i < triangulated.Count; ++i)
{
covered[i] = 0;
//Check here for degenerate triangles
if (((triangulated[i].x[0] == triangulated[i].x[1]) && (triangulated[i].y[0] == triangulated[i].y[1]))
|| ((triangulated[i].x[1] == triangulated[i].x[2]) && (triangulated[i].y[1] == triangulated[i].y[2]))
|| ((triangulated[i].x[0] == triangulated[i].x[2]) && (triangulated[i].y[0] == triangulated[i].y[2])))
{
covered[i] = 1;
}
}
bool notDone = true;
while (notDone)
{
int currTri = -1;
for (int i = 0; i < triangulated.Count; ++i)
{
if (covered[i] != 0)
continue;
currTri = i;
break;
}
if (currTri == -1)
{
notDone = false;
}
else
{
Polygon poly = new Polygon(triangulated[currTri]);
covered[currTri] = 1;
int index = 0;
for (int i = 0; i < 2*triangulated.Count; ++i, ++index)
{
while (index >= triangulated.Count) index -= triangulated.Count;
if (covered[index] != 0)
{
continue;
}
Polygon newP = poly.Add(triangulated[index]);
if (newP == null)
continue;
if (newP.nVertices > Polygon.maxVerticesPerPolygon)
{
newP = null;
continue;
}
if (newP.IsConvex())
{ //Or should it be IsUsable? Maybe re-write IsConvex to apply the angle threshold from Box2d
poly.Set(newP);
newP = null;
covered[index] = 1;
}
else
newP = null;
}
if (polyIndex < polysLength)
{
poly.MergeParallelEdges(Box2DSettings.b2_angularSlop);
//If identical points are present, a triangle gets
//borked by the MergeParallelEdges function, hence
//the vertex number check
if (poly.nVertices >= 3) polys.Add(poly);
//else printf("Skipping corrupt poly\n");
}
if (poly.nVertices >= 3) polyIndex++; //Must be outside (polyIndex < polysLength) test
}
//printf("MEMCHECK: %d\n",_CrtCheckMemory());
}
}
return polyIndex;
}
示例2: ConvexHull
public static Polygon ConvexHull(float[] cloudX, float[] cloudY)
{
int nVert = cloudX.Length;
int[] edgeList = new int[nVert];
int numEdges = 0;
float minY = Statics.FLT_MAX;
int minYIndex = nVert;
for (int i = 0; i < nVert; ++i)
{
if (cloudY[i] < minY)
{
minY = cloudY[i];
minYIndex = i;
}
}
int startIndex = minYIndex;
int winIndex = -1;
float dx = -1.0f;
float dy = 0.0f;
while (winIndex != minYIndex)
{
float newdx = 0.0f;
float newdy = 0.0f;
float maxDot = -2.0f;
float nrm;
for (int i = 0; i < nVert; ++i)
{
if (i == startIndex)
continue;
newdx = cloudX[i] - cloudX[startIndex];
newdy = cloudY[i] - cloudY[startIndex];
nrm = (float)Math.Sqrt(newdx * newdx + newdy * newdy);
nrm = (nrm == 0.0f) ? 1.0f : nrm;
newdx /= nrm;
newdy /= nrm;
//Cross and dot products act as proxy for angle
//without requiring inverse trig.
//FIXED: don't need cross test
//float newCross = newdx * dy - newdy * dx;
float newDot = newdx * dx + newdy * dy;
if (newDot > maxDot)
{//newCross >= 0.0f && newDot > maxDot) {
maxDot = newDot;
winIndex = i;
}
}
edgeList[numEdges++] = winIndex;
dx = cloudX[winIndex] - cloudX[startIndex];
dy = cloudY[winIndex] - cloudY[startIndex];
nrm = (float)Math.Sqrt(dx * dx + dy * dy);
nrm = (nrm == 0.0f) ? 1.0f : nrm;
dx /= nrm;
dy /= nrm;
startIndex = winIndex;
}
float[] xres = new float[numEdges];
float[] yres = new float[numEdges];
for (int i = 0; i < numEdges; i++)
{
xres[i] = cloudX[edgeList[i]];
yres[i] = cloudY[edgeList[i]];
//("%f, %f\n",xres[i],yres[i]);
}
Polygon returnVal = new Polygon(xres.ToList<float>(), yres.ToList<float>());
returnVal.MergeParallelEdges(Box2DSettings.b2_angularSlop);
return returnVal;
}