我们说我有这样的多边形
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;
}
}
让我们说我想画出从一边到另一边穿过多边形的红线,如下图所示:
我只知道线应该站立的垂直位置,但是我怎么知道我应该从哪个水平点开始该线以及哪条水平点结束线?
我的主要目标是知道线在哪个水平点开始以及在哪个点结束,以便在此线上排列文本。
如果线在几个地方穿过形状(如下图所示),我想获得所有线的数组:
请注意,形状可以由直线和拱形组成。
以下是Adobe Illustrator如何将文本排列成形状:
如何在C#中执行此操作?
谢谢!
注意:要获得赏金,请在C#中附上一个示例。
答案 0 :(得分:4)
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);
}
}
}
}
结果如下:
答案 1 :(得分:1)
检查线路交叉点的代码:
git config merge.ff
检查与曲线的线交点的代码:
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);
}
答案 2 :(得分:-1)
注意:这个答案不是关于计算合适的线条大小(算术结果),而是关于仅在多边形上显示线条(视觉结果)。如果您需要数学,请更改问题上的标签。
您可以绘制线条的完整大小,并使用等于多边形的几何图形剪切它。假设您在名为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);
结合问题中提供的代码:
package test;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "DATAPACKET")
public class DataPacket {
@XmlAttribute(name = "REQUEST_ID")
private int REQUEST_ID;
@XmlElement(name ="BODY")
private Body BODY;
public DataPacket(){}
public int getREQUEST_ID() {
return REQUEST_ID;
}
public void setREQUEST_ID(int REQUEST_ID) {
this.REQUEST_ID = REQUEST_ID;
}
public Body getBODY() {
return BODY;
}
public void setBODY(Body BODY) {
this.BODY = BODY;
}
}
向Clemens WPF Clipping with a shape致信,了解从点创建几何的方法。
如果您打算绘制多条线,您可以选择在父面板上定义剪裁,这样内部的所有内容都将剪切到多边形边界。
答案 3 :(得分:-1)
我说你唯一的选择是测试红线是否与多边形的任何/某些段/弧相交。
蛮力是第一个想法。第二个想法是使用Fortune's algorithm
因为你的红线是水平的,这有点帮助。
如果通过Y最小坐标将多边形的部分存储在已排序的数组中,则可以从测试中跳过红线下方的所有部分。
通过二分查找很容易找到这个下限。
一些util链接:
How do you detect where two line segments intersect?
Line Segment Circle Intersection