WPF 中的点几何

问题描述 投票:0回答:1

我正在创建直线和椭圆的几何图形,现在我想向该几何图形添加点。为此,我正在创建半径较小的椭圆。但问题是我正在对这个几何体应用变换。所以当我平移和缩放时,点会变得更大。如何向该几何体添加点,使其在缩放时不会变大。

这就是我创建点几何的方式:

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;
    }
c# .net wpf user-controls
1个回答
1
投票

为了绘制“点”,您可以绘制零长度和圆形笔划帽的线条:

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

    foreach (var 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;
}

或者使用 PathGeometry 而不是 GeometryGroup:

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

    foreach (var 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;
}
© www.soinside.com 2019 - 2024. All rights reserved.