0

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

}
Liam McInroy
  • 4,339
  • 5
  • 32
  • 53
Rahul S Tomar
  • 121
  • 10
  • possible duplicate of [Draw a filled triangle in DrawingContext](http://stackoverflow.com/questions/4732869/draw-a-filled-triangle-in-drawingcontext) – user7116 Aug 04 '12 at 14:09

3 Answers3

3

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

steveg89
  • 1,807
  • 2
  • 15
  • 29
  • I tried that way also but this example for rendering the path uses UI framework element. I need to implement that using DrawingContext. I tried implementing the pathGeometry by adding pathGeometry.addGeometry(hexakey); and gave the fill rule as NonZero and then used this geometry in the drawingContext but still the result is same only outline is there. I want to fill the whole hexagon. – Rahul S Tomar Aug 02 '12 at 14:51
0

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

Nickon
  • 9,652
  • 12
  • 64
  • 119
0

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
                );
        }
Chris Kerekes
  • 1,116
  • 8
  • 27