daily pastebin goal
57%
SHARE
TWEET

Untitled

a guest Jan 20th, 2016 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. private double GetGeometryLength(Geometry geometry)
  2. {
  3.     double result = 0;
  4.     var pathGeometry = geometry as PathGeometry;
  5.     if (pathGeometry != null)
  6.     {
  7.         foreach (var figure in pathGeometry.Figures)
  8.         {
  9.             var currentPoint = figure.StartPoint;
  10.             foreach (var segment in figure.Segments)
  11.             {
  12.                 var bezier = segment as BezierSegment;
  13.                 var line = segment as LineSegment;
  14.  
  15.                 if (bezier != null)
  16.                 {
  17.                     result += GetBezierLength(currentPoint, bezier.Point1, bezier.Point2, bezier.Point3);
  18.                     currentPoint = bezier.Point3;
  19.                 }
  20.                 else if (line != null)
  21.                 {
  22.                     result += GetLineLength(currentPoint, line.Point);
  23.                     currentPoint = line.Point;
  24.                 }
  25.             }
  26.         }
  27.     }
  28.  
  29.     return result;
  30. }
  31.  
  32. private double GetBezierLength(Point p0, Point p1, Point p2, Point p3)
  33. {
  34.     double result = 0;
  35.     Point lastPoint = p0;
  36.  
  37.     for (double t = 0.001; t <= 1; t += 0.001)
  38.     {
  39.         Point currentPoint;
  40.  
  41.         // Формула кубической кривой Безье
  42.         // https://ru.wikipedia.org/wiki/Кривая_Безье
  43.         currentPoint.X = Math.Pow(1 - t, 3) * p0.X +
  44.             3 * t * Math.Pow(1 - t, 2) * p1.X +
  45.             3 * t * t * (1 - t) * p2.X +
  46.             Math.Pow(t, 3) * p3.X;
  47.  
  48.         currentPoint.Y = Math.Pow(1 - t, 3) * p0.Y +
  49.             3 * t * Math.Pow(1 - t, 2) * p1.Y +
  50.             3 * t * t * (1 - t) * p2.Y +
  51.             Math.Pow(t, 3) * p3.Y;
  52.  
  53.         double dx = currentPoint.X - lastPoint.X;
  54.         double dy = currentPoint.Y - lastPoint.Y;
  55.         result += Math.Sqrt(dx * dx + dy * dy);
  56.         lastPoint = currentPoint;
  57.     }
  58.  
  59.     return result;
  60. }
  61.  
  62. private double GetLineLength(Point p0, Point p1)
  63. {
  64.     double dx = p0.X - p1.X;
  65.     double dy = p0.Y - p1.Y;
  66.     return Math.Sqrt(dx * dx + dy * dy);
  67. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top