private void CalculateRoundLines(CornersLine line1, CornersLine line2,
                                         CornersArc arc)
        {
            double f1   = Math.Atan2(line1.Y1 - line1.Y2, line1.X1 - line1.X2);
            double f2   = Math.Atan2(line2.Y2 - line2.Y1, line2.X2 - line2.X1);
            double alfa = f2 - f1;

            if ((alfa == 0) || (Math.Abs(alfa) == Math.PI))
            {
                addWithoutArc(arc);
            }
            else
            {
                addWithArc(line1, line2, arc, f1, f2, alfa);
            }
        }
        private void addWithArc(CornersLine line1, CornersLine line2, CornersArc arc, double f1, double f2, double alfa)
        {
            double s           = radius / Math.Tan(alfa / 2);
            double line1Length = Math.Sqrt(Math.Pow(line1.X1 - line1.X2, 2) + Math.Pow(line1.Y1 - line1.Y2, 2));
            double line2Length = Math.Sqrt(Math.Pow(line2.X1 - line2.X2, 2) + Math.Pow(line2.Y1 - line2.Y2, 2));
            double newRadius   = radius;

            if ((Math.Abs(s) > line1Length / 2) || (Math.Abs(s) > line2Length / 2))
            {
                if (s < 0)
                {
                    s = -Math.Min(line1Length / 2, line2Length / 2);
                }
                else
                {
                    s = Math.Min(line1Length / 2, line2Length / 2);
                }
                newRadius = s * Math.Tan(alfa / 2);
            }

            line1.NewX2 = line1.X2 + Math.Abs(s) * Math.Cos(f1);
            line1.NewY2 = line1.Y2 + Math.Abs(s) * Math.Sin(f1);
            line2.NewX1 = line2.X1 + Math.Abs(s) * Math.Cos(f2);
            line2.NewY1 = line2.Y1 + Math.Abs(s) * Math.Sin(f2);

            double circleCenterAngle = f1 + alfa / 2;
            double cs           = newRadius / Math.Sin(alfa / 2);
            PointF circleCenter = new PointF();

            if (s > 0)
            {
                circleCenter.X = (float)(line1.X2 + cs * Math.Cos(circleCenterAngle));
                circleCenter.Y = (float)(line1.Y2 + cs * Math.Sin(circleCenterAngle));
            }
            else
            {
                circleCenter.X = (float)(line1.X2 - cs * Math.Cos(circleCenterAngle));
                circleCenter.Y = (float)(line1.Y2 - cs * Math.Sin(circleCenterAngle));
            }

            double firstAngle  = Math.Atan2(line1.NewY2 - circleCenter.Y, line1.NewX2 - circleCenter.X);
            double secondAngle = Math.Atan2(line2.NewY1 - circleCenter.Y, line2.NewX1 - circleCenter.X);
            double startAngle  = firstAngle;
            double sweepAngle  = secondAngle - firstAngle;

            if (sweepAngle > Math.PI)
            {
                sweepAngle = -(2 * Math.PI - sweepAngle);
            }
            else
            {
                if (sweepAngle < -Math.PI)
                {
                    sweepAngle = (2 * Math.PI + sweepAngle);
                }
            }
            arc.X          = circleCenter.X - newRadius;
            arc.Y          = circleCenter.Y - newRadius;
            arc.Width      = newRadius * 2;
            arc.Height     = newRadius * 2;
            arc.StartAngle = startAngle * (180 / Math.PI);
            arc.SweepAngle = sweepAngle * (180 / Math.PI);
        }
        private void addWithArc(CornersLine line1, CornersLine line2, CornersArc arc, double f1, double f2, double alfa)
        {
            double s = radius / Math.Tan(alfa / 2);
            double line1Length = Math.Sqrt(Math.Pow(line1.X1 - line1.X2, 2) + Math.Pow(line1.Y1 - line1.Y2, 2));
            double line2Length = Math.Sqrt(Math.Pow(line2.X1 - line2.X2, 2) + Math.Pow(line2.Y1 - line2.Y2, 2));
            double newRadius = radius;

            if ((Math.Abs(s) > line1Length / 2) || (Math.Abs(s) > line2Length / 2))
            {
                if (s < 0)
                    s = -Math.Min(line1Length / 2, line2Length / 2);
                else
                    s = Math.Min(line1Length / 2, line2Length / 2);
                newRadius = s * Math.Tan(alfa / 2);
            }

            line1.NewX2 = line1.X2 + Math.Abs(s) * Math.Cos(f1);
            line1.NewY2 = line1.Y2 + Math.Abs(s) * Math.Sin(f1);
            line2.NewX1 = line2.X1 + Math.Abs(s) * Math.Cos(f2);
            line2.NewY1 = line2.Y1 + Math.Abs(s) * Math.Sin(f2);

            double circleCenterAngle = f1 + alfa / 2;
            double cs = newRadius / Math.Sin(alfa / 2);
            PointF circleCenter = new PointF();
            if (s > 0)
            {
                circleCenter.X = (float)(line1.X2 + cs * Math.Cos(circleCenterAngle));
                circleCenter.Y = (float)(line1.Y2 + cs * Math.Sin(circleCenterAngle));
            }
            else
            {
                circleCenter.X = (float)(line1.X2 - cs * Math.Cos(circleCenterAngle));
                circleCenter.Y = (float)(line1.Y2 - cs * Math.Sin(circleCenterAngle));
            }

            double firstAngle = Math.Atan2(line1.NewY2 - circleCenter.Y, line1.NewX2 - circleCenter.X);
            double secondAngle = Math.Atan2(line2.NewY1 - circleCenter.Y, line2.NewX1 - circleCenter.X);
            double startAngle = firstAngle;
            double sweepAngle = secondAngle - firstAngle;
            if (sweepAngle > Math.PI)
                sweepAngle = -(2 * Math.PI - sweepAngle);
            else
            {
                if (sweepAngle < -Math.PI)
                    sweepAngle = (2 * Math.PI + sweepAngle);
            }
            arc.X = circleCenter.X - newRadius;
            arc.Y = circleCenter.Y - newRadius;
            arc.Width = newRadius * 2;
            arc.Height = newRadius * 2;
            arc.StartAngle = startAngle * (180 / Math.PI);
            arc.SweepAngle = sweepAngle * (180 / Math.PI);
        }
 private void CalculateRoundLines(CornersLine line1, CornersLine line2,
     CornersArc arc)
 {
     double f1 = Math.Atan2(line1.Y1 - line1.Y2, line1.X1 - line1.X2);
     double f2 = Math.Atan2(line2.Y2 - line2.Y1, line2.X2 - line2.X1);
     double alfa = f2 - f1;
     if ((alfa == 0) || (Math.Abs(alfa) == Math.PI))
         addWithoutArc(arc);
     else
         addWithArc(line1, line2, arc, f1, f2, alfa);
 }