gpt4 book ai didi

c# - 计算给定水平线处的形状宽度

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:40:30 27 4
gpt4 key购买 nike

假设我有这样一个多边形

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

enter image description here

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

enter image description here

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

我的主要目标是知道这条线在哪个水平点开始,在哪个水平点结束,以便在这条线上排列文本。

如果线在几个地方穿过形状(如下图),我想得到一个包含所有线的数组:

enter image description here

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

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

enter image description here

我如何在 C# 中执行此操作?

谢谢!

注意:如需赏金,请附上 C# 示例。

最佳答案

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

在这里,我简单地使用了 combination几何特征,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);
}
}
}
}

这是结果:

enter image description here

关于c# - 计算给定水平线处的形状宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46469581/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com