R
R
Rustam Khalilov2018-11-11 00:43:25
WPF
Rustam Khalilov, 2018-11-11 00:43:25

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

1 answer(s)
F
Foggy Finder, 2018-12-28
@FoggyFinder

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);

where p1 is the first polygon.
We do the same with the second, but then you already know
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 question

Ask a Question

731 491 924 answers to any question