2

假设我有这样一个多边形

public partial class Window2 : Window
{
    public Window2()
    {
        InitializeComponent();

        var myPolygon = new Polygon();
        myPolygon.Stroke = Brushes.Black;
        myPolygon.Fill = Brushes.LightSeaGreen;
        myPolygon.StrokeThickness = 2;

        myPolygon.Points = new PointCollection(new Point[] {
            new Point(50,50),
            new Point(50,165),
            new Point(140,165),
            new Point(140,120),
            new Point(70,120),
            new Point(80,70),
            new Point(140,70),
            new Point(140,50)
        });

        this.Content = myPolygon;
    }
}

在此处输入图像描述

假设我想绘制从一边到另一边穿过多边形的红线,如下图所示:

在此处输入图像描述

我只知道直线应该站立的垂直位置,但我怎么知道我应该从哪个水平点开始直线以及在哪个水平点结束直线呢?

我的主要目标是知道该行从哪个水平点开始以及在哪个点结束,以便在该行上安排文本。

如果线条在几个地方穿过形状(如下图所示),我想获得所有线条的数组:

在此处输入图像描述

请注意,该形状可以由直线和拱形组成。

以下是 Adob​​e Illustrator 如何在形状中排列文本:

在此处输入图像描述

我如何在 C# 中做到这一点?

谢谢!

注意:对于赏金,请附上 C# 中的示例。

4

4 回答 4

5

WPF 内置了很多算法,可以避免为像我这样的懒人编写复杂的算法。如果使用得当,Geometry类可以做很多事情,而且性能很好。所以你真的想开始使用几何而不是点集合或形状(它们是更多的 UI 实用程序)。

在这里,我简单地使用几何的组合特征,用于 4 行代码算法:

public static IEnumerable<Rect> ComputeIntersectingSegments(Geometry geometry, double y, double width)
{
    // Add a geometry line to compute intersections.
    // A geometry must not be 0 thickness for combination to be meaningful.
    // So we widen the line by a very small size
    var line = new LineGeometry(new Point(0, y), new Point(width, y)).GetWidenedPathGeometry(new Pen(null, 0.01));

    // Intersect the line with input geometry and compute intersections
    var combined = Geometry.Combine(line, geometry, GeometryCombineMode.Intersect, null);
    foreach (var figure in combined.Figures)
    {
        // the resulting figure can be a complex thing
        // we just want the bounding box
        yield return new PathGeometry(new PathFigure[] { figure }).Bounds;
    }
}

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        // use a canvas to display shape and intersections
        var canvas = new Canvas();
        Content = canvas;

        // your polygon can be built as a geometry for example like this:
        // var myPolygon = Geometry.Parse("M50,50 L50,165 L140,165 L140,120 L70,120 L80,70 L140,70 L140,50");

        // build a 'o' shape for testing, add it to the canvas
        var circle1 = new EllipseGeometry(new Point(100, 100), 70, 70);
        var circle2 = new EllipseGeometry(new Point(100, 100), 40, 40);

        // exclude mode will compute the 'o' shape ...
        var oGeometry = new CombinedGeometry(GeometryCombineMode.Exclude, circle1, circle2);

        var oPath = new Path();
        oPath.Stroke = Brushes.Black;
        oPath.Fill = Brushes.LightSeaGreen;
        oPath.StrokeThickness = 2;
        oPath.Data = oGeometry;

        canvas.Children.Add(oPath);

        // test many heights
        for (int y = 0; y < Height; y += 25)
        {
            foreach (var segment in ComputeIntersectingSegments(oGeometry, y, Width))
            {
                // for our sample, we add each segment to the canvas
                // Height is irrelevant, we use 2 for tests
                var line = new Rectangle();
                Canvas.SetLeft(line, segment.X);
                Canvas.SetTop(line, segment.Y);
                line.Width = segment.Width;
                line.Height = 2;
                line.Stroke = Brushes.Red;
                line.StrokeThickness = 1;
                canvas.Children.Add(line);
            }
        }
    }
}

这是结果:

在此处输入图像描述

于 2017-10-02T09:13:13.563 回答
1
  1. 你必须把形状分成直线和曲线
  2. 检查与您的红线相交的这些线/曲线下面提供的代码
  3. 最后你会得到至少两条线/曲线相交,所以你会知道红线的宽度。

检查线交叉点的代码:

    public static Point GetLineLineIntersections(
        Point start1, Point end1,
        Point start2, Point end2)
    {
        return GetLineLineIntersections(start1.X, start1.Y,
            end1.X, end1.Y,
            start2.X, start2.Y,
            end2.X, end2.Y);
    }

    public static Point GetLineLineIntersections(
        double x1, double y1,
        double x2, double y2,
        double x3, double y3,
        double x4, double y4)
    {
        double px = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) /
            ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4));

        double py = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) /
            ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4));

        return new Point(px, py);
    }

检查直线与曲线相交的代码:

    public static List<Point> GetLineCurveIntersections(
               Point curve1, Point curve2, Point curve3, Point curve4,
               Point lineStart, Point lineEnd)
    {
        var res = new List<Point>();

        var points = new List<Point>(new Point[] { curve1, curve2, curve3, curve4 });
        Rect rect = pointsBoundingRect(points);
        var rectData = new Tuple<Rect, List<Point>>(rect, points);
        var rectsData = new Queue<Tuple<Rect, List<Point>>>();
        rectsData.Enqueue(rectData);

        while (rectsData.Count != 0)
        {
            rectData = rectsData.Dequeue();
            rect = rectData.Item1;
            var controlPoints = rectData.Item2;
            if (!lineIntersectsRect(lineStart, lineEnd, rect))
                continue;

            if (isRectSmallEnough(rect))
            {
                res.Add(rect.Location);
                continue;
            }

            var pointsLeft = controlPointsForCurveInRange(0, 0.5, controlPoints);
            var pointsRight = controlPointsForCurveInRange(0.501, 1, controlPoints);
            var rectLeft = pointsBoundingRect(pointsLeft);
            var rectRight = pointsBoundingRect(pointsRight);

            rectsData.Enqueue(new Tuple<Rect, List<Point>>(rectLeft, pointsLeft));
            rectsData.Enqueue(new Tuple<Rect, List<Point>>(rectRight, pointsRight));
        }

        return res;
    }

    static Rect pointsBoundingRect(List<Point> points)
    {
        var xMin = points[0].X;
        var yMin = points[0].Y;
        var xMax = xMin;
        var yMax = yMin;

        for (var i = 0; i < points.Count; ++i)
        {
            var x = points[i].X;
            var y = points[i].Y;
            if (x < xMin)
                xMin = x;
            if (x > xMax)
                xMax = x;
            if (y < yMin)
                yMin = y;
            if (y > yMax)
                yMax = y;
        }

        return new Rect(new Point(xMax, yMax), new Point(xMin, yMin));
    }

    static bool lineIntersectsRect(Point lineStart, Point lineEnd, Rect rect)
    {
        var lineXmin = lineStart.X;
        var lineXmax = lineEnd.X;

        if (lineXmin > lineXmax)
        {
            lineXmin = lineEnd.X;
            lineXmax = lineStart.X;
        }

        if (lineXmax > rect.BottomRight.X)
            lineXmax = rect.BottomRight.X;

        if (lineXmin < rect.Location.X)
            lineXmin = rect.Location.X;

        if (lineXmin > lineXmax)
            return false;

        var minY = lineStart.Y;
        var maxY = lineEnd.Y;

        var dx = lineEnd.X - lineStart.X;
        if (Math.Abs(dx) > 0.000001)
        {
            //line equation
            var a = (lineEnd.Y - lineStart.Y) / dx;
            var b = lineStart.Y - a * lineStart.X;
            minY = a * lineXmin + b;
            maxY = a * lineXmax + b;
        }

        if (minY > maxY)
        {
            var tmp = minY;
            minY = maxY;
            maxY = tmp;
        }

        if (maxY > rect.BottomRight.Y)
            maxY = rect.BottomRight.Y;

        if (minY < rect.Location.Y)
            minY = rect.Location.Y;

        if (minY > maxY)
            return false;

        return true;
    }

    static bool isRectSmallEnough(Rect rect)
    {
        return rect.Width * rect.Height <= 1;
    }

    static Point calculatePointForParameters(double[] parameters, List<Point> controlPoints)
    {
        //De Casteljau's algorithm

        if (parameters.Length != (controlPoints.Count - 1))
        {
            throw new Exception("Invalid input(calculate curve point)");
        }

        if (controlPoints.Count == 1)
            return controlPoints[0];

        var points = controlPoints;
        var iteration = 0;
        while (points.Count != 1)
        {
            var t = parameters[iteration];
            var newPoints = new List<Point>();
            for (var i = 1; i < points.Count; ++i)
            {
                var x = (1 - t) * points[i - 1].X + t * points[i].X;
                var y = (1 - t) * points[i - 1].Y + t * points[i].Y;

                newPoints.Add(new Point(x, y));
            }

            ++iteration;
            points = newPoints;
        }

        return points[0];
    }

    static List<Point> controlPointsForCurveInRange(double tMin, double tMax, List<Point> points)
    {
        var controlPoints = new List<Point>();
        var pointsCount = points.Count;

        var parameters = new double[pointsCount - 1];
        for (var i = 0; i < pointsCount; ++i)
        {
            parameters.Fill(tMin, 0, parameters.Length - i);
            parameters.Fill(tMax, parameters.Length - i, pointsCount);
            var newPoint = calculatePointForParameters(parameters, points);
            controlPoints.Add(newPoint);
        }

        return controlPoints;
    }

public static class Ex
{
    public static void Fill<T>(this IList<T> list, T value, int start, int end)
    {
        end = Math.Min(list.Count, end);
        for (int i = start; i < end; ++i)
        {
            list[i] = value;
        }
    }
}
于 2017-10-01T20:17:00.663 回答
-1

注意:这个答案不是关于计算适当的线条大小(算术结果),而是关于仅在多边形上显示线条(视觉结果)。如果您需要数学,请更改问题上的标签。

您可以绘制完整尺寸的线并使用等于多边形的几何图形对其进行剪辑。假设您将多边形和线托管在名为的网格中grid1

private void DrawLine(Polygon myPolygon, int linePos)
{

    var clip = new StreamGeometry();
    using (var context = clip.Open())
    {
        context.BeginFigure(myPolygon.Points.First(), true, true);
        context.PolyLineTo(myPolygon.Points.Skip(1).ToList(), true, false);
    }
    var line = new Line()
    {
        X1 = 0,
        X2 = Width,
        Y1 = linePos,
        Y2 = linePos,
        Stroke = Brushes.Red,
        StrokeThickness = 2,
        Clip = clip
    };

    grid1.Children.Add(line);
}

结合问题中提供的代码:

var myPolygon = new Polygon();
myPolygon.Stroke = Brushes.Black;
myPolygon.Fill = Brushes.LightSeaGreen;
myPolygon.StrokeThickness = 2;

myPolygon.Points = new PointCollection(new Point[]
{
    new Point(50,50),
    new Point(50,165),
    new Point(140,165),
    new Point(140,120),
    new Point(70,120),
    new Point(80,70),
    new Point(140,70),
    new Point(140,50)
});

grid1.Children.Add(myPolygon);

DrawLine(myPolygon, 80);
DrawLine(myPolygon, 150);

归功于 Clemens WPF Clipping 的形状,用于从点创建几何图形。

如果您计划绘制多条线,您可以选择在父面板上定义一个剪裁,这样里面的所有东西都将被剪裁到多边形边界。

于 2017-09-28T13:25:17.303 回答
-1

我想说你唯一的选择是测试红线是否与多边形的任何/某些段/弧相交。

蛮力是第一个想法。第二个想法是使用财富的算法

因为你的红线是水平的,所以这有点帮助。

如果您将多边形的片段存储在按 Y 最小坐标排序的数组中,则可以从测试中跳过红线以下的所有片段。
这个下限很容易通过二分搜索找到。

一些实用链接:
如何检测两条线段相交的位置?
线段圆交点

于 2017-09-28T23:05:46.953 回答