本文整理汇总了C#中Vertices.Insert方法的典型用法代码示例。如果您正苦于以下问题:C# Vertices.Insert方法的具体用法?C# Vertices.Insert怎么用?C# Vertices.Insert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Vertices
的用法示例。
在下文中一共展示了Vertices.Insert方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CalculateIntersections
/// <summary>
/// Calculates all intersections between two polygons.
/// </summary>
/// <param name="polygon1">The first polygon.</param>
/// <param name="polygon2">The second polygon.</param>
/// <param name="slicedPoly1">Returns the first polygon with added intersection points.</param>
/// <param name="slicedPoly2">Returns the second polygon with added intersection points.</param>
private static void CalculateIntersections(Vertices polygon1, Vertices polygon2,
out Vertices slicedPoly1, out Vertices slicedPoly2)
{
slicedPoly1 = new Vertices(polygon1);
slicedPoly2 = new Vertices(polygon2);
// Iterate through polygon1's edges
for (int i = 0; i < polygon1.Count; i++)
{
// Get edge vertices
Vector2 a = polygon1[i];
Vector2 b = polygon1[polygon1.NextIndex(i)];
// Get intersections between this edge and polygon2
for (int j = 0; j < polygon2.Count; j++)
{
Vector2 c = polygon2[j];
Vector2 d = polygon2[polygon2.NextIndex(j)];
Vector2 intersectionPoint;
// Check if the edges intersect
if (LineTools.LineIntersect(a, b, c, d, out intersectionPoint))
{
// calculate alpha values for sorting multiple intersections points on a edge
float alpha;
// Insert intersection point into first polygon
alpha = GetAlpha(a, b, intersectionPoint);
if (alpha > 0f && alpha < 1f)
{
int index = slicedPoly1.IndexOf(a) + 1;
while (index < slicedPoly1.Count &&
GetAlpha(a, b, slicedPoly1[index]) <= alpha)
{
++index;
}
slicedPoly1.Insert(index, intersectionPoint);
}
// Insert intersection point into second polygon
alpha = GetAlpha(c, d, intersectionPoint);
if (alpha > 0f && alpha < 1f)
{
int index = slicedPoly2.IndexOf(c) + 1;
while (index < slicedPoly2.Count &&
GetAlpha(c, d, slicedPoly2[index]) <= alpha)
{
++index;
}
slicedPoly2.Insert(index, intersectionPoint);
}
}
}
}
// Check for very small edges
for (int i = 0; i < slicedPoly1.Count; ++i)
{
int iNext = slicedPoly1.NextIndex(i);
//If they are closer than the distance remove vertex
if ((slicedPoly1[iNext] - slicedPoly1[i]).LengthSquared() <= ClipperEpsilonSquared)
{
slicedPoly1.RemoveAt(i);
--i;
}
}
for (int i = 0; i < slicedPoly2.Count; ++i)
{
int iNext = slicedPoly2.NextIndex(i);
//If they are closer than the distance remove vertex
if ((slicedPoly2[iNext] - slicedPoly2[i]).LengthSquared() <= ClipperEpsilonSquared)
{
slicedPoly2.RemoveAt(i);
--i;
}
}
}
示例2: SplitPolygonEdge
private static bool SplitPolygonEdge(Vertices polygon, EdgeAlignment edgeAlign, Vector2 coordInsideThePolygon,
out int vertex1Index, out int vertex2Index)
{
List<CrossingEdgeInfo> edges;
Vector2 slope;
int nearestEdgeVertex1Index = 0;
int nearestEdgeVertex2Index = 0;
bool edgeFound = false;
float shortestDistance = float.MaxValue;
bool edgeCoordFound = false;
Vector2 foundEdgeCoord = Vector2.Zero;
vertex1Index = 0;
vertex2Index = 0;
switch (edgeAlign)
{
case EdgeAlignment.Vertical:
edges = GetCrossingEdges(polygon, EdgeAlignment.Vertical, (int) coordInsideThePolygon.Y);
foundEdgeCoord.Y = coordInsideThePolygon.Y;
if (edges != null && edges.Count > 1 && edges.Count%2 == 0)
{
float distance;
for (int i = 0; i < edges.Count; i++)
{
if (edges[i].CrossingPoint.X < coordInsideThePolygon.X)
{
distance = coordInsideThePolygon.X - edges[i].CrossingPoint.X;
if (distance < shortestDistance)
{
shortestDistance = distance;
foundEdgeCoord.X = edges[i].CrossingPoint.X;
edgeCoordFound = true;
}
}
}
if (edgeCoordFound)
{
shortestDistance = float.MaxValue;
int edgeVertex2Index = polygon.Count - 1;
int edgeVertex1Index;
for (edgeVertex1Index = 0; edgeVertex1Index < polygon.Count; edgeVertex1Index++)
{
Vector2 tempVector1 = polygon[edgeVertex1Index];
Vector2 tempVector2 = polygon[edgeVertex2Index];
distance = LineTools.DistanceBetweenPointAndLineSegment(ref foundEdgeCoord,
ref tempVector1, ref tempVector2);
if (distance < shortestDistance)
{
shortestDistance = distance;
nearestEdgeVertex1Index = edgeVertex1Index;
nearestEdgeVertex2Index = edgeVertex2Index;
edgeFound = true;
}
edgeVertex2Index = edgeVertex1Index;
}
if (edgeFound)
{
slope = polygon[nearestEdgeVertex2Index] - polygon[nearestEdgeVertex1Index];
slope.Normalize();
Vector2 tempVector = polygon[nearestEdgeVertex1Index];
distance = LineTools.DistanceBetweenPointAndPoint(ref tempVector, ref foundEdgeCoord);
vertex1Index = nearestEdgeVertex1Index;
vertex2Index = nearestEdgeVertex1Index + 1;
polygon.Insert(nearestEdgeVertex1Index, distance*slope + polygon[vertex1Index]);
polygon.Insert(nearestEdgeVertex1Index, distance*slope + polygon[vertex2Index]);
return true;
}
}
}
break;
case EdgeAlignment.Horizontal:
throw new Exception("EdgeAlignment.Horizontal isn't implemented yet. Sorry.");
}
return false;
}
示例3: SplitPolygonEdge
private bool SplitPolygonEdge(Vertices polygon, Vector2 coordInsideThePolygon, out int vertex1Index, out int vertex2Index)
{
Vector2 slope;
int nearestEdgeVertex1Index = 0;
int nearestEdgeVertex2Index = 0;
bool edgeFound = false;
float shortestDistance = float.MaxValue;
bool edgeCoordFound = false;
Vector2 foundEdgeCoord = Vector2.Zero;
List<float> xCoords = SearchCrossingEdges(polygon, (int)coordInsideThePolygon.Y);
vertex1Index = 0;
vertex2Index = 0;
foundEdgeCoord.Y = coordInsideThePolygon.Y;
if (xCoords != null && xCoords.Count > 1 && xCoords.Count % 2 == 0)
{
float distance;
for (int i = 0; i < xCoords.Count; i++)
{
if (xCoords[i] < coordInsideThePolygon.X)
{
distance = coordInsideThePolygon.X - xCoords[i];
if (distance < shortestDistance)
{
shortestDistance = distance;
foundEdgeCoord.X = xCoords[i];
edgeCoordFound = true;
}
}
}
if (edgeCoordFound)
{
shortestDistance = float.MaxValue;
int edgeVertex2Index = polygon.Count - 1;
int edgeVertex1Index;
for (edgeVertex1Index = 0; edgeVertex1Index < polygon.Count; edgeVertex1Index++)
{
Vector2 tempVector1 = polygon[edgeVertex1Index];
Vector2 tempVector2 = polygon[edgeVertex2Index];
distance = LineTools.DistanceBetweenPointAndLineSegment(ref foundEdgeCoord,
ref tempVector1, ref tempVector2);
if (distance < shortestDistance)
{
shortestDistance = distance;
nearestEdgeVertex1Index = edgeVertex1Index;
nearestEdgeVertex2Index = edgeVertex2Index;
edgeFound = true;
}
edgeVertex2Index = edgeVertex1Index;
}
if (edgeFound)
{
slope = polygon[nearestEdgeVertex2Index] - polygon[nearestEdgeVertex1Index];
slope.Normalize();
Vector2 tempVector = polygon[nearestEdgeVertex1Index];
distance = Vector2.Distance(tempVector, foundEdgeCoord);
vertex1Index = nearestEdgeVertex1Index;
vertex2Index = nearestEdgeVertex1Index + 1;
polygon.Insert(nearestEdgeVertex1Index, distance * slope + polygon[vertex1Index]);
polygon.Insert(nearestEdgeVertex1Index, distance * slope + polygon[vertex2Index]);
return true;
}
}
}
return false;
}
示例4: PreparePolygons
/// <summary>
/// Prepares the polygons.
/// </summary>
/// <param name="polygon1">The polygon1.</param>
/// <param name="polygon2">The polygon2.</param>
/// <param name="poly1">The poly1.</param>
/// <param name="poly2">The poly2.</param>
/// <param name="intersections">The intersections.</param>
/// <param name="error">The error.</param>
/// <returns></returns>
private static int PreparePolygons(Vertices polygon1, Vertices polygon2, out Vertices poly1, out Vertices poly2,
out List<EdgeIntersectInfo> intersections, out PolyUnionError error)
{
error = PolyUnionError.None;
// Make a copy of the polygons so that we dont modify the originals, and
// force vertices to integer (pixel) values.
poly1 = Round(polygon1);
poly2 = Round(polygon2);
// Find intersection points
if (!VerticesIntersect(poly1, poly2, out intersections))
{
// No intersections found - polygons do not overlap.
error = PolyUnionError.NoIntersections;
return -1;
}
// Add intersection points to original polygons, ignoring existing points.
foreach (EdgeIntersectInfo intersect in intersections)
{
if (!poly1.Contains(intersect.IntersectionPoint))
{
poly1.Insert(poly1.IndexOf(intersect.EdgeOne.EdgeStart) + 1, intersect.IntersectionPoint);
}
if (!poly2.Contains(intersect.IntersectionPoint))
{
poly2.Insert(poly2.IndexOf(intersect.EdgeTwo.EdgeStart) + 1, intersect.IntersectionPoint);
}
}
// Find starting point on the edge of polygon1
// that is outside of the intersected area
// to begin polygon trace.
int startingIndex = -1;
int currentIndex = 0;
do
{
if (!PointInPolygonAngle(poly1[currentIndex], poly2))
{
startingIndex = currentIndex;
break;
}
currentIndex = poly1.NextIndex(currentIndex);
} while (currentIndex != 0);
// If we dont find a point on polygon1 thats outside of the
// intersect area, the polygon1 must be inside of polygon2,
// in which case, polygon2 IS the union of the two.
if (startingIndex == -1)
{
error = PolyUnionError.Poly1InsidePoly2;
}
return startingIndex;
}