Answer the question
In order to leave comments, you need to log in
C# WPF Graphics. Crossing shapes. How to cast Polygon to Geometry?
There are two polygons p1 and p2 . We need to find their intersection and display them on the screen.
The problem is that CombinedGeometry takes Geometry in its arguments.
Alas, I can't figure out how to bring "Polygon" to "Geometry".
Answer the question
In order to leave comments, you need to log in
Essentially your question contains two separate questions:
1. How to find the intersection of polygons.
2. How to convert Polygon to Geometry .
I answer the second one: The PathFigure
class is responsible for defining complex geometric shapes . You need to define the starting point and the sequence of segments (in our case - LineSegment )
var s1 = p1.Points.Skip(1).Select(p => new LineSegment { Point = p } );
var g1 = new PathFigure(p1.Points.First(), s1, true);
var path1 = new PathGeometry(new[] { g1 });
var path2 = new PathGeometry(new[] { g2 });
var path = new Path()
{
Data = new CombinedGeometry(GeometryCombineMode.Intersect, path1, path2),
Fill = Brushes.Green
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question