Filling Color in a hexagon in WPF

你离开我真会死。 提交于 2019-12-25 05:05:19

问题


I'm having problem in filling the hexagon using this code, when this code runs it draws only the outline of the hexagon that is "White", I want to fill the hexagon with a color but it is not working.

I have searched a lot and tried many things like drawingContext.Drawing() , drawingBrush, etc.

Am I missing something in this code? This is the code:

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);

}

回答1:


LineGeometry doesn't have a way to fill... they're just lines. You need a path. The MSDN has an example




回答2:


An example, how to fill hexagons: http://www.codeproject.com/Articles/14948/Hexagonal-grid-for-games-and-other-projects-Part-1




回答3:


In your example you have a number of LineGeometry instances inside a GeometryGroup inside a GeometryDrawing.

First, I'd link to bring to your attention that you're not actually using the GeometryDrawing. In your example you've used it solely as a place holder for your GemetryGroup.

Ignoring this the problem is that LineGeometry instances were not intended to draw shapes and GeometryGroup is not able to realize that your 6 LineSegments together form a closed shape.

Despair not however as there is a way to accomplish what you want: PathGeometry. This geometry essentially defines the contour of a region that may be filled! This contour may is defined by a starting point and a series of 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
                );
        }


来源:https://stackoverflow.com/questions/11766954/filling-color-in-a-hexagon-in-wpf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!