获取一个的PathGeometry的长度(系)在C#/ WPF [英] Getting length of a PathGeometry (lines) in C#/WPF
问题描述
如果我有一个封闭路径,我可以使用 Geometry.GetArea()
逼近我的形状的面积。这是伟大的,为我节省了很多时间。但有周围很少会帮助我找到一个未闭合路径的长度?
If I have a closed path, I can use Geometry.GetArea()
to approximate the area of my shape. This is great and saves me a lot of time. But is there anything around that will help me find the length of a unclosed path?
我已经能够拿出现在最好的就是确保我现在用的PathGeometry
并调用 GetPointAtFractionLength
方法多次,获得积分,并添加了所有这些点之间的距离。
The best I've been able to come up with for now is to make sure I am using PathGeometry
and call the GetPointAtFractionLength
method multiple times, get the points and add up the distance between all those points.
代码:
public double LengthOfPathGeometry(PathGeometry path, double steps)
{
Point pointOnPath;
Point previousPointOnPath;
Point tangent;
double length = 0;
path.GetPointAtFractionLength(0, out previousPointOnPath, out tangent);
for (double progress = (1 / steps); progress < 1; progress += (1 / steps))
{
path.GetPointAtFractionLength(progress, out pointOnPath, out tangent);
length += Distance(previousPointOnPath, pointOnPath);
previousPointOnPath = pointOnPath;
}
path.GetPointAtFractionLength(1, out pointOnPath, out tangent);
length += Distance(previousPointOnPath, pointOnPath);
return length;
}
public static double Distance(Point p0, Point p1)
{
return Math.Sqrt((Math.Pow((p1.X - p0.X),2) + Math.Pow((p1.Y - p0.Y),2)));
}
使用(XAML):
<Path Stroke="Beige" StrokeThickness="5" x:Name="Robert">
<Path.Data>
<PathGeometry x:Name="Bob">
<PathGeometry.Figures>
<PathFigure StartPoint="20,10" IsClosed="False" IsFilled="False">
<PathFigure.Segments>
<BezierSegment
Point1="100,50"
Point2="100,200"
Point3="70,200"/>
<LineSegment Point="200,300" />
<ArcSegment
Size="50,50" RotationAngle="45"
IsLargeArc="True" SweepDirection="Counterclockwise"
Point="250,150"/>
<PolyLineSegment Points="450,75 190,100" />
<QuadraticBezierSegment Point1="50,250" Point2="180,70"/>
</PathFigure.Segments>
</PathFigure>
</PathGeometry.Figures>
</PathGeometry>
</Path.Data>
</Path>
使用(代码):
Usage (Code):
双长度= LengthOfPathGeometry(鲍勃,10000);
在这个例子中返回的结果应该在什么地方:1324.37
For this example the result returned should be somewhere around: 1324.37
这似乎迎刃而解,但有其缺陷。如果我想为一个非常大的行更准确的数字,我需要更多的步骤。如果你得到上面的步骤100000,您遇到很长的时间来近似。一对夫妇每次我的测试机上的方法调用秒。
This seems to work out fine, but has its flaws. If I want a more accurate number for a very large line, I need more steps. And if you get above 100000 steps, you run into a long time to approximate. A couple of seconds per method call on my test machine.
有谁知道一个更好的方式来近似直线的任何形状的长度是多少?
Does anyone know a better way to approximate a length of any shape of line?
推荐答案
有关更快的近似通话GetFlattenedPathGeometry,这将路径转换为一系列的直线,并增加了线路长度。
For a quicker approximation call GetFlattenedPathGeometry, which will convert your path to a series of straight lines, and add up the line lengths.
这是几乎在做同样的事情,现有的代码只是它更加智能地选择线段(如段的贝塞尔曲线分割成的数量取决于曲率),所以你'会拥有幅度较少的点订单相同的精度。
This is pretty much doing the same thing as your existing code except that it selects the line segments more intelligently (e.g. the number of segments a bezier curve is split into depends on the curvature) so you'll have orders of magnitude fewer points for the same accuracy.
这篇关于获取一个的PathGeometry的长度(系)在C#/ WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!