Intersection points of WPF geometries [duplicate]

I have two geometries (System.Windows.Media.Geometry) in WPF (mostly PathGeometries) and I need to know the positions where the two geometries intersect each other.

Is there a way of doing this directly without a workaround like using Geometry.GetWidenedPathGeometry() on both of the geometries and creating the intersections of these?

To be clear: I don't need the full intersection that I can get using a CombinedGeometry with GeometryCombineMode.Intersect, but the positions where the outlines of the geometries intersect.

The geometries are mostly path geometries containing multiple figures each. The code I use at the moment looks like the following example:

public static List<Point> GetIntersections(Geometry geo1, Geometry geo2)
{
  var pen = new Pen(Brushes.Black, 0.01);
  var path1 = geo1.GetOutlinedPathGeometry().GetWidenedPathGeometry(pen);
  var path2 = geo2.GetOutlinedPathGeometry().GetWidenedPathGeometry(pen);

  var combine = new CombinedGeometry(GeometryCombineMode.Intersect, path1, path2);
  var simplePath = combine.GetFlattenedPathGeometry();

  var results = new List<Point>();
  foreach (var figure in simplePath.Figures)
  {
    var figures = new[] { figure };
    var bounds = new PathGeometry(figures).Bounds;
    results.Add(new Point(bounds.Left + bounds.Width / 2.0,
                          bounds.Top + bounds.Height / 2.0));
  }
  return results;
}

From my point of view there must be a more elegant solution to this problem.



Read more here: https://stackoverflow.com/questions/68480607/intersection-points-of-wpf-geometries

Content Attribution

This content was originally published by Marc at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: