Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Point geometry in WPF

I’m creating a geometry of lines and ellipses, now I want to add points to this geometry. I’m creating Ellipses with small radius to do this. But the problem is I’m applying transforms to this geometry. So when I translate and scale, the points get a lot bigger. How do I add points to this geometry such that it doesn’t gets bigger while scaling.

This is how I’m creating the point geometry:

public static Path PointsGeometry(List<Point> Locations, Transform transform)
    {
        GeometryGroup geometries = new GeometryGroup();
        foreach (Point Location in Locations)
        {
            geometries.Children.Add(new EllipseGeometry
                {
                    Center = Location,
                    RadiusX = .5,
                    RadiusY = .5,
                }
            );
        }

        geometries.Transform = transform;

        Path path = new Path
        {
            Fill = Brushes.WhiteSmoke,
            Data = geometries,
        };

        return path;
    }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In order to draw "dots", you may draw lines with zero length and round stroke caps:

public static Path PointsGeometry(
    IEnumerable<Point> locations, Transform transform)
{
    var geometries = new GeometryGroup { Transform = transform };

    foreach (Point location in locations)
    {
        geometries.Children.Add(new LineGeometry
        {
            StartPoint = location,
            EndPoint = location
        });
    }

    var path = new Path
    {
        Stroke = Brushes.WhiteSmoke,
        StrokeThickness = 2,
        StrokeStartLineCap = PenLineCap.Round,
        StrokeEndLineCap = PenLineCap.Round,
        Data = geometries,
    };

    return path;
}

Or with a PathGeometry instead of a GeometryGroup:

public static Path PointsGeometry(
    IEnumerable<Point> locations, Transform transform)
{
    var geometry = new PathGeometry { Transform = transform };

    foreach (Point location in locations)
    {
        var figure = new PathFigure { StartPoint = location };
        figure.Segments.Add(new LineSegment(location, true));
        geometry.Figures.Add(figure);
    }

    var path = new Path
    {
        Stroke = Brushes.WhiteSmoke,
        StrokeThickness = 2,
        StrokeStartLineCap = PenLineCap.Round,
        StrokeEndLineCap = PenLineCap.Round,
        Data = geometry,
    };

    return path;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading