Search code examples
c#wpfellipsepathgeometry

Show Radius of Ellipse - WPF


I have an ellipse geometry. I want to show the radius of the ellipse by a line drawn from the center to the edge of the circle using C#. How can I achieve this?

Note: The center of the ellipse and the radius are not fixed and are defined by the user.

enter image description here


Solution

  • Suppose you have an ellipse with known center and radius:

            Path path = new Path();
            EllipseGeometry eg = new EllipseGeometry();
            eg.Center = new Point(left + side / 2, top + side / 2);
            eg.RadiusX = side / 2;
            eg.RadiusY = side / 2;
            path.Data = eg;
            paths.Add(path);
            canvas1.Children.Add(paths[paths.Count - 1]);
            .
            .
            path = new Path();
            borderColor.Color = Colors.Red;
            path.Stroke = borderColor;
            path.StrokeThickness = 2;
            LineGeometry r = new LineGeometry();
            r.StartPoint = eg.Center;
            r.EndPoint = new Point(eg.Center.X + eg.RadiusX, eg.Center.Y);
            path.Data = r;
            paths.Add(path);
            canvas1.Children.Add(paths[paths.Count - 1]);
    

    enter image description here