Justin CI
Justin CI

Reputation: 2741

Draw a circle in specified point inside a polygon

I have a polygon with points. I need to draw a circle in specified point inside a polygon. I tried Clip Property of ellipse but unable to get points of polygon.

XAML

<Border  x:Name="boardDiagramBorder"  BorderThickness="2.5" Margin="5" Background="Gray" >
                        <Grid  x:Name="boardCanvas">
                            <Polygon x:Name="polyOutLine" ClipToBounds="True"   Fill="Black"  
                                        Stroke="White"
                                        StrokeThickness="2"> 
                            </Polygon>

                        </Grid>
                    </Border>

Code to draw Polygon

 polyOutLine.Points.Add(new System.Windows.Point() { X = 0, Y = 0 });
            polyOutLine.Points.Add(new System.Windows.Point() { X = 118900, Y = 0 });
            polyOutLine.Points.Add(new System.Windows.Point() { X = 118900, Y = 62993 });
            polyOutLine.Points.Add(new System.Windows.Point() { X = 0, Y = 62993 });

I need to draw a circle in the point 21004,-57874.

Upvotes: 0

Views: 1435

Answers (2)

Eriawan Kusumawardhono
Eriawan Kusumawardhono

Reputation: 4896

You can't draw with precise coordinates within a Grid. You have to use Canvas to draw shapes, including rectangles, circles, and polygons using coordinates.

UPDATE: I extend my answer based on your question in your comment and I put in here.

Depends on the location of your canvas, because the circle will always have the relative coordinates based on your canvas.

For example, if your canvas is located at coordinate of 0,0 from the top left, then your circle will be located in coordinate relative to your Canvas with no further offset. If the Canvas is not positioned at 0,0 then your drawing will be located with the offset from the location of your Canvas.

Upvotes: 2

Prasanth V J
Prasanth V J

Reputation: 1196

You can use Canvas.Top and Canvas.Left properties to set the circle center, Or you can use margin property

<Grid>
        <Canvas>
            <Ellipse x:Name="innerCircle" Width="100"
                     Height="100"
                     Fill="#FFF4F4F5"
                     Stroke="Black"
                     Canvas.Left="50"
                     Canvas.Top="0"
                     />

            <Polygon x:Name="polyOutLine"
                     Stroke="Purple"
                     StrokeThickness="2">
                <Polygon.Fill>
                    <SolidColorBrush Opacity="0.4" Color="Blue" />
                </Polygon.Fill>
            </Polygon>
        </Canvas>
    </Grid>

To set circle center from code use follwing function

 private void SetCircleCenter(int x,int y)
 {
     double radius = innerCircle.Width / 2;
     innerCircle.Margin = new Thickness(x-radius, y-radius, 0, 0);
 }

Upvotes: 1

Related Questions