我用PathGeometry
画了一条线。通过获取几何长度的参考,我使用 GetFlattenedPathGeometry 方法获取路径长度,该方法将路径转换为一系列直线,并将线长度相加。引用的代码是
public static double GetLength(this Geometry geo)
{
PathGeometry path = geo.GetFlattenedPathGeometry();
double length = 0.0;
foreach (PathFigure pf in path.Figures)
{
Point start = pf.StartPoint;
foreach (PolyLineSegment seg in pf.Segments)
{
foreach (Point point in seg.Points)
{
length += Distance(start, point);
start = point;
}
}
}
return length;
}
private static double Distance(Point p1, Point p2)
{
return Math.Sqrt(Math.Pow(p1.X - p2.X,2) + Math.Pow(p1.Y - p2.Y,2));
}
有没有其他更好的方法来获取路径几何长度?
你可以尝试这样的事情:
public static double GetLengthOfGeo(Geometry geo)
{
PathGeometry pg = PathGeometry.CreateFromGeometry(geo);
Point p; Point tp;
pg.GetPointAtFractionLength(0.0001, out p, out tp);
return (pg.Figures[0].StartPoint - p).Length*10000;
}
毫不费力 您可以将System.Windows.Media.Geometry
转换为具有 ComputeLength 函数的SharpDX.Direct2D1.Geometry
:
计算几何图形的长度,就好像每个段都是 展开成一条线。