How to create a circle divided into equal parts in WPF and write text in each part using GeometryDrawing

338 Views Asked by At

I have a dynamic list of strings with length "n" and need to draw a circle divided into "n" number of equal parts and distribute the name in parts.

I found this Xaml piece of code that generates 3 parts but I can't figure how to do it programmatically

<Image Width="500" Height="500" Name="image1">
            <Image.Source>
                <DrawingImage>
                    <DrawingImage.Drawing>
                        <DrawingGroup>

                            <GeometryDrawing Brush="Red">
                                <GeometryDrawing.Pen>
                                    <Pen Brush="Black" />
                                </GeometryDrawing.Pen>
                                <GeometryDrawing.Geometry>
                                    <PathGeometry>
                                        <PathFigure StartPoint="100,100">
                                            <PathFigure.Segments>
                                                <LineSegment Point="100,0"/>
                                                <ArcSegment Point="186.6,150"  SweepDirection="Clockwise" Size="100,100"/>
                                                <LineSegment Point="100,100"/>
                                            </PathFigure.Segments>
                                        </PathFigure>
                                    </PathGeometry>
                                </GeometryDrawing.Geometry>
                            </GeometryDrawing>

                            <GeometryDrawing Brush="Blue">
                                <GeometryDrawing.Pen>
                                    <Pen Brush="Black"/>
                                </GeometryDrawing.Pen>
                                <GeometryDrawing.Geometry>
                                    <PathGeometry>
                                        <PathFigure StartPoint="100,100">
                                            <PathFigure.Segments>
                                                <LineSegment Point="186.6,150"/>
                                                <ArcSegment Point="13.4,150" SweepDirection="Clockwise" Size="100,100"/>
                                                <LineSegment Point="100,100"/>
                                            </PathFigure.Segments>
                                        </PathFigure>
                                    </PathGeometry>
                                </GeometryDrawing.Geometry>
                            </GeometryDrawing>

                            <GeometryDrawing Brush="Green">
                                <GeometryDrawing.Pen>
                                    <Pen Brush="Black"/>
                                </GeometryDrawing.Pen>
                                <GeometryDrawing.Geometry>
                                    <PathGeometry>
                                        <PathFigure StartPoint="100,100">
                                            <PathFigure.Segments>
                                                <LineSegment Point="13.4,150"/>
                                                <ArcSegment Point="100,0" SweepDirection="Clockwise" Size="100,100"/>
                                                <LineSegment Point="100,100"/>
                                            </PathFigure.Segments>
                                        </PathFigure>
                                    </PathGeometry>
                                </GeometryDrawing.Geometry>
                            </GeometryDrawing>

                        </DrawingGroup>
                    </DrawingImage.Drawing>
                </DrawingImage>
            </Image.Source>
        </Image>

Is it even a good way? if not, what is the best way to generate what I need?

What really confuses me is how to calculate the ArcSegment value?

            DrawingGroup drawingGroup = new DrawingGroup();
            var lastPoint = new Point(100, 0);
            for (int i = 0; i < 3; i++)
            {
                GeometryDrawing drawing = new GeometryDrawing();
                drawing.Brush = (Brush)new BrushConverter().ConvertFrom("#FF0000"); // TODO: change color

                drawing.Pen = new Pen
                {
                    Brush = (Brush)new BrushConverter().ConvertFrom("#000000")
                };

                PathSegment lineSegment1 = new LineSegment(lastPoint, true);
                lastPoint = new Point(200, 100); // TODO: calculate
                PathSegment arcSegment = new ArcSegment(lastPoint, new Size(100, 100), 0, false, SweepDirection.Clockwise, true);
                PathSegment lineSegment2 = new LineSegment(new Point(100, 100), true);
                PathFigure figure = new PathFigure(new Point(100, 100), new PathSegment[] { lineSegment1, arcSegment, lineSegment2 }, false);
                drawing.Geometry = new PathGeometry(new PathFigure[] { figure });

                drawingGroup.Children.Add(drawing);
            }

Thanks in advance.

0

There are 0 best solutions below