从画布上获取多边形的点
本文关键字:多边形 获取 | 更新日期: 2023-09-27 18:06:58
用c#实现一个Pixelsense的WPF应用程序。
我很难找到一个多边形放置在屏幕上的特定点,以检测多边形重叠(这是下一阶段)。总的来说,我可以在我的应用程序中有7个多边形,下面是我尝试获得它们的点的代码。
我认为代码将理想地做是开始一个循环,创建一个多边形(形状1),然后开始第二个循环,创建另一个多边形(形状2),并获得两个形状点和检查重叠。然后第二个循环将继续检查另一个形状(形状3)是否与形状1重叠,获取其点,以此类推……因此,第一个多边形将与其他6个多边形进行重叠检查,因此是嵌套循环。
然而,当前发生的情况是方法为第一个多边形选择的点,它们也被复制到第二个多边形。因此,它会为两个单独的多边形和循环永远打印出一组重复的点。
我已经被困在这个问题上一段时间了,请帮帮我:)
public void DefiningPolygonArea()
{
foreach (var polygonsCollection1 in this.PlayingCanvas.Children)
{
Polygon polygon1 = polygonsCollection1 as Polygon;
if (polygon1 != null)
{
foreach (var polygonsCollection2 in this.PlayingCanvas.Children)
{
Polygon polygon2 = polygonsCollection2 as Polygon;
if (polygon1 != polygon2 && polygon2 != null)
{
HelperMethods.PointCollectionsOverlap_Fast(getPoints(polygon1), getPoints(polygon2));
}
}
}
}
}
private List<Point> getPoints(Polygon poly)
{
if (poly.Points.Count == 4)
{
var transform = poly.TransformToVisual(this.PlayingCanvas);
Point point1 = transform.Transform(poly.Points[0]);
Point point2 = transform.Transform(poly.Points[1]);
Point point3 = transform.Transform(poly.Points[2]);
Point point4 = transform.Transform(poly.Points[3]);
area1.Add(point1);
area1.Add(point2);
area1.Add(point3);
area1.Add(point4);
Console.WriteLine("polygon with 4 points consists of: " + area1[0] + " , " + area1[1] + " , " + area1[2] + " , " + area1[3]);
}
else if (poly.Points.Count == 3)
{
var transform = poly.TransformToVisual(this.PlayingCanvas);
Point point1 = transform.Transform(poly.Points[0]);
Point point2 = transform.Transform(poly.Points[1]);
Point point3 = transform.Transform(poly.Points[2]);
area1.Add(point1);
area1.Add(point2);
area1.Add(point3);
Console.WriteLine("polygon with 3 points consists of: " + area1[0] + " , " + area1[1] + " , " + area1[2]);
}
return area1;
}
输出:4点多边形由:713.345368087522、713.83644226973、550.136522242161、724.044829935686、463.067512500796、641.78273862321、626.276358346157、631.574350957254组成
3点多边形由:713.345368087522、713.83644226973、550.136522242161、724.044829935686、463.067512500796、641.78273862321组成
根据我的评论,你的循环需要做一个排除,像这样:
foreach (var polygon1 in this.PlayingCanvas.Children.OfType<Polygon>())
foreach (var polygon2 in this.PlayingCanvas.Children.OfType<Polygon>().Where(x => x != polygon1))
HelperMethods.PointCollectionsOverlap_Fast(getPoints(polygon1), getPoints(polygon2));
用StackEdit编写。