0

我在使用此代码填充六边形时遇到问题,当此代码运行时,它仅绘制“白色”六边形的轮廓,我想用颜色填充六边形,但它不起作用。

我进行了很多搜索并尝试了很多东西,例如drawingContext.Drawing()drawingBrush等。

我在这段代码中遗漏了什么吗?这是代码:

public void DrawHexagon(DrawingContext drawingContext)
{
   GeometryGroup hexaKey = new GeometryGroup();

        //making lines for hexagon 
        hexaKey.Children.Add(
           new LineGeometry(new Point(X1, Y1), new Point(X2, Y2)));

        hexaKey.Children.Add(
            new LineGeometry(new Point(X2, Y2), new Point(X3, Y3)));

        hexaKey.Children.Add(
            new LineGeometry(new Point(X3, Y3), new Point(X4, Y4)));

        hexaKey.Children.Add(
            new LineGeometry(new Point(X4, Y4), new Point(X5, Y5)));

        hexaKey.Children.Add(
            new LineGeometry(new Point(X5, Y5), new Point(X6, Y6)));

        hexaKey.Children.Add(
            new LineGeometry(new Point(X6, Y6), new Point(X1, Y1)));

        //
        // Create a GeometryDrawing.
        //
        GeometryDrawing hexaKeyDrawing = new GeometryDrawing();
        hexaKeyDrawing.Geometry = hexaKey;

        // Paint the drawing with a gradient.
        hexaKeyDrawing.Brush =new SolidColorBrush(Colors.Red);


        // Outline the drawing with a solid color.
        hexaKeyDrawing.Pen = new Pen(Brushes.White, 2);


        drawingContext.DrawGeometry(hexaKeyDrawing.Brush, hexaKeyDrawing.Pen, hexaKeyDrawing.Geometry);

}
4

3 回答 3

3

LineGeometry 没有办法填充......它们只是线条。你需要一条路。MSDN有一个例子

于 2012-08-01T20:25:01.293 回答
0

一个例子,如何填充六边形: http: //www.codeproject.com/Articles/14948/Hexagonal-grid-for-games-and-other-projects-Part-1

于 2012-08-02T10:21:53.987 回答
0

在您的示例中,您在 a内有许多LineGeometry实例。GeometryGroupGeometryDrawing

首先,我会链接以提醒您注意您实际上并未使用GeometryDrawing. 在您的示例中,您仅将其用作GemetryGroup.

忽略这个问题是LineGeometry实例不打算绘制形状并且GeometryGroup无法意识到你的 6LineSegments一起形成一个封闭的形状。

但是不要绝望,因为有一种方法可以实现您想要的:PathGeometry. 这种几何形状本质上定义了可能被填充的区域的轮廓!该轮廓可以由起点和一系列PathSegments 定义。

private Point GetExtremity(Point center, double radius, double orientation)
        {
            return new Point(
                center.X + Math.Cos(orientation) * radius,
                center.Y + Math.Sin(orientation) * radius
                );
        }

        public void DrawUniformShape(DrawingContext context, Brush brush, Pen pen, Point center, double radius, int sides, double orientationRadians)
        {
            context.DrawGeometry(
                brush,
                pen,
                new PathGeometry(
                    Enumerable.Repeat(
                        new PathFigure(
                            GetExtremity(center, radius, orientationRadians),
                            from vertex in Enumerable.Range(1, sides - 1)
                            let angle = orientationRadians + vertex * 2 * Math.PI / sides
                            select new LineSegment(GetExtremity(center, radius, angle), true),
                            true
                            ),
                        1
                        )
                    )
                );
        }

        public void DrawBarnColouredHexagon(DrawingContext context, Point center, double radius, double orientation)
        {
            DrawUniformShape(
                context,
                Brushes.Red,
                new Pen(Brushes.White, 2),
                center,
                radius,
                6,
                0
                );
        }
于 2012-11-16T20:59:25.870 回答