goo*_*dev 2 c# algorithm math geometry line-intersection
假设我有这样一个多边形
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;
}
}
Run Code Online (Sandbox Code Playgroud)
假设我想绘制从一边到另一边穿过多边形的红线,如下图所示:
我只知道线应该站立的垂直位置,但是我怎么知道我应该从哪个水平点开始线以及哪个水平点结束线?
我的主要目标是知道线从哪个水平点开始和在哪个点结束,以便在这条线上安排文本。
如果线在几个地方穿过形状(如下图所示),我想获得所有线的数组:
请注意,形状可以由直线和拱形组成。
以下是 Adobe Illustrator 以形状排列文本的方式:
我如何在 C# 中做到这一点?
谢谢!
注意:对于赏金,请附上 C# 中的示例。
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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是结果: