c# - 将凹面 PathGeometry 填充为凸面(找到凹面顶点并移除它们)的简单方法是什么?

标签 c# wpf geometry pathgeometry concave

我有一个 PathGeometry(多边形),它由一个 PathFigure 上的 LineSegments 组成,我想确保它是凸面的。我有一个方法使用 CrossProduct 来确定几何是否是凸的,我假设我可以返回一个点列表,当它为假时,它会成为凹面,并删除这些点以填充多边形,但它不能正常工作。

这是我得到的代码:

    public static bool IsConvexPolygon(this IList<Point> polygon, out List<Point> concavePoints)
    {
        int n = polygon.Count;
        List<double> result = new List<double>();
        concavePoints = new List<Point>();
        for (int i = 0; i < n; i++)
        {
            result.Add(polygon[i].CrossProduct(polygon[i.RotateNext(n)]));
            if (result.Last() < 0.0)
            {
                concavePoints.Add(polygon[i.RotateNext(n)]);
            }
        }
        return (result.All(d => d >= 0.0));
    }

    public static double CrossProduct(this Point p1, Point p2)
        {
            return (p1.X * p2.Y) - (p1.Y * p2.X);
        }

    public static int RotateNext(this int index, int count)
        {
            return (index + 1) % count;
        }

    public static PointCollection ExtractPoints(this Geometry geometry)
        {
            PointCollection pc = new PointCollection();
            if (geometry is LineGeometry)
            {
                var lg = (LineGeometry)geometry;
                pc.Add(lg.StartPoint);
                pc.Add(lg.EndPoint);
                return pc;
            }
            else if (geometry is PathGeometry)
            {
                var pg = (PathGeometry)geometry;
                if (pg.Figures.Count > 0)
                {
                    List<Point> points;
                    if ((pg.Figures[0].Segments.Count > 0) && (pg.Figures[0].Segments[0] is PolyLineSegment))
                        points = ((PolyLineSegment)pg.Figures[0].Segments[0]).Points.ToList();
                    else
                        points = pg.Figures[0].Segments.Select(seg => (seg as LineSegment).Point).ToList();

                    pc.Add(pg.Figures[0].StartPoint);
                    foreach (Point p in points)
                        pc.Add(p);
                    return pc;
                }
            }
            else if (geometry is RectangleGeometry)
            {
                var rg = (RectangleGeometry)geometry;
                var rect = rg.Rect;
                pc.Add(rect.TopLeft);
                pc.Add(rect.TopRight);
                pc.Add(rect.BottomRight);
                pc.Add(rect.BottomLeft);
                return pc;
            }
            return pc;
        }

public static Geometry CreateGeometryFromPoints(this List<Point> pts)
{
    if (pts.Count < 2)
        return null;

    PathFigure pFig = new PathFigure() { StartPoint = pts[0] };
    for (int i = 1; i < pts.Count; i++)
    {
        pFig.Segments.Add(new LineSegment(pts[i], true));
    }
    pFig.IsClosed = true;

    PathGeometry pg = new PathGeometry(new List<PathFigure>() { pFig });
    return pg;
}
public static Path CreatePolygonFromGeometry(this Geometry geo, Brush fillBrush)
        {
            Path path = new Path() { Stroke = Brushes.Black, StrokeThickness = 1, Fill = fillBrush };
            path.Data = geo;
            return path;
        }

这是我进行检查和更正多边形的地方:

        List<Point> outstuff;
        if (geo1.ExtractPoints().IsConvexPolygon(out outstuff) == false)
        {
            // Got to fill it in if it's concave
            var newpts = geo1.ExtractPoints().Except(outstuff).ToList();
            var z = newpts.CreateGeometryFromPoints().CreatePolygonFromGeometry(Brushes.Purple);
            z.MouseRightButtonDown += delegate { canvas.Children.Remove(z); };
            canvas.Children.Add(z);
        }

最终我希望能够像这样将我的凹面几何变成凸面:

alt text

最佳答案

我会计算 convex hull (也:NTS)并删除生成的凸包多边形内部的所有顶点(使用 point-in-polygon 测试)。

关于c# - 将凹面 PathGeometry 填充为凸面(找到凹面顶点并移除它们)的简单方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3208515/

相关文章:

c# - 如何运行 DropDownList 更改事件

c# - 隐藏部分面板C#

c# - readonly 关键字不会使 List<> 只读?

c# - 从全局X关闭按钮调用viewModel中定义的方法

c# - 将 Listbox.ItemContainerStyle 绑定(bind)到当前项目属性

geometry - 多边形 "sleeves"是如何调用的以及它们是如何计算的?

c# - 如何强制以父类(super class)形式放置控件?

javascript - 如何将 JS 函数的结果解析为 SVG 对象?

python - 为什么 Shapely 无法检测给定的地理坐标点是否在 route

wpf - 如何让winform运行在WPF上?