Share:

Create Equal Distance Points On Path | WPF | Saatody | Amit Padhiyar

 In this post, I will show you about set points or object to path using WPF Geometry. If we use GeometryGroup then WPF able to set point on every geometry shape. For example: We take two lines and one ellipse geometry.

LineGeometry LGL = new LineGeometry();
EllipseGeometry EG = new EllipseGeometry();
LineGeometry LGR = new LineGeometry();
GeometryGroup GG = new GeometryGroup();

Now we add all geometry shapes in GeometryGroup.

GG.Children.Add(LGL);
GG.Children.Add(EG);
GG.Children.Add(LGR);

Add GeometryGroup in path data.

Path.Data = GG;

Use below algorithm to get points.

Point p;
Point tg;
var points = new List<Point>();
for (var i = 0; i < 100; i++)
{
    Path.Data.GetFlattenedPathGeometry().GetPointAtFractionLength(i / 100f, out p, out tg);
    points.Add(p);
    DrawFixtures(p.X, p.Y);
}

DrawFixtures function is below

public void DrawFixtures(double X, double Y)
{
    Ellipse Ellipse = new Ellipse();

    Ellipse.HorizontalAlignment = HorizontalAlignment.Left;
    Ellipse.VerticalAlignment = VerticalAlignment.Top;
    Ellipse.Margin = new Thickness(X - 10, Y - 10, 0, 0);
    Ellipse.Width = 20;
    Ellipse.Height = 20;
    Ellipse.Stroke = Brushes.Blue;
    Ellipse.StrokeThickness = 2;
    Ellipse.Fill = Brushes.Red;

    Container.Children.Add(Ellipse);
}

See other information on StackOverflow : https://stackoverflow.com/questions/25408250/getting-pixel-positions-for-all-points-in-a-wpf-path

Below part from StackOverflow

If you mean you want to find the center point along the path. I think we have to do something with the Path's Data which is actually a Geometry. A Geometry has a method called GetFlattenedGeometryPath returning a PathGeometry which has a method called GetPointAtFractionLength. So you can try something like this:

 Point centerPoint;
 Point tg;
 SomePath.Data.GetFlattenedGeometryPath()
              .GetPointAtFractionLength(0.5, out centerPoint, out tg);

If you mean you want to find all the points, I think it's a little abstract on this problem. Technically there are an infinite number of points. So you can just such as find 1000 points evenly scattered along the path using the following code:

Point p;
Point tg;
var points = new List<Point>();
for(var i = 0; i < 1000; i++){
  SomePath.Data.GetFlattenedGeometryPath()
               .GetPointAtFractionLength(i / 1000f, out p, out tg);
  points.Add(p);
}


Comments

Popular posts from this blog

Get Color From Pixel C# WPF | Saatody | Amit Padhiyar

Basic Audio Operations With MP3 And Wave Files Using NAudio C#

Create Drag And Drop Operation Between DevExpress GridControl And Custom WPF UI | Saatody | Amit Padhiyar