[英]To get length of WPF path
我使用PathGeometry
繪制了一條線。使用PathGeometry
Geometry長度的引用,我使用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));
}
還有其他更好的方法來獲取PathGeometry的長度嗎?
很少的努力,你可以轉換System.Windows.Media.Geometry
到SharpDX.Direct2D1.Geometry
有ComputeLength功能:
計算幾何的長度,就像每個段展開成一條線一樣。
你可以嘗試這樣的事情:
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.