닫힌 경로가있는 경우 Geometry.GetArea()
을 사용하여 내 셰이프 영역을 근사 할 수 있습니다. 이것은 위대하며 많은 시간을 절약 해줍니다. 그러나 주변에 숨겨진 길의 길이를 찾는 데 도움이 될만한 것이 있습니까?C#/WPF의 PathGeometry (선) 길이 가져 오기
제가 지금 생각해 낼 수 있었던 가장 좋은 점은 PathGeometry
을 사용하고 GetPointAtFractionLength
메서드를 여러 번 호출하고 점을 얻은 다음 모든 점 사이의 거리를 더하는 것입니다.
코드 :
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>
사용 (코드) :
double length = LengthOfPathGeometry(Bob, 10000);
결과가 어딘가에 있어야한다 반환이 예를 들어 약 1324.37
이것은 잘 작동하는 것으로 보이지만 그 결함이 있습니다. 매우 큰 행에 대해 더 정확한 숫자를 원한다면 더 많은 단계가 필요합니다. 그리고 만약 당신이 100000 단계를 넘으면, 당신은 대략적으로 오랜 시간이 걸립니다. 테스트 머신에서 메소드 호출 당 2 초.
누구든지 선의 모양을 대략적으로 계산하는 더 좋은 방법을 알고 있습니까?
정확히 내가 무엇을 찾고 있었습니까. 고맙습니다. –