Implementing a WPF application in C# for Pixelsense.
I'm having difficulties with finding the specific points where a polygon is placed on the screen in order to detect polygon overlap (which is next stage). In total, I can have 7 polygons in my application and below is the code where I try and get their points.
What I thought the code would ideally do is start a loop, create a polygon (shape 1) and then start the second loop, create another polygon (shape 2) and get both the shapes points and check for overlap. Then the second loop would proceed to check if another shape (shape 3) overlaps with shape 1 by getting its points and so on... So the first polygon would get checked against the other 6 polygons for an overlap hence the nested loop.
However, what is happening currently is whatever points the method picks for the first polygon, they are replicated for the second polygon as well. So it prints out a set of duplicate points for the two separate polygons and loops forever.
I've been stuck on this for a while now, please please help me :)
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;
}
OUTPUT:
polygon with 4 points consists of: 713.345368087522,713.83644226973 , 550.136522242161,724.044829935686 , 463.067512500796,641.78273862321 , 626.276358346157,631.574350957254
polygon with 3 points consists of: 713.345368087522,713.83644226973 , 550.136522242161,724.044829935686 , 463.067512500796,641.78273862321
答案 0 :(得分:0)
根据我的评论,你的循环需要像这样排除:
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撰写。