using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Xml;
using System.Diagnostics;
namespace Math3
{
    public class classMath3
    {

        public static double dblTwoPi = 2.0 * Math.PI;
        public static double dblHalfPi = Math.PI / 2.0;
  
        public static void cleanAngle(ref double dblAngle)
        {
            dblAngle = cleanAngle(dblAngle);
        }

        public static double cleanAngle(double dblAngle)
        {
            while (dblAngle > TwoPi) dblAngle -= TwoPi;
            while (dblAngle < 0) dblAngle += TwoPi;
            if (double.IsNaN(dblAngle))
                dblAngle = 0;

            return dblAngle;
        }

        public static double ConvertRadiansToDegrees(double dblRadians)
        {
            return 180.0 * dblRadians / Math.PI;
        }

        public static double ConvertDegreesToRadians(double dblDegrees)
        {
            return dblDegrees / 180.0 * Math.PI;
        }

        public static double ConvertDegreesToRadians(int intDegrees)
        {
            return (double)intDegrees / 180.0 * Math.PI;
        }

        public static double RoundNearest(double dblIn, int intDecimalPoint)
        {
            dblIn = dblIn * Math.Pow(10, intDecimalPoint);

            double dblRetVal = (double)(int)dblIn;
            double dblDecimal = dblIn - dblRetVal;
            if (dblDecimal >= 0.5) dblRetVal++;

            dblRetVal = dblRetVal / Math.Pow(10, intDecimalPoint);
            return dblRetVal;
        }

        public static double DotProduct(Point pt1, Point pt2)
        {
            return pt1.X * pt2.X + pt1.Y * pt2.Y;
        }
        public static double DotProduct(PointF ptf1, PointF ptf2)
        {
            return ptf1.X * ptf2.X + ptf1.Y * ptf2.Y;
        }

        public static classPointF3D CrossProduct(classPointF3D A, classPointF3D B)
        {
            return new classPointF3D(A.Y * B.Z - A.Z * B.Y, A.Z * B.X - A.X * B.Z, A.X * B.Y - A.Y * B.X);
        }
        public static double Get_ShortestDistancePointLine(classLineF cLine, PointF ptf, ref PointF ptfNearestOnLine)
        {
            // calculate a vector from 1st pt on line to center of circle 
            PointF ptfA = new PointF(ptf.X - cLine.ptf1.X, ptf.Y - cLine.ptf1.Y);

            // calculate unit normal to line => unit-B = B / |B|
            classRadialCoordinate cRad_Line = new classRadialCoordinate(cLine.ptf2, cLine.ptf1);
            classRadialCoordinate cRad_Normal = new classRadialCoordinate(cRad_Line.Radians + Math.PI / 2, cRad_Line.Magnitude);

            PointF ptfNormal = cRad_Normal.toPointF();
            classLineF cLineNormal = new classLineF(ptf, AddTwoPointFs(ptfNormal, ptf));
            cRad_Normal.Magnitude = 1;

            PointF ptfBUnit = cRad_Normal.toPointF();
            double dblRetVal = Math.Abs(DotProduct(ptfBUnit, ptfA));

            /*          calculating the nearest PointF on line
             line eq'n ->   y = Mx + B
                            at intersection between perpendicular line2 through ball and line1
                            both lines meet at some PointF (x,y)
             normal to M = -1/M
             therefore      y = M1x + B1 = (-1/M1x) + B2

             therefore       x = (b2-b1) / (M1 + 1/M1)

             B2 = y - M2x 
                = y + x/M1  - true for any PointF(x,y) on line2 (e.g. center of the circle)
             */
            cLine.IntersectsLine(ref cLineNormal, ref ptfNearestOnLine);

            return dblRetVal;
        }

        public static double Get_ShortestDistanceCircleLine(classLineF cLine, classCircle cCircle, ref PointF ptfNearestOnLine)
        {
            // calculate a vector from 1st pt on line to center of circle 
            PointF ptfA = new PointF(cCircle.ptfCenter.X - cLine.ptf1.X, cCircle.ptfCenter.Y - cLine.ptf1.Y);

            // calculate unit normal to line => unit-B = B / |B|
            classRadialCoordinate cRad_Line = new classRadialCoordinate(cLine.ptf2, cLine.ptf1);
            classRadialCoordinate cRad_Normal = new classRadialCoordinate(cRad_Line.Radians + Math.PI / 2, cRad_Line.Magnitude);

            PointF ptfNormal = cRad_Normal.toPointF();
            classLineF cLineNormal = new classLineF(cCircle.ptfCenter, AddTwoPointFs(ptfNormal, cCircle.ptfCenter));
            cRad_Normal.Magnitude = 1;

            PointF ptfBUnit = cRad_Normal.toPointF();
            double dblRetVal = Math.Abs(DotProduct(ptfBUnit, ptfA));

            /*          calculating the nearest PointF on line
             line eq'n ->   y = Mx + B
                            at intersection between perpendicular line2 through ball and line1
                            both lines meet at some PointF (x,y)
             normal to M = -1/M
             therefore      y = M1x + B1 = (-1/M1x) + B2

             therefore       x = (b2-b1) / (M1 + 1/M1)

             B2 = y - M2x 
                = y + x/M1  - true for any PointF(x,y) on line2 (e.g. center of the circle)
             */
            cLine.IntersectsLine(ref cLineNormal, ref ptfNearestOnLine);

            return dblRetVal;
        }

        public static int Get_Sign(int intValue)
        {
            if (intValue == 0) return 0;
            else return (int)intValue / Math.Abs(intValue);
        }

        public static int Get_Sign(double dblValue)
        {
            if (dblValue == 0) return 0;
            else return (int)(dblValue / Math.Abs(dblValue));
        }
        public static bool SameSign(int int1, int int2) { return ((int1 < 0 && int2 < 0) || (int1 > 0 && int2 > 0)); }

        public static bool SameSign(double dbl1, double dbl2) { return ((dbl1 < 0 && dbl2 < 0) || (dbl1 > 0 && dbl2 > 0)); }

        public static bool RectanglesIntersect(RectangleF rec1, RectangleF rec2)
        {
            bool bolHorizontal = (rec1.Left >= rec2.Left && rec1.Right < rec2.Right)             // rec 1 inside rec2
                              || (rec1.Left <= rec2.Left && rec1.Right > rec2.Right)             // rec 1 envelopes rec2
                              || (rec1.Left <= rec2.Left && rec1.Right > rec2.Left)              // rec 1 straddles rec2's left side
                              || (rec1.Left <= rec2.Right && rec1.Right > rec2.Right);           // rec 1 straddles rec2's right side

            bool bolVertical = (rec1.Top >= rec2.Top && rec1.Bottom < rec2.Bottom)               // rec 1 inside rec2
                              || (rec1.Top <= rec2.Top && rec1.Bottom > rec2.Bottom)             // rec 1 envelopes rec2
                              || (rec1.Top <= rec2.Top && rec1.Bottom > rec2.Top)                // rec 1 straddles rec2's Top side
                              || (rec1.Top <= rec2.Bottom && rec1.Bottom > rec2.Bottom);         // rec 1 straddles rec2's Bottom side
            return bolVertical && bolHorizontal;
        }
        public static bool RectanglesIntersect(Rectangle rec1, Rectangle rec2)
        {
            bool bolHorizontal = (rec1.Left >= rec2.Left && rec1.Right <= rec2.Right)             // rec 1 inside rec2
                              || (rec1.Left <= rec2.Left && rec1.Right >= rec2.Right)             // rec 1 envelopes rec2
                              || (rec1.Left <= rec2.Left && rec1.Right >= rec2.Left)              // rec 1 straddles rec2's left side
                              || (rec1.Left < rec2.Right && rec1.Right >= rec2.Right);           // rec 1 straddles rec2's right side

            bool bolVertical = (rec1.Top >= rec2.Top && rec1.Bottom <= rec2.Bottom)               // rec 1 inside rec2
                              || (rec1.Top <= rec2.Top && rec1.Bottom >= rec2.Bottom)             // rec 1 envelopes rec2
                              || (rec1.Top <= rec2.Top && rec1.Bottom >= rec2.Top)                // rec 1 straddles rec2's Top side
                              || (rec1.Top <= rec2.Bottom && rec1.Bottom >= rec2.Bottom);         // rec 1 straddles rec2's Bottom side
            return bolVertical && bolHorizontal;
        }
        public static bool RectanglesIntersect(Rectangle rec1, RectangleF rec2)
        {
            bool bolHorizontal = (rec1.Left >= rec2.Left && rec1.Right < rec2.Right)             // rec 1 inside rec2
                              || (rec1.Left <= rec2.Left && rec1.Right > rec2.Right)             // rec 1 envelopes rec2
                              || (rec1.Left <= rec2.Left && rec1.Right > rec2.Left)              // rec 1 straddles rec2's left side
                              || (rec1.Left <= rec2.Right && rec1.Right > rec2.Right);           // rec 1 straddles rec2's right side

            bool bolVertical = (rec1.Top >= rec2.Top && rec1.Bottom < rec2.Bottom)               // rec 1 inside rec2
                              || (rec1.Top <= rec2.Top && rec1.Bottom > rec2.Bottom)             // rec 1 envelopes rec2
                              || (rec1.Top <= rec2.Top && rec1.Bottom > rec2.Top)                // rec 1 straddles rec2's Top side
                              || (rec1.Top <= rec2.Bottom && rec1.Bottom > rec2.Bottom);         // rec 1 straddles rec2's Bottom side
            return bolVertical && bolHorizontal;
        }

        public static Rectangle RectangleFromTwoPoints(Point pt1, Point pt2)
        {
            return new Rectangle(pt1.X <= pt2.X ? pt1.X : pt2.X,
                                 pt1.Y <= pt2.Y ? pt1.Y : pt2.Y,
                                (int)Math.Abs(pt1.X - pt2.X),
                                (int)Math.Abs(pt1.Y - pt2.Y));
        }

        public static Rectangle RectangleFromTwoPointFs(PointF pt1, PointF pt2)
        {
            return new Rectangle(pt1.X <= pt2.X ? (int)pt1.X : (int)pt2.X,
                                 pt1.Y <= pt2.Y ? (int)pt1.Y : (int)pt2.Y,
                                 (int)Math.Abs(pt1.X - pt2.X),
                                 (int)Math.Abs(pt1.Y - pt2.Y));
        }

        public static RectangleF RectangleFFromTwoPoints(Point pt1, Point pt2)
        {
            return new RectangleF(pt1.X <= pt2.X ? pt1.X : pt2.X,
                                 pt1.Y <= pt2.Y ? pt1.Y : pt2.Y,
                                (int)Math.Abs(pt1.X - pt2.X),
                                (int)Math.Abs(pt1.Y - pt2.Y));

        }

        public static RectangleF RectangleThatContainsAllPoints(List<PointF> lstPtf)
        {
            IEnumerable<PointF> query_X = lstPtf.OrderBy(ptf => ptf.X);
            List<PointF> lstPtf_XOrder = (List<PointF>)query_X.ToList<PointF>();

            IEnumerable<PointF> query_Y = lstPtf.OrderBy(ptf => ptf.Y);
            List<PointF> lstPtf_YOrder = (List<PointF>)query_Y.ToList<PointF>();

            return new RectangleF(new PointF(lstPtf_XOrder[0].X, lstPtf_YOrder[0].Y), new SizeF(lstPtf_XOrder[lstPtf_XOrder.Count - 1].X - lstPtf_XOrder[0].X, lstPtf_YOrder[lstPtf_YOrder.Count - 1].Y - lstPtf_YOrder[0].Y));
        }

        public static RectangleF RectangleFFromTwoPointFs(PointF pt1, PointF pt2)
        {
            return new RectangleF(pt1.X <= pt2.X ? (float)pt1.X : (float)pt2.X,
                                 pt1.Y <= pt2.Y ? (float)pt1.Y : (float)pt2.Y,
                                 (float)Math.Abs(pt1.X - pt2.X),
                                 (float)Math.Abs(pt1.Y - pt2.Y));

        }

        /// <summary>
        /// adds an ellipse to a graphics path and returns a description of the same ellipse
        /// </summary>
        /// <param name="grPath">graphic path into which the new graphic ellipse is added</param>
        /// <param name="bolClockwise">rotational direction of arc of the ellipse to be added</param>
        /// <param name="pt0">starting point of the ellipse added to the graphics path</param>
        /// <param name="pt1">end point of the ellipse added to the graphics path</param>
        /// <param name="intSegmentHeight">the height of the ellipse measured as the shortest distance between the output ellipse arc and a point located equidistant from the start and end points</param>
        /// <returns>a description of the arc added to the graphics path</returns>
        public static classGraphicArcDescription GraphicsPath_Add(ref GraphicsPath grPath, bool bolClockwise, PointF pt0, Point pt1, int intSegmentHeight)
        {
            double dblArc_Height = (double)intSegmentHeight;
            classRadialCoordinate cRad = new classRadialCoordinate(pt0, pt1);
            double dblArc_Width = cRad.Magnitude;

            PointF ptfBetweenInputs = new PointF((pt0.X + (pt1.X - pt0.X) / 2), (pt0.Y + (pt1.Y - pt0.Y) / 2));
            float fltBaseAngle = (float)(cRad.Degrees
                                        + (bolClockwise ? -90 : 90));

            if (bolClockwise)
                cRad.Radians -= Math.PI / 2;
            else
                cRad.Radians += Math.PI / 2;

            //https://www.mathopenref.com/arcradius.html 
            // R = h/2 + w^2/(8h)
            double dblRadius = dblArc_Height / 2 + Math.Pow(dblArc_Width, 2) / (8 * dblArc_Height);
            double dblTheta = Math.Asin((dblArc_Width / 2) / dblRadius);
            float fltDegree_Start = fltBaseAngle - (bolClockwise ? 1 : -1) * (float)(180.0 / Math.PI * dblTheta);
            float fltDegree_Sweep = (bolClockwise ? 1 : -1) * (float)(360.0 / Math.PI * dblTheta);
            cRad.Magnitude = dblRadius - dblArc_Height;
            cRad.Radians += Math.PI;
            PointF ptfCenterEllipse = AddTwoPointFs(ptfBetweenInputs, cRad.toPointF());
            RectangleF recEllipse = new RectangleF((float)ptfCenterEllipse.X - (float)dblRadius, (float)ptfCenterEllipse.Y - (float)dblRadius, (2.0f * (float)dblRadius), (2.0f * (float)dblRadius));

            classGraphicArcDescription cRetVal = new classGraphicArcDescription(recEllipse, fltDegree_Start, fltDegree_Sweep);

            grPath.AddArc(recEllipse, fltDegree_Start, fltDegree_Sweep);

            return cRetVal;
        }

        public class classGraphicArcDescription
        {
            public RectangleF rec = new RectangleF();
            public float flt_Degree_start = 0;
            public float flt_Degree_Sweep = 0;
            public classGraphicArcDescription(RectangleF _rec, float _flt_Degree_start, float _flt_Degree_Sweep)
            {
                rec = _rec;
                flt_Degree_start = _flt_Degree_start;
                flt_Degree_Sweep = _flt_Degree_Sweep;
            }
        }
        

        public static bool CircleIntersectaPolygon(ref PointF[] ptfPolygon, classCircle cCircle)
        {
            for (int intPtfCounter = 0; intPtfCounter < ptfPolygon.Length; intPtfCounter++)
            {
                PointF ptfStart = ptfPolygon[intPtfCounter];
                PointF ptfEnd = ptfPolygon[(intPtfCounter + 1) % ptfPolygon.Length];

                classLineF cLineF = new classLineF(ptfStart, ptfEnd);
                PointF ptfNearestToLine = new PointF();
                Get_ShortestDistanceCircleLine(cLineF, cCircle, ref ptfNearestToLine);
                if (Math.Abs(ptfNearestToLine.X - cCircle.ptfCenter.X) < cCircle.dblRadius && Math.Abs(ptfNearestToLine.Y - cCircle.ptfCenter.Y) < cCircle.dblRadius)
                    return true;
            }
            return false;
        }

        public static Point Get_PointFromElement(XmlNode xPt)
        {
            int intX = 0, intY = 0;
            try
            {
                intX = Convert.ToInt32(xPt.Attributes[0].InnerText);
                intY = Convert.ToInt32(xPt.Attributes[1].InnerText);

            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show("Error:" + e.Message);
            }

            return new Point(intX, intY);
        }

        const string conXmlField_Point_X = "X";
        const string conXmlField_Point_Y = "Y";
        public static XmlNode Get_NodeFromPointF(ref XmlDocument xDoc, string strName, PointF ptf)
        {
            XmlElement xRetVal = xDoc.CreateElement(strName);

            XmlNode xAtt_X = xDoc.CreateNode(XmlNodeType.Attribute, conXmlField_Point_X, "pt");
            XmlNode xAtt_Y = xDoc.CreateNode(XmlNodeType.Attribute, conXmlField_Point_Y, "pt");

            xAtt_X.Value = ptf.X.ToString();
            xAtt_Y.Value = ptf.Y.ToString();

            xRetVal.Attributes.SetNamedItem(xAtt_X);
            xRetVal.Attributes.SetNamedItem(xAtt_Y);

            return xRetVal;
        }
        public static XmlNode Get_NodeFromPoint(ref XmlDocument xDoc, string strName, Point pt)
        {
            XmlElement xRetVal = xDoc.CreateElement(strName);

            XmlNode xAtt_X = xDoc.CreateNode(XmlNodeType.Attribute, conXmlField_Point_X, "pt");
            XmlNode xAtt_Y = xDoc.CreateNode(XmlNodeType.Attribute, conXmlField_Point_Y, "pt");

            xAtt_X.Value = pt.X.ToString();
            xAtt_Y.Value = pt.Y.ToString();

            xRetVal.Attributes.SetNamedItem(xAtt_X);
            xRetVal.Attributes.SetNamedItem(xAtt_Y);

            return xRetVal;
        }

        public static PointF Get_PointFFromXml(XmlNode xPtf)
        {
            double dblX = 0, dblY = 0;
            try
            {
                dblX = Convert.ToDouble(xPtf.Attributes[0].InnerText);
                dblY = Convert.ToDouble(xPtf.Attributes[1].InnerText);

            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show("Error:" + e.Message);
            }

            return new PointF((float)dblX, (float)dblY);
        }

        public static PointF PointFFromPoint(Point pt)
        {
            return new PointF(pt.X, pt.Y);
        }


        public static PointF PointF_FactorSize(PointF ptf, float factorSize)
        {
            return new PointF(ptf.X * factorSize, ptf.Y * factorSize);
        }
        public static PointF Point_FactorSize(PointF pt, float factorSize) { return PointF_FactorSize(pt, (double)factorSize); }
        public static PointF PointF_FactorSize(PointF pt, double factorSize)
        {
            return new PointF((int)((double)pt.X * factorSize), (int)((double)pt.Y * factorSize));
        }

        public static PointF AverageTwoPointF(PointF ptf1, PointF ptf2)
        {
            return new PointF((ptf1.X + ptf2.X) / 2, (ptf1.Y + ptf2.Y) / 2);
        }

        public static double Get_SweepRadians(double dblStart, double dblFinish)
        {
            dblStart = cleanAngle(dblStart);
            dblFinish = cleanAngle(dblFinish);
            if (dblFinish < dblStart)
                dblFinish += 2 * Math.PI;
            return dblFinish - dblStart;
        }

        public static void Sort(ref List<double> lst)
        {
            for (int intOuterCounter = 0; intOuterCounter < lst.Count - 1; intOuterCounter++)
            {
                for (int intInnerCounter = intOuterCounter + 1; intInnerCounter < lst.Count; intInnerCounter++)
                {
                    if (lst[intOuterCounter] > lst[intInnerCounter])
                    {
                        double dblTemp = lst[intOuterCounter];
                        lst[intOuterCounter] = lst[intInnerCounter];
                        lst[intInnerCounter] = dblTemp;
                    }
                }
            }
        }

        /// <summary>
        /// returns the difference between the start angle and the finish angle rotating in positive direction
        /// </summary>
        /// <param name="dblStart">angle sweep starts</param>
        /// <param name="dblFinish">angle sweep  ends</param>
        /// <returns>the angle that sweeps between start and finish</returns>
        public static double Get_SweepAngle(double dblStart, double dblFinish)
        {
            dblStart = cleanDegrees(dblStart);
            dblFinish = cleanDegrees(dblFinish);
            if (dblStart > dblFinish)
                dblFinish += (2.0 * Math.PI);


            double dblRetVal = cleanDegrees( dblFinish - dblStart);
            return dblRetVal;
        }

        /// <summary>
        /// determines whether a given angle is located within an angle sweep 
        /// </summary>
        /// <param name="dblAngle">angle being tested</param>
        /// <param name="dblStart">start angle of sweep</param>
        /// <param name="dblSweep">number of degrees the angle is to be swept</param>
        /// <returns>true if the input angle is within the sweep</returns>
        public static bool AngleIsInSweep(double dblAngle, double dblStart, double dblSweep)
        {
            dblAngle = cleanDegrees(dblAngle);
            dblStart = cleanDegrees(dblStart);
            dblSweep = cleanDegrees(dblSweep);

            if (dblStart + dblSweep > 360)
            {
                if (dblAngle > dblStart)
                    return true;
                return (dblAngle < cleanDegrees(dblStart + dblSweep));
            }
            else
            {
                return (dblAngle > dblStart && dblAngle < dblStart + dblSweep);
            }
        }

        /// <summary>
        /// determines whether a given angle is located within an angle sweep 
        /// </summary>
        /// <param name="dblRadian">angle being tested</param>
        /// <param name="dblStart">start angle of sweep</param>
        /// <param name="dblSweep">number of Radiansthe angle is to be swept</param>
        /// <returns>true if the input angle is within the sweep</returns>
        public static bool RadianAngleIsInSweep(double dblRadian, double dblStart, double dblSweep)
        {
            dblRadian = cleanAngle(dblRadian);
            dblStart = cleanAngle(dblStart);
            dblSweep = cleanAngle(dblSweep);

            if (dblStart + dblSweep > 2*Math.PI)
            {
                if (dblRadian> dblStart)
                    return true;
                return (dblRadian < cleanAngle(dblStart + dblSweep));
            }
            else
            {
                return (dblRadian > dblStart && dblRadian < dblStart + dblSweep);
            }
        }

        /// <summary>
        /// returns a value between 0 & 360 degrees equivalent to the input value
        /// </summary>
        /// <param name="dblAngle">angle to be replaced with equivalent between 0 & 360</param>
        /// <returns>value equivalent to input between 0 & 360 degrees</returns>
        public static double cleanDegrees(double dblAngle)
        {
            while (dblAngle > 360)
                dblAngle -= 360;
            while (dblAngle < 0)
                dblAngle += 360;
            return dblAngle;

        }
        public const double TwoPi = Math.PI * 2.0;

        public static classRadialCoordinate CarToRad(PointF pt)
        {
            classRadialCoordinate cRad_RetVal = new classRadialCoordinate((double)Math.Atan2(pt.Y, pt.X), (int)Math.Sqrt((double)(pt.X * pt.X) + (double)(pt.Y * pt.Y)));
            return cRad_RetVal;
        }


        public static Point RadToPt(classRadialCoordinate cRad, double dblAngle)
        {
            Point RetVal = new Point();
            RetVal.X = (int)(cRad.Magnitude * Math.Cos(cRad.Radians + dblAngle));
            RetVal.Y = (int)(cRad.Magnitude * Math.Sin(cRad.Radians + dblAngle));
            return RetVal;
        }

        public static Point RadToPt(classRadialCoordinate cRad, double dblAngle, double dblSizeFactor)
        {
            Point RetVal = new Point();
            RetVal.X = (int)(cRad.Magnitude * dblSizeFactor * Math.Cos(cRad.Radians + dblAngle));
            RetVal.Y = (int)(cRad.Magnitude * dblSizeFactor * Math.Sin(cRad.Radians + dblAngle));
            return RetVal;
        }


        public static PointF RadToPtF(classRadialCoordinate cRad, double dblAngle)
        {
            PointF RetVal = new PointF();
            RetVal.X = (int)(cRad.Magnitude * Math.Cos(cRad.Radians + dblAngle));
            RetVal.Y = (int)(cRad.Magnitude * Math.Sin(cRad.Radians + dblAngle));
            return RetVal;
        }

        public static PointF RadToPtF(classRadialCoordinate cRad, double dblAngle, double dblSizeFactor)
        {
            PointF RetVal = new PointF();
            RetVal.X = (int)(cRad.Magnitude * dblSizeFactor * Math.Cos(cRad.Radians + dblAngle));
            RetVal.Y = (int)(cRad.Magnitude * dblSizeFactor * Math.Sin(cRad.Radians + dblAngle));
            return RetVal;
        }
        public static PointF AddTwoDVectors(PointF pt1, PointF fpt2)
        {
            PointF fpt_RetVal = new PointF();
            fpt_RetVal.X = pt1.X + fpt2.X;
            fpt_RetVal.Y = pt1.Y + fpt2.Y;
            return fpt_RetVal;
        }

        public static PointF AddTwoPoints(Point pt1, PointF ptf2) { return new PointF(pt1.X + ptf2.X, pt1.Y + ptf2.Y); }

        public static Point AddTwoPoints(Point pt1, Point pt2) { return new Point(pt1.X + pt2.X, pt1.Y + pt2.Y); }
        public static PointF AddTwoPointFs(PointF ptf1, PointF ptf2) { return new PointF(ptf1.X + ptf2.X, ptf1.Y + ptf2.Y); }

        /// <summary>
        /// returns the vector difference between the two PointFs pt1-pt2
        /// </summary>
        /// <param name="pt1">PointF from which second PointF is subtracted</param>
        /// <param name="pt2">PointF subtracted from the first</param>
        /// <returns></returns>
        public static PointF SubTwoPointFs(PointF pt1, PointF pt2) { return new PointF(pt1.X - pt2.X, pt1.Y - pt2.Y); }

        /// <summary>
        /// returns the vector difference between the two PointFs pt1-pt2
        /// </summary>
        /// <param name="pt1">PointF from which second PointF is subtracted</param>
        /// <param name="pt2">PointF subtracted from the first</param>
        /// <returns></returns>
        public static Point SubTwoPoints(Point pt1, Point pt2) { return new Point(pt1.X - pt2.X, pt1.Y - pt2.Y); }

        public static bool PointIsInsideARectangle(PointF ptf, List<RectangleF> lstRecF)
        {
            for (int intRecCounter = 0; intRecCounter < lstRecF.Count; intRecCounter++)
            {
                if (PointIsInsideARectangle(ptf, lstRecF[intRecCounter]))
                    return true;
            }
            return false;
        }

        public static bool PointIsInsideARectangle(PointF ptf, RectangleF recF)
        {
            return (ptf.X >= recF.Left
                          && ptf.X <= recF.Right
                          && ptf.Y >= recF.Top
                          && ptf.Y <= recF.Bottom);
        }

        public static bool PointFIsInsideAPolygon(PointF[] ptfPolygon, PointF ptf)
        {
            if (ptfPolygon == null
                ||
                ptfPolygon.Length < 3) return false;

            RectangleF recF = RectangleThatContainsAllPoints(ptfPolygon.ToList());
            if (!PointIsInsideARectangle(ptf, recF))
                return false;

            classLineF cLineX = new classLineF();
            cLineX.ptf1 = ptf;
            cLineX.ptf2 = new PointF(float.MaxValue, ptf.Y);

            classLineF cLineTest = new classLineF();

            cLineTest.ptf2 = ptfPolygon[ptfPolygon.Length - 1];
            PointF ptfIntersect = new PointF();
            int intNumIntersect = 0;
            for (int intTestCounter = 0; intTestCounter < ptfPolygon.Length; intTestCounter++)
            {
                cLineTest.ptf1 = cLineTest.ptf2;
                cLineTest.ptf2 = ptfPolygon[intTestCounter];

                if (cLineTest.ptf1.Y == cLineTest.ptf2.Y
                    &&
                    cLineTest.ptf1.Y == cLineX.ptf1.Y)
                {
                    float flt1Min = cLineTest.ptf1.X < cLineTest.ptf2.X
                                                     ? cLineTest.ptf1.X
                                                     : cLineTest.ptf2.X;
                    float flt1Max = cLineTest.ptf1.X > cLineTest.ptf2.X
                                                     ? cLineTest.ptf1.X
                                                     : cLineTest.ptf2.X;
                    float flt2Min = cLineX.ptf1.X < cLineX.ptf2.X
                                                   ? cLineX.ptf1.X
                                                   : cLineX.ptf2.X;
                    float flt2Max = cLineX.ptf1.X > cLineX.ptf2.X
                                                   ? cLineX.ptf1.X
                                                   : cLineX.ptf2.X;
                    if ((flt1Min < flt2Max && flt1Max > flt2Max)       // line1 straddles line2 max
                         || (flt1Min < flt2Min && flt1Max > flt2Min)    // line1 straddles line2 min
                         || (flt2Min < flt1Min && flt2Max > flt1Min)    // line2 straddles line1 min
                         || (flt2Min < flt1Max && flt2Max > flt1Max)    // line2 straddles line1 max
                         || (flt1Min > flt2Min && flt1Max < flt2Max)    // line1 rests inside line2 (line2 covers all of line1)
                         || (flt1Min < flt2Min && flt1Max > flt2Max))    // line1 covers all of line2 (line2 rests inside line1)
                        intNumIntersect++;
                }
                else if (cLineTest.IntersectsLine(ref cLineX, ref ptfIntersect))
                    intNumIntersect++;
            }

            return (intNumIntersect % 2 == 1);
        }

        /// <summary>
        /// converts a metric length of meters into feet and inches string description
        /// </summary>
        /// <param name="flatMetricLength">distance to be converted from metric measured in meters</param>
        /// <returns>a string variable displaying the standard equivalent of the input length in feet and inches</returns>
        public static string MetricToInches_string(float fltMetricLength)
        {
            //  1.000000032 M =  3.28084 feet
            float fltStandard = fltMetricLength * 3.28084f / 1.000000032f;
            return FeetDecimalToFraction(fltStandard);
        }


public static string FeetDecimalToFraction(float fltFeet)
{
    int intSign = Math.Sign(fltFeet);
    fltFeet = Math.Abs(fltFeet);

    int intFeet = (int)fltFeet;// - Math.Floor(fltStandard));

    float fltInches = (fltFeet- (float)intFeet) * 12f;
    int intInches = (int)fltInches;// - Math.Floor(fltInches));

    float fltSixteenths = (fltInches - (float)intInches) * 16f;
    int intSixteenths = (int)(fltSixteenths+.5);// - Math.Floor(fltSixteenths));

    classFraction cFraction = new classFraction(intSixteenths, 16);
    classFraction cFraction_Reduced = classFraction.ReduceToLowestTerms(ref cFraction);
    if (intFeet == 0 && intInches == 0 && cFraction_Reduced.Numerator == 0)
        return "0";

    if (cFraction_Reduced.Denominator == cFraction_Reduced.Numerator)
    {
        cFraction_Reduced.Numerator = 0;
        intInches++;
        if (intInches == 12)
        {
            intInches = 0;
            intFeet++;
        }
    }

    string strRetVal = (intSign < 0
                                ? "-"
                                : "")
                        + (intFeet != 0
                                ? intFeet.ToString() + "\""
                                : "")
                        + (intInches > 0 || cFraction_Reduced .Numerator >0
                                ? intInches.ToString() + "'"
                                : "")
                        + (cFraction_Reduced.Numerator > 0
                                ? cFraction_Reduced.toString()
                                : "");
    return strRetVal;
}



    }

    public class classFraction
    {
        public int Numerator = 0;
        public int Denominator = 1;

        public classFraction(int Numerator, int Denominator)
        {
            this.Numerator = Numerator;
            this.Denominator = Denominator;
        }

        public static classFraction ReduceToLowestTerms(ref classFraction cFraction)
        {
            if (cFraction.Numerator == 0)
                return cFraction.Copy();
            if (cFraction.Numerator == cFraction.Denominator)
                return new classFraction(1, 1);

            int[] intPrimeNumbers = {   2,3,4,5,6,7,8,9,10,
                                        11,12,13,14,15,16,17,18,19,20,
                                        21,22,23,24,25,26,27,28,29,30,
                                        31,32,33,34,35,36,37,38,39,40,
                                        41,42,43,44,45,46,47,48,49,50,
                                        51,52,53,54,55,56,57,58,59,60,
                                        61,62,63,64,65,66,67,68,69,70,
                                        71,72,73,74,75,76,77,78,79,80,
                                        81,82,83,84,85,86,87,88,89,90,
                                        91,92,93,94,95,96,97,98,99,100};

            for (int intPrimeCounter = intPrimeNumbers.Length -1; intPrimeCounter >=0; intPrimeCounter--)
            {
                if (cFraction.Numerator % intPrimeNumbers[intPrimeCounter] == 0)
                {
                    if (cFraction.Denominator % intPrimeNumbers[intPrimeCounter] == 0)
                    {
                        int intDenominator_New = cFraction.Denominator / intPrimeNumbers[intPrimeCounter];
                        int intNumerator_New = cFraction.Numerator / intPrimeNumbers[intPrimeCounter];
                        classFraction cFractionReduced = new classFraction(intNumerator_New, intDenominator_New);
                        return ReduceToLowestTerms(ref cFractionReduced);
                    }
                }
            }
            return cFraction.Copy();
        }

        public string toString()
        {
            return  Numerator.ToString() + "/" + Denominator.ToString();
        }

        public classFraction Copy()
        {
            return new classFraction(Numerator, Denominator);
        }



    }



    public class classRotateImage
    {

        static bool bolMaintainSize = false;
        public static bool MaintainSize
        {
            get { return bolMaintainSize; }
            set { bolMaintainSize = value; }
        }

        static public Bitmap rotateImage(Bitmap bmpInput, double dblRadians)
        {
            PointF ptCenterSource = new PointF(bmpInput.Width / 2, bmpInput.Height / 2);

            // measure size of output file
            PointF[] ptRotatedCorners = new PointF[4];
            PointF ptfVector;
            classRadialCoordinate cRadVector;

            // TL
            PointF ptfTL = new PointF(0, 0);
            ptfVector = new PointF(ptfTL.X - ptCenterSource.X, ptfTL.Y - ptCenterSource.Y);
            cRadVector = new classRadialCoordinate(ptfVector);
            cRadVector.Radians -= dblRadians;
            ptRotatedCorners[0] = classMath3.AddTwoPointFs(ptCenterSource, cRadVector.toPointF());
            // TR
            PointF ptfTR = new PointF(bmpInput.Width - 1, 0);
            ptfVector = new PointF(ptfTR.X - ptCenterSource.X, ptfTR.Y - ptCenterSource.Y);
            cRadVector = new classRadialCoordinate(ptfVector);
            cRadVector.Radians -= dblRadians;
            ptRotatedCorners[1] = classMath3.AddTwoPointFs(ptCenterSource, cRadVector.toPointF());
            // BR
            PointF ptfBR = new PointF(bmpInput.Width - 1, bmpInput.Height - 1);
            ptfVector = new PointF(ptfBR.X - ptCenterSource.X, ptfBR.Y - ptCenterSource.Y);
            cRadVector = new classRadialCoordinate(ptfVector);
            cRadVector.Radians -= dblRadians;
            ptRotatedCorners[2] = classMath3.AddTwoPointFs(ptCenterSource, cRadVector.toPointF());
            // BL
            PointF ptfBL = new PointF(0, bmpInput.Height - 1);
            ptfVector = new PointF(ptfBL.X - ptCenterSource.X, ptfBL.Y - ptCenterSource.Y);
            cRadVector = new classRadialCoordinate(ptfVector);
            cRadVector.Radians -= dblRadians;
            ptRotatedCorners[3] = classMath3.AddTwoPointFs(ptCenterSource, cRadVector.toPointF());

            Size szRotated = new Size(0, 0);
            int intX_Least = (int)Math.Pow(2, 16);
            int intX_Most = -64;
            int intY_Least = (int)Math.Pow(2, 16);
            int intY_Most = -64;
            for (int intCounter = 0; intCounter < ptRotatedCorners.Length; intCounter++)
            {
                if (ptRotatedCorners[intCounter].X > intX_Most) intX_Most = (int)ptRotatedCorners[intCounter].X;
                if (ptRotatedCorners[intCounter].X < intX_Least) intX_Least = (int)ptRotatedCorners[intCounter].X;

                if (ptRotatedCorners[intCounter].Y > intY_Most) intY_Most = (int)ptRotatedCorners[intCounter].Y;
                if (ptRotatedCorners[intCounter].Y < intY_Least) intY_Least = (int)ptRotatedCorners[intCounter].Y;
            }
            szRotated.Width = (intX_Most - intX_Least);
            szRotated.Height = (intY_Most - intY_Least);
            // set up tarGet_ image : white background with a limegreen rectangle size & position of rotated source image
            Bitmap bmpRotatedImage = new Bitmap(szRotated.Width, szRotated.Height);
            Bitmap bmpRetVal = bolMaintainSize
                                        ? new Bitmap(bmpInput.Width, bmpInput.Height)
                                        : new Bitmap(szRotated.Width, szRotated.Height);
            PointF ptCenterRotated = new PointF(szRotated.Width / 2, szRotated.Height / 2);
            Brush brTestFill_LimeGreen = Brushes.LimeGreen;
            Color clrTestRotRecColor = Color.LimeGreen;
            Brush brEdges_Salmon = Brushes.Salmon;
            Color clrTestEdgesColor = Color.Salmon;

            using (Graphics g = Graphics.FromImage(bmpRotatedImage))
            {
                PointF ptVectorToCenterRotated = new PointF(intX_Least, intY_Least);
                for (int intCounter = 0; intCounter < ptRotatedCorners.Length; intCounter++)
                    ptRotatedCorners[intCounter] = classMath3.SubTwoPointFs(ptRotatedCorners[intCounter], ptVectorToCenterRotated);

                g.FillRectangle(brEdges_Salmon, new RectangleF(new PointF(0, 0), szRotated));

                g.FillPolygon(brTestFill_LimeGreen, ptRotatedCorners);
                clrTestRotRecColor = bmpRotatedImage.GetPixel((int)ptCenterRotated.X, (int)ptCenterRotated.Y);

                clrTestEdgesColor = bmpRotatedImage.GetPixel(0, 0);  // will be used to make border transparent before returning
                PointF ptSearch = new PointF();
                for (int intX = 0; intX < bmpRotatedImage.Width; intX++)
                {
                    ptSearch.X = intX;
                    for (int intY = 0; intY < bmpRotatedImage.Height; intY++)
                    {
                        ptSearch.Y = intY;
                        Color clrPixel = bmpRotatedImage.GetPixel((int)ptSearch.X, (int)ptSearch.Y);
                        if (clrPixel == clrTestRotRecColor) // test if this pixel is in the rectangle and has not been set
                        {
                            classRadialCoordinate cradRotated = new classRadialCoordinate(ptCenterRotated, ptSearch);
                            classRadialCoordinate cradSource = new classRadialCoordinate(cradRotated.Radians + dblRadians, cradRotated.Magnitude);
                            PointF ptSourceShiftFromCenter = cradSource.toPointF();

                            PointF ptSource = new PointF((int)ptCenterSource.X + ptSourceShiftFromCenter.X, (int)ptCenterSource.Y + ptSourceShiftFromCenter.Y);
                            if (ptSource.X >= 0 && ptSource.X < bmpInput.Width
                                && ptSource.Y >= 0 && ptSource.Y < bmpInput.Height)
                            {
                                Color clrSource = bmpInput.GetPixel((int)ptSource.X, (int)ptSource.Y);
                                bmpRotatedImage.SetPixel((int)ptSearch.X, (int)ptSearch.Y, clrSource);
                            }
                        }
                    }
                }
            }
            // copy the rotated image onto a new white bitmap with the edge color set to transparent
            bmpRotatedImage.MakeTransparent(clrTestEdgesColor);
            using (Graphics g = Graphics.FromImage(bmpRetVal))
            {
                Rectangle recSource = MaintainSize
                                         ? new Rectangle((bmpRotatedImage.Width - bmpInput.Width) / 2, (bmpRotatedImage.Height - bmpInput.Height) / 2, bmpInput.Width, bmpInput.Height)
                                         : new Rectangle(0, 0, bmpRotatedImage.Width, bmpRotatedImage.Height);
                Rectangle recDest = new Rectangle(0, 0, bmpRetVal.Width, bmpRetVal.Height);

                g.DrawImage(bmpRotatedImage, recDest, recSource, GraphicsUnit.Pixel);
            }

            return bmpRetVal;
        }
    }
    public class classPointTree
    {
        classPointTreeElement cRoot = null;
        public void Insert(PointF ptf, ref object data)
        {
            if (cRoot == null)
            {
                cRoot = new classPointTreeElement(ptf, ref data);
                return;
            }
            classPointTreeElement cThis = cRoot;
            string strNew = getPointString(ptf);
            do
            {
                string strThis = getPointString(cThis.ptf);
                int intCompareResult = string.Compare(strNew, strThis);
                if (intCompareResult  > 0)
                {
                    if (cThis.right != null)
                        cThis = cThis.right;
                    else
                    {
                        cThis.right = new classPointTreeElement(ptf, ref data);
                        return;
                    }
                }
                else if (intCompareResult < 0)
                {
                    if (cThis.left != null)
                        cThis = cThis.left;
                    else
                    {
                        cThis.left = new classPointTreeElement(ptf, ref data);
                        return;
                    }
                }
                else
                {
                    cThis.lstData.Add(data);
                    return;
                }
            } while (true);
        }
        string getPointString(PointF ptf) { return ptf.X.ToString() + "." + ptf.Y.ToString(); }

        public List<object> Search(Point pt)
        {
            PointF ptf =new PointF( pt.X, pt.Y);
            return Search(ptf);
        }
        public List<object> Search(PointF ptf)
        {
            if (cRoot == null) return new List<object>();

            classPointTreeElement cThis = cRoot;
            string strSearch = getPointString(ptf);
            do
            {
                string strThis = getPointString(cThis.ptf);
                int intCompareResult = string.Compare(strSearch, strThis);
                if (intCompareResult > 0)
                {
                    if (cThis.right != null)
                        cThis = cThis.right;
                    else
                        return new List<object>();
                }
                else if (intCompareResult < 0)
                {
                    if (cThis.left != null)
                        cThis = cThis.left;
                    else
                        return new List<object>();
                }
                else
                    return cThis.lstData;

            } while (true);
        }

        public class classPointTreeElement
        {
            public PointF ptf;
            public classPointTreeElement left = null;
            public classPointTreeElement right = null;
            public classPointTreeElement YTreeRoot = null;
            public List<object>  lstData = new List<object>();

            public classPointTreeElement(PointF _ptf, ref object obj)
            {
                ptf = _ptf;
                lstData.Add(obj);
            }
        }
    }

    public partial class formGet_Double : Form
    {
        /*
           sample call :
           
           void main()
            {
                string strHeading = "Edit Size " + cLandmark_ToEdit.eType.ToString();
                if (frmGetDouble == null)
                    frmGetDouble = new Math3.formGet_Double(_eventhandler_Landmark_EditSize, strHeading);
                else
                    frmGetDouble.Text = strHeading;
                frmGetDouble.Value = cLandmark_ToEdit.dblDrawSizeFactor;
                frmGetDouble.TopMost = true;
                frmGetDouble.Location = new Point(e.X + 15, e.Y - 15 - frmGetDouble.Height);
                while (frmGetDouble.Right > picMap.Width)
                    frmGetDouble.Left -= 5;
                while (frmGetDouble.Top < 0)
                    frmGetDouble.Top += 5;
                frmGetDouble.Show();
            }
        
            public void _eventhandler_Landmark_EditSize(object sender, EventArgs e)
            {
                if (cLandmark_ToEdit != null)
                {
                    Math3.formGet_Double frmSender = (Math3.formGet_Double)sender;
                    cLandmark_ToEdit.dblDrawSizeFactor = frmSender.Value;
                }
            }
         */

        Label lblValue = new Label();
        TextBox txtInput = new TextBox();
        Button btnOk = new Button();
        Button btnCancel = new Button();
        Button btnRnd = new Button();

        classMinMaxDouble cMinMax = new classMinMaxDouble(0, 10);
        public double Min
        {
            get { return cMinMax.Min; }
            set
            {
                cMinMax.Min = value;
            }
        }

        public double Max
        {
            get { return cMinMax.Max; }
            set
            {
                cMinMax.Max = value;
            }
        }
     
        GroupBox grbMinMaxEditor = new GroupBox();
        TextBox txtMinMax_Editor_Min = new TextBox();
        TextBox txtMinMax_Editor_Max = new TextBox();
        Label lblMinMax_Editor_Min = new Label();
        Label lblMinMax_Editor_Max = new Label();
        Button btnEditor_Ok = new Button();

        EventHandler eventHandler_ValueEntered = null;
        public formGet_Double(EventHandler _eventHandler_ValueEntered,
                              string strHeading)
        {
            eventHandler_ValueEntered = _eventHandler_ValueEntered;
            Text = strHeading;
            TopMost = true;

            Controls.Add(btnOk);
            Controls.Add(btnCancel);
            Controls.Add(lblValue);
            Controls.Add(txtInput);
            Controls.Add(btnRnd);
            Controls.Add(grbMinMaxEditor);

            buildMinMaxEditor();

            btnOk.AutoSize
                = btnCancel.AutoSize
                = btnRnd.AutoSize
                = true;
            btnOk.AutoSizeMode
                = btnCancel.AutoSizeMode
                = btnRnd.AutoSizeMode
                = AutoSizeMode.GrowAndShrink;

            btnRnd.Tag = (object)cMinMax;

            btnOk.Text = "Ok";
            btnCancel.Text = "Cancel";
            btnRnd.Text
                = "rnd";

            lblValue.Text = "X:";

            lblValue.AutoSize = true;

            Size = new Size(250, 105);
            Size szForm = new Size(20, 45);

            btnOk.Location = new Point(Width - btnOk.Width - szForm.Width, Height - btnOk.Height - szForm.Height);
            btnCancel.Location = new Point(btnOk.Left - btnCancel.Width - 5, btnOk.Top);

            txtInput.Width
                = Width - lblValue.Width - btnRnd.Width - szForm.Width - 15; ;
            txtInput.Text = "0";

            lblValue.Location = new Point(5, 5);// szForm.Height);
            txtInput.Location = new Point(lblValue.Right + 5, lblValue.Top);
            btnRnd.Location = new Point(txtInput.Right, txtInput.Top);

            WindowState = FormWindowState.Normal;
            StartPosition = FormStartPosition.CenterParent;

            btnOk.Click += BtnOk_Click;
            btnCancel.Click += BtnCancel_Click;
            txtInput.KeyDown += TxtInput_KeyDown;
            VisibleChanged += FormGet_PointF_VisibleChanged;
            btnRnd.MouseDown += BtnRnd_MouseDown;
            Show();
        }

        private void BtnRnd_MouseDown(object sender, MouseEventArgs e)
        {
            Button btnSender = (Button)sender;
            classMinMaxDouble cMM = (classMinMaxDouble)btnSender.Tag;
            if (e.Button == MouseButtons.Left)
            {
                    double dblX = classRND.Get_Double(cMinMax);
                    txtInput.Text = dblX.ToString();
            }
            else if (e.Button == MouseButtons.Right)
            {
                grbMinMaxEditor.Tag = (object)cMM;
                txtMinMax_Editor_Min.Text = cMM.Min.ToString();
                txtMinMax_Editor_Max.Text = cMM.Max.ToString();
                grbMinMaxEditor.Text = "Edit MinMax Rnd - "
                                        + (cMM == cMinMax ? "X" : "Y");
                grbMinMaxEditor.Location = new Point(10, 10);
                grbMinMaxEditor.Show();
            }
        }

        void buildMinMaxEditor()
        {
            grbMinMaxEditor.Controls.Add(txtMinMax_Editor_Min);
            grbMinMaxEditor.Controls.Add(txtMinMax_Editor_Max);
            grbMinMaxEditor.Controls.Add(lblMinMax_Editor_Min);
            grbMinMaxEditor.Controls.Add(lblMinMax_Editor_Max);
            grbMinMaxEditor.Controls.Add(btnEditor_Ok);

            grbMinMaxEditor.Text = "Random MinMax Editor";
            lblMinMax_Editor_Min.AutoSize
                = lblMinMax_Editor_Max.AutoSize
                = btnEditor_Ok.AutoSize
                = true;
            btnEditor_Ok.AutoSizeMode = AutoSizeMode.GrowAndShrink;

            lblMinMax_Editor_Min.Text = "min:";
            lblMinMax_Editor_Max.Text = "max:";
            btnEditor_Ok.Text = "ok";

            txtMinMax_Editor_Max.Size
                = txtMinMax_Editor_Min.Size
                = new Size(120, 25);

            grbMinMaxEditor.Location = new Point();
            grbMinMaxEditor.BringToFront();

            lblMinMax_Editor_Min.Location = new Point(5, 15);
            lblMinMax_Editor_Max.Location = new Point(lblMinMax_Editor_Min.Left, lblMinMax_Editor_Min.Bottom + 5);

            txtMinMax_Editor_Min.Location = new Point(lblMinMax_Editor_Max.Right, lblMinMax_Editor_Min.Top);
            txtMinMax_Editor_Max.Location = new Point(lblMinMax_Editor_Max.Right, lblMinMax_Editor_Max.Top);

            btnEditor_Ok.Location = new Point(txtMinMax_Editor_Max.Right + 5, txtMinMax_Editor_Max.Top);

            grbMinMaxEditor.Size = new Size(btnEditor_Ok.Right + 5, btnEditor_Ok.Bottom + 5);

            txtMinMax_Editor_Min.KeyDown += TxtInput_KeyDown;
            txtMinMax_Editor_Max.KeyDown += TxtInput_KeyDown;
            btnEditor_Ok.Click += BtnEditor_Ok_Click;
            grbMinMaxEditor.VisibleChanged += GrbMinMaxEditor_VisibleChanged;
            grbMinMaxEditor.Hide();
        }

        private void GrbMinMaxEditor_VisibleChanged(object sender, EventArgs e)
        {
            txtInput.Visible
                = btnRnd.Visible
                = btnOk.Visible
                = btnCancel.Visible
                = lblValue.Visible
                = !grbMinMaxEditor.Visible;
        }

        private void BtnEditor_Ok_Click(object sender, EventArgs e)
        {
            classMinMaxDouble cMM = (classMinMaxDouble)grbMinMaxEditor.Tag;
            try
            {
                double dbl1 = Convert.ToDouble(txtMinMax_Editor_Min.Text);
                double dbl2 = Convert.ToDouble(txtMinMax_Editor_Max.Text);
                cMM.Min = dbl1 < dbl2 ? dbl1 : dbl2;
                cMM.Max = dbl1 > dbl2 ? dbl1 : dbl2;
            }
            catch (Exception err)
            {
            }
            grbMinMaxEditor.Hide();
        }

        private void FormGet_PointF_VisibleChanged(object sender, EventArgs e)
        {
            txtInput.Focus();
        }

        public double Value
        {
            get
            {
                double dblValue = 0;
                try
                {
                    dblValue = Convert.ToDouble(txtInput.Text);
                    return dblValue;
                }
                catch (Exception e)
                {
                    return 0;
                }
            }
            set
            {
                txtInput.Text = value.ToString();
            }
        }
        private void TxtInput_KeyDown(object sender, KeyEventArgs e)
        {
            string strAllowedChar = "0123456789";
            TextBox txtSender = (TextBox)sender;
            switch (e.KeyCode)
            {
                case Keys.Back:
                case Keys.Left:
                case Keys.Delete:
                case Keys.Insert:
                case Keys.Home:
                case Keys.End:
                case Keys.OemMinus:
                case Keys.Decimal:
                    break;

                default:
                    char chrPressed = (char)e.KeyCode;
                    if (!strAllowedChar.Contains(chrPressed))
                        e.SuppressKeyPress = true;
                    break;
            }
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            Hide();
        }

        private void BtnOk_Click(object sender, EventArgs e)
        {
            Hide();
            object objSender = (object)this;
            eventHandler_ValueEntered(objSender, new EventArgs());
        }
    }
    
    public partial class formGet_PointF : Form
    {
        /*
           sample call :     

            formGet_String(_eventHandler_Get_PointF, "Velocity :");

             public void _eventHandler_Get_PointF(object sender, EventArgs e)
             {
                 formGet_String frmSender = (formGet_String)sender;
                 PointF ptf = frmSender.ptf;
                  .. do work ..
             }
         */
        Label lblX = new Label();
        Label lblY = new Label();
        TextBox txtInput_X = new TextBox();
        TextBox txtInput_Y = new TextBox();
        Button btnOk = new Button();
        Button btnCancel = new Button();
        Button btnRnd_X = new Button();
        Button btnRnd_Y = new Button();

        classMinMaxDouble cMinMax_X = new classMinMaxDouble(0, 10);
        public double X_Min
        {
            get { return cMinMax_X.Min; }
            set
            {
                cMinMax_X.Min = value;
            }
        }

        public double X_Max
        {
            get { return cMinMax_X.Max; }
            set
            {
                cMinMax_X.Max = value;
            }
        }

        classMinMaxDouble cMinMax_Y = new classMinMaxDouble(0, 10);
        public double Y_Min
        {
            get { return cMinMax_Y.Min; }
            set
            {
                cMinMax_Y.Min = value;
            }
        }

        public double Y_Max
        {
            get { return cMinMax_Y.Max; }
            set
            {
                cMinMax_Y.Max = value;
            }
        }

        GroupBox grbMinMaxEditor = new GroupBox();
        TextBox txtMinMax_Editor_Min = new TextBox();
        TextBox txtMinMax_Editor_Max = new TextBox();
        Label lblMinMax_Editor_Min = new Label();
        Label lblMinMax_Editor_Max = new Label();
        Button btnEditor_Ok = new Button();

        EventHandler eventHandler_ValueEntered = null;
        public formGet_PointF(EventHandler _eventHandler_ValueEntered,
                              string strHeading)
        {
            eventHandler_ValueEntered = _eventHandler_ValueEntered;
            Text = strHeading;
            TopMost = true;

            Controls.Add(btnOk);
            Controls.Add(btnCancel);
            Controls.Add(lblX);
            Controls.Add(lblY);
            Controls.Add(txtInput_X);
            Controls.Add(txtInput_Y);
            Controls.Add(btnRnd_X);
            Controls.Add(btnRnd_Y);
            Controls.Add(grbMinMaxEditor);

            buildMinMaxEditor();

            btnOk.AutoSize
                = btnCancel.AutoSize
                = btnRnd_X.AutoSize
                = btnRnd_Y.AutoSize
                = true;
            btnOk.AutoSizeMode
                = btnCancel.AutoSizeMode
                = btnRnd_X.AutoSizeMode
                = btnRnd_Y.AutoSizeMode
                = AutoSizeMode.GrowAndShrink;

            btnRnd_X.Tag = (object)cMinMax_X;
            btnRnd_Y.Tag = (object)cMinMax_Y;

            btnOk.Text = "Ok";
            btnCancel.Text = "Cancel";
            btnRnd_X.Text
                = btnRnd_Y.Text
                = "rnd";

            lblX.Text = "X:";
            lblY.Text = "Y:";

            lblY.AutoSize
                = lblX.AutoSize = true;

            Size = new Size(250, 125);
            Size szForm = new Size(20, 45);

            btnOk.Location = new Point(Width - btnOk.Width - szForm.Width, Height - btnOk.Height - szForm.Height);
            btnCancel.Location = new Point(btnOk.Left - btnCancel.Width - 5, btnOk.Top);

            txtInput_X.Width
                = txtInput_Y.Width
                = Width - lblX.Width - btnRnd_X.Width - szForm.Width - 15; ;
            txtInput_X.Text = "0";
            txtInput_Y.Text = "0";

            lblX.Location = new Point(5, 5);// szForm.Height);
            txtInput_X.Location = new Point(lblX.Right + 5, lblX.Top);
            btnRnd_X.Location = new Point(txtInput_X.Right, txtInput_X.Top);

            lblY.Location = new Point(lblX.Left, txtInput_X.Bottom + 5);
            txtInput_Y.Location = new Point(lblY.Right + 5, lblY.Top);
            btnRnd_Y.Location = new Point(txtInput_Y.Right, txtInput_Y.Top);

            WindowState = FormWindowState.Normal;
            StartPosition = FormStartPosition.CenterParent;

            btnOk.Click += BtnOk_Click;
            btnCancel.Click += BtnCancel_Click;
            txtInput_X.KeyDown += TxtInput_KeyDown;
            txtInput_Y.KeyDown += TxtInput_KeyDown;
            VisibleChanged += FormGet_PointF_VisibleChanged;
            btnRnd_X.MouseDown += BtnRnd_MouseDown;
            btnRnd_Y.MouseDown += BtnRnd_MouseDown;
            Show();
        }

        private void BtnRnd_MouseDown(object sender, MouseEventArgs e)
        {
            Button btnSender = (Button)sender;
            classMinMaxDouble cMM = (classMinMaxDouble)btnSender.Tag;
            if (e.Button == MouseButtons.Left)
            {
                if (cMM == cMinMax_X)
                {
                    double dblX = classRND.Get_Double(cMinMax_X);
                    txtInput_X.Text = dblX.ToString();
                }
                else
                {
                    double dblY = classRND.Get_Double(cMinMax_Y);
                    txtInput_Y.Text = dblY.ToString();
                }
            }
            else if (e.Button == MouseButtons.Right)
            {
                grbMinMaxEditor.Tag = (object)cMM;
                txtMinMax_Editor_Min.Text = cMM.Min.ToString();
                txtMinMax_Editor_Max.Text = cMM.Max.ToString();
                grbMinMaxEditor.Text = "Edit MinMax Rnd - "
                                        + (cMM == cMinMax_X ? "X" : "Y");
                grbMinMaxEditor.Location = new Point(10, 10);
                grbMinMaxEditor.Show();
            }
        }

        void buildMinMaxEditor()
        {
            grbMinMaxEditor.Controls.Add(txtMinMax_Editor_Min);
            grbMinMaxEditor.Controls.Add(txtMinMax_Editor_Max);
            grbMinMaxEditor.Controls.Add(lblMinMax_Editor_Min);
            grbMinMaxEditor.Controls.Add(lblMinMax_Editor_Max);
            grbMinMaxEditor.Controls.Add(btnEditor_Ok);

            grbMinMaxEditor.Text = "Random MinMax Editor";
            lblMinMax_Editor_Min.AutoSize
                = lblMinMax_Editor_Max.AutoSize
                = btnEditor_Ok.AutoSize
                = true;
            btnEditor_Ok.AutoSizeMode = AutoSizeMode.GrowAndShrink;

            lblMinMax_Editor_Min.Text = "min:";
            lblMinMax_Editor_Max.Text = "max:";
            btnEditor_Ok.Text = "ok";

            txtMinMax_Editor_Max.Size
                = txtMinMax_Editor_Min.Size
                = new Size(120, 25);

            grbMinMaxEditor.Location = new Point();
            grbMinMaxEditor.BringToFront();

            lblMinMax_Editor_Min.Location = new Point(5, 15);
            lblMinMax_Editor_Max.Location = new Point(lblMinMax_Editor_Min.Left, lblMinMax_Editor_Min.Bottom + 5);

            txtMinMax_Editor_Min.Location = new Point(lblMinMax_Editor_Max.Right, lblMinMax_Editor_Min.Top);
            txtMinMax_Editor_Max.Location = new Point(lblMinMax_Editor_Max.Right, lblMinMax_Editor_Max.Top);

            btnEditor_Ok.Location = new Point(txtMinMax_Editor_Max.Right + 5, txtMinMax_Editor_Max.Top);

            grbMinMaxEditor.Size = new Size(btnEditor_Ok.Right + 5, btnEditor_Ok.Bottom + 5);

            txtMinMax_Editor_Min.KeyDown += TxtInput_KeyDown;
            txtMinMax_Editor_Max.KeyDown += TxtInput_KeyDown;
            btnEditor_Ok.Click += BtnEditor_Ok_Click;
            grbMinMaxEditor.VisibleChanged += GrbMinMaxEditor_VisibleChanged;
            grbMinMaxEditor.Hide();
        }

        private void GrbMinMaxEditor_VisibleChanged(object sender, EventArgs e)
        {
            txtInput_X.Visible
                = txtInput_Y.Visible
                = btnRnd_X.Visible
                = btnRnd_Y.Visible
                = btnOk.Visible
                = btnCancel.Visible
                = lblX.Visible
                = lblY.Visible
                = !grbMinMaxEditor.Visible;
        }

        private void BtnEditor_Ok_Click(object sender, EventArgs e)
        {
            classMinMaxDouble cMM = (classMinMaxDouble)grbMinMaxEditor.Tag;
            try
            {
                double dbl1 = Convert.ToDouble(txtMinMax_Editor_Min.Text);
                double dbl2 = Convert.ToDouble(txtMinMax_Editor_Max.Text);
                cMM.Min = dbl1 < dbl2 ? dbl1 : dbl2;
                cMM.Max = dbl1 > dbl2 ? dbl1 : dbl2;
            }
            catch (Exception err)
            {
            }
            grbMinMaxEditor.Hide();
        }

        private void FormGet_PointF_VisibleChanged(object sender, EventArgs e)
        {
            txtInput_X.Focus();
        }

        public PointF ptf
        {
            get
            {
                double dbl_X = 0, dbl_Y = 0;
                try
                {
                    dbl_X = Convert.ToDouble(txtInput_X.Text);
                    dbl_Y = Convert.ToDouble(txtInput_Y.Text);
                    return new PointF((float)dbl_X, (float)dbl_Y);
                }
                catch (Exception e)
                {
                    return new PointF();
                }
            }
            set
            {
                txtInput_X.Text = value.X.ToString();
                txtInput_Y.Text = value.Y.ToString();
            }
        }
        private void TxtInput_KeyDown(object sender, KeyEventArgs e)
        {
            string strAllowedChar = "0123456789";
            TextBox txtSender = (TextBox)sender;
            switch (e.KeyCode)
            {
                case Keys.Back:
                case Keys.Left:
                case Keys.Delete:
                case Keys.Insert:
                case Keys.Home:
                case Keys.End:
                case Keys.OemMinus:
                case Keys.Decimal:
                    break;

                default:
                    char chrPressed = (char)e.KeyCode;
                    if (!strAllowedChar.Contains(chrPressed))
                        e.SuppressKeyPress = true;
                    break;
            }
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            Hide();
        }

        private void BtnOk_Click(object sender, EventArgs e)
        {
            Hide();
            object objSender = (object)this;
            eventHandler_ValueEntered(objSender, new EventArgs());
        }
    }  
    public partial class formGet_SizeF : Form
    {
        /*
           sample call :     

            formGet_String(_eventHandler_Get_SizeF, "Size :");

             public void _eventHandler_Get_SizeF(object sender, EventArgs e)
             {
                 formGet_String frmSender = (formGet_String)sender;
                 SizeF ptf = frmSender.ptf;
                  .. do work ..
             }
         */
        Label lblWidth = new Label();
        Label lblHeight = new Label();
        TextBox txtInput_Width = new TextBox();
        TextBox txtInput_Height = new TextBox();
        Button btnOk = new Button();
        Button btnCancel = new Button();
        Button btnRnd_Width = new Button();
        Button btnRnd_Height = new Button();

        classMinMaxDouble cMinMaWidth_Width = new classMinMaxDouble(1, 100);
        public double Width_Min
        {
            get { return cMinMaWidth_Width.Min; }
            set
            {
                cMinMaWidth_Width.Min = value;
            }
        }

        public double Width_Max
        {
            get { return cMinMaWidth_Width.Max; }
            set
            {
                cMinMaWidth_Width.Max = value;
            }
        }

        classMinMaxDouble cMinMaWidth_Height = new classMinMaxDouble(0, 10);
        public double Height_Min
        {
            get { return cMinMaWidth_Height.Min; }
            set
            {
                cMinMaWidth_Height.Min = value;
            }
        }

        public double Height_Max
        {
            get { return cMinMaWidth_Height.Max; }
            set
            {
                cMinMaWidth_Height.Max = value;
            }
        }

        GroupBox grbMinMaxEditor = new GroupBox();
        TextBox txtMinMaWidth_Editor_Min = new TextBox();
        TextBox txtMinMaWidth_Editor_Max = new TextBox();
        Label lblMinMaWidth_Editor_Min = new Label();
        Label lblMinMaWidth_Editor_Max = new Label();
        Button btnEditor_Ok = new Button();

        EventHandler eventHandler_ValueEntered = null;
        public formGet_SizeF(EventHandler _eventHandler_ValueEntered,
                              string strHeading)
        {
            eventHandler_ValueEntered = _eventHandler_ValueEntered;
            Text = strHeading;
            TopMost = true;

            Controls.Add(btnOk);
            Controls.Add(btnCancel);
            Controls.Add(lblWidth);
            Controls.Add(lblHeight);
            Controls.Add(txtInput_Width);
            Controls.Add(txtInput_Height);
            Controls.Add(btnRnd_Width);
            Controls.Add(btnRnd_Height);
            Controls.Add(grbMinMaxEditor);

            buildMinMaxEditor();

            btnOk.AutoSize
                = btnCancel.AutoSize
                = btnRnd_Width.AutoSize
                = btnRnd_Height.AutoSize
                = true;
            btnOk.AutoSizeMode
                = btnCancel.AutoSizeMode
                = btnRnd_Width.AutoSizeMode
                = btnRnd_Height.AutoSizeMode
                = AutoSizeMode.GrowAndShrink;

            btnRnd_Width.Tag = (object)cMinMaWidth_Width;
            btnRnd_Height.Tag = (object)cMinMaWidth_Height;

            btnOk.Text = "Ok";
            btnCancel.Text = "Cancel";
            btnRnd_Width.Text
                = btnRnd_Height.Text
                = "rnd";

            lblWidth.Text = "X:";
            lblHeight.Text = "Y:";

            lblHeight.AutoSize
                = lblWidth.AutoSize = true;

            Size = new Size(250, 125);
            Size szForm = new Size(20, 45);

            btnOk.Location = new Point(Width - btnOk.Width - szForm.Width, Height - btnOk.Height - szForm.Height);
            btnCancel.Location = new Point(btnOk.Left - btnCancel.Width - 5, btnOk.Top);

            txtInput_Width.Width
                = txtInput_Height.Width
                = Width - lblWidth.Width - btnRnd_Width.Width - szForm.Width - 15; ;
            txtInput_Width.Text = "0";
            txtInput_Height.Text = "0";

            lblWidth.Location = new Point(5, 5);// szForm.Height);
            txtInput_Width.Location = new Point(lblWidth.Right + 5, lblWidth.Top);
            btnRnd_Width.Location = new Point(txtInput_Width.Right, txtInput_Width.Top);

            lblHeight.Location = new Point(lblWidth.Left, txtInput_Width.Bottom + 5);
            txtInput_Height.Location = new Point(lblHeight.Right + 5, lblHeight.Top);
            btnRnd_Height.Location = new Point(txtInput_Height.Right, txtInput_Height.Top);

            WindowState = FormWindowState.Normal;
            StartPosition = FormStartPosition.CenterParent;

            btnOk.Click += BtnOk_Click;
            btnCancel.Click += BtnCancel_Click;
            txtInput_Width.KeyDown += TxtInput_KeyDown;
            txtInput_Height.KeyDown += TxtInput_KeyDown;
            VisibleChanged += FormGet_SizeF_VisibleChanged;
            btnRnd_Width.MouseDown += BtnRnd_MouseDown;
            btnRnd_Height.MouseDown += BtnRnd_MouseDown;
        }

        private void BtnRnd_MouseDown(object sender, MouseEventArgs e)
        {
            Button btnSender = (Button)sender;
            classMinMaxDouble cMM = (classMinMaxDouble)btnSender.Tag;
            if (e.Button == MouseButtons.Left)
            {
                if (cMM == cMinMaWidth_Width)
                {
                    double dblX = classRND.Get_Double(cMinMaWidth_Width);
                    txtInput_Width.Text = dblX.ToString();
                }
                else
                {
                    double dblY = classRND.Get_Double(cMinMaWidth_Height);
                    txtInput_Height.Text = dblY.ToString();
                }
            }
            else if (e.Button == MouseButtons.Right)
            {
                grbMinMaxEditor.Tag = (object)cMM;
                txtMinMaWidth_Editor_Min.Text = cMM.Min.ToString();
                txtMinMaWidth_Editor_Max.Text = cMM.Max.ToString();
                grbMinMaxEditor.Text = "Edit MinMax Rnd - "
                                        + (cMM == cMinMaWidth_Width ? "X" : "Y");
                grbMinMaxEditor.Location = new Point(10, 10);
                grbMinMaxEditor.Show();
            }
        }

        void buildMinMaxEditor()
        {
            grbMinMaxEditor.Controls.Add(txtMinMaWidth_Editor_Min);
            grbMinMaxEditor.Controls.Add(txtMinMaWidth_Editor_Max);
            grbMinMaxEditor.Controls.Add(lblMinMaWidth_Editor_Min);
            grbMinMaxEditor.Controls.Add(lblMinMaWidth_Editor_Max);
            grbMinMaxEditor.Controls.Add(btnEditor_Ok);

            grbMinMaxEditor.Text = "Random MinMax Editor";
            lblMinMaWidth_Editor_Min.AutoSize
                = lblMinMaWidth_Editor_Max.AutoSize
                = btnEditor_Ok.AutoSize
                = true;
            btnEditor_Ok.AutoSizeMode = AutoSizeMode.GrowAndShrink;

            lblMinMaWidth_Editor_Min.Text = "min:";
            lblMinMaWidth_Editor_Max.Text = "max:";
            btnEditor_Ok.Text = "ok";

            txtMinMaWidth_Editor_Max.Size
                = txtMinMaWidth_Editor_Min.Size
                = new Size(120, 25);

            grbMinMaxEditor.Location = new Point();
            grbMinMaxEditor.BringToFront();

            lblMinMaWidth_Editor_Min.Location = new Point(5, 15);
            lblMinMaWidth_Editor_Max.Location = new Point(lblMinMaWidth_Editor_Min.Left, lblMinMaWidth_Editor_Min.Bottom + 5);

            txtMinMaWidth_Editor_Min.Location = new Point(lblMinMaWidth_Editor_Max.Right, lblMinMaWidth_Editor_Min.Top);
            txtMinMaWidth_Editor_Max.Location = new Point(lblMinMaWidth_Editor_Max.Right, lblMinMaWidth_Editor_Max.Top);

            btnEditor_Ok.Location = new Point(txtMinMaWidth_Editor_Max.Right + 5, txtMinMaWidth_Editor_Max.Top);

            grbMinMaxEditor.Size = new Size(btnEditor_Ok.Right + 5, btnEditor_Ok.Bottom + 5);

            txtMinMaWidth_Editor_Min.KeyDown += TxtInput_KeyDown;
            txtMinMaWidth_Editor_Max.KeyDown += TxtInput_KeyDown;
            btnEditor_Ok.Click += BtnEditor_Ok_Click;
            grbMinMaxEditor.VisibleChanged += GrbMinMaxEditor_VisibleChanged;
            grbMinMaxEditor.Hide();
        }

        private void GrbMinMaxEditor_VisibleChanged(object sender, EventArgs e)
        {
            txtInput_Width.Visible
                = txtInput_Height.Visible
                = btnRnd_Width.Visible
                = btnRnd_Height.Visible
                = btnOk.Visible
                = btnCancel.Visible
                = lblWidth.Visible
                = lblHeight.Visible
                = !grbMinMaxEditor.Visible;
        }

        private void BtnEditor_Ok_Click(object sender, EventArgs e)
        {
            classMinMaxDouble cMM = (classMinMaxDouble)grbMinMaxEditor.Tag;
            try
            {
                double dbl1 = Convert.ToDouble(txtMinMaWidth_Editor_Min.Text);
                double dbl2 = Convert.ToDouble(txtMinMaWidth_Editor_Max.Text);
                cMM.Min = dbl1 < dbl2 ? dbl1 : dbl2;
                cMM.Max = dbl1 > dbl2 ? dbl1 : dbl2;
            }
            catch (Exception err)
            {
            }
            grbMinMaxEditor.Hide();
        }

        private void FormGet_SizeF_VisibleChanged(object sender, EventArgs e)
        {
            txtInput_Width.Focus();
        }

        public SizeF sizeF
        {
            get
            {
                double dbl_Width = 0, dbl_Height = 0;
                try
                {
                    dbl_Width = Convert.ToDouble(txtInput_Width.Text);
                    dbl_Height = Convert.ToDouble(txtInput_Height.Text);
                    return new SizeF((float)dbl_Width, (float)dbl_Height);
                }
                catch (Exception e)
                {
                    return new SizeF();
                }
            }
            set
            {
                txtInput_Width.Text = value.Width.ToString();
                txtInput_Height.Text = value.Height.ToString();
            }
        }
        private void TxtInput_KeyDown(object sender, KeyEventArgs e)
        {
            string strAllowedChar = "0123456789";
            TextBox txtSender = (TextBox)sender;
            switch (e.KeyCode)
            {
                case Keys.Back:
                case Keys.Left:
                case Keys.Delete:
                case Keys.Insert:
                case Keys.Home:
                case Keys.End:
                case Keys.OemMinus:
                case Keys.Decimal:
                    break;

                default:
                    char chrPressed = (char)e.KeyCode;
                    if (!strAllowedChar.Contains(chrPressed))
                        e.SuppressKeyPress = true;
                    break;
            }
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            Hide();
        }

        private void BtnOk_Click(object sender, EventArgs e)
        {
            Hide();
            object objSender = (object)this;
            eventHandler_ValueEntered(objSender, new EventArgs());
        }
    }
    public partial class formGet_Size : Form
    {
        /*
           sample call :     

            formGet_String(_eventHandler_Get_Size, "Size :");

             public void _eventHandler_Get_Size(object sender, EventArgs e)
             {
                 formGet_String frmSender = (formGet_String)sender;
                 Size ptf = frmSender.ptf;
                  .. do work ..
             }
         */
        Label lblWidth = new Label();
        Label lblHeight = new Label();
        TextBox txtInput_Width = new TextBox();
        TextBox txtInput_Height = new TextBox();
        Button btnOk = new Button();
        Button btnCancel = new Button();
        Button btnRnd_Width = new Button();
        Button btnRnd_Height = new Button();

        classMinMaxDouble cMinMaWidth_Width = new classMinMaxDouble(1, 100);
        public double Width_Min
        {
            get { return cMinMaWidth_Width.Min; }
            set
            {
                cMinMaWidth_Width.Min = value;
            }
        }

        public double Width_Max
        {
            get { return cMinMaWidth_Width.Max; }
            set
            {
                cMinMaWidth_Width.Max = value;
            }
        }

        classMinMaxDouble cMinMaWidth_Height = new classMinMaxDouble(0, 10);
        public double Height_Min
        {
            get { return cMinMaWidth_Height.Min; }
            set
            {
                cMinMaWidth_Height.Min = value;
            }
        }

        public double Height_Max
        {
            get { return cMinMaWidth_Height.Max; }
            set
            {
                cMinMaWidth_Height.Max = value;
            }
        }

        GroupBox grbMinMaxEditor = new GroupBox();
        TextBox txtMinMaWidth_Editor_Min = new TextBox();
        TextBox txtMinMaWidth_Editor_Max = new TextBox();
        Label lblMinMaWidth_Editor_Min = new Label();
        Label lblMinMaWidth_Editor_Max = new Label();
        Button btnEditor_Ok = new Button();

        EventHandler eventHandler_ValueEntered = null;
        public formGet_Size(EventHandler _eventHandler_ValueEntered,
                              string strHeading)
        {
            eventHandler_ValueEntered = _eventHandler_ValueEntered;
            Text = strHeading;
            TopMost = true;

            Controls.Add(btnOk);
            Controls.Add(btnCancel);
            Controls.Add(lblWidth);
            Controls.Add(lblHeight);
            Controls.Add(txtInput_Width);
            Controls.Add(txtInput_Height);
            Controls.Add(btnRnd_Width);
            Controls.Add(btnRnd_Height);
            Controls.Add(grbMinMaxEditor);

            buildMinMaxEditor();

            btnOk.AutoSize
                = btnCancel.AutoSize
                = btnRnd_Width.AutoSize
                = btnRnd_Height.AutoSize
                = true;
            btnOk.AutoSizeMode
                = btnCancel.AutoSizeMode
                = btnRnd_Width.AutoSizeMode
                = btnRnd_Height.AutoSizeMode
                = AutoSizeMode.GrowAndShrink;


            btnRnd_Width.Tag = (object)cMinMaWidth_Width;
            btnRnd_Height.Tag = (object)cMinMaWidth_Height;

            btnOk.Text = "Ok";
            btnCancel.Text = "Cancel";
            btnRnd_Width.Text
                = btnRnd_Height.Text
                = "rnd";

            lblWidth.Text = "X:";
            lblHeight.Text = "Y:";

            lblHeight.AutoSize
                = lblWidth.AutoSize = true;

            Size = new Size(250, 125);
            Size szForm = new Size(20, 45);

            btnOk.Location = new Point(Width - btnOk.Width - szForm.Width, Height - btnOk.Height - szForm.Height);
            btnCancel.Location = new Point(btnOk.Left - btnCancel.Width - 5, btnOk.Top);

            txtInput_Width.Width
                = txtInput_Height.Width
                = Width - lblWidth.Width - btnRnd_Width.Width - szForm.Width - 15; ;
            txtInput_Width.Text = "0";
            txtInput_Height.Text = "0";

            lblWidth.Location = new Point(5, 5);// szForm.Height);
            txtInput_Width.Location = new Point(lblWidth.Right + 5, lblWidth.Top);
            btnRnd_Width.Location = new Point(txtInput_Width.Right, txtInput_Width.Top);

            lblHeight.Location = new Point(lblWidth.Left, txtInput_Width.Bottom + 5);
            txtInput_Height.Location = new Point(lblHeight.Right + 5, lblHeight.Top);
            btnRnd_Height.Location = new Point(txtInput_Height.Right, txtInput_Height.Top);

            WindowState = FormWindowState.Normal;
            StartPosition = FormStartPosition.CenterParent;

            btnOk.Click += BtnOk_Click;
            btnCancel.Click += BtnCancel_Click;
            txtInput_Width.KeyDown += TxtInput_KeyDown;
            txtInput_Height.KeyDown += TxtInput_KeyDown;
            txtInput_Height.MouseWheel += TxtInput_MouseWheel;
            txtInput_Width.MouseWheel += TxtInput_MouseWheel;
            txtInput_Height.TextChanged += TxtInput_TextChanged;
            txtInput_Width.TextChanged += TxtInput_TextChanged;

            VisibleChanged += FormGet_Size_VisibleChanged;
            btnRnd_Width.MouseDown += BtnRnd_MouseDown;
            btnRnd_Height.MouseDown += BtnRnd_MouseDown;
        }

        private void TxtInput_TextChanged(object sender, EventArgs e)
        {
            TextBox txtSender = (TextBox)sender;
            try
            {
                int intValue = Convert.ToInt32(txtSender.Text);
                if (intValue < (txtSender == txtInput_Height ? Height_Min : Width_Min))
                    txtSender.Text = (txtSender == txtInput_Height ? Height_Min : Width_Min).ToString();
                if (intValue > (txtSender == txtInput_Height ? Height_Max : Width_Max))
                    txtSender.Text = (txtSender == txtInput_Height ? Height_Max : Width_Max).ToString();
            }
            catch (Exception)
            {
            }
        }

        private void TxtInput_MouseWheel(object sender, MouseEventArgs e)
        {
            TextBox txtSender = (TextBox)sender;
            
            try
            {
                int intValue = Convert.ToInt32(txtSender.Text);
                intValue += (int)(Math.Abs(e.Delta) / e.Delta);
                txtSender.Text = intValue.ToString();
            }
            catch (Exception)
            {
                return;
            }
            

        }

        private void BtnRnd_MouseDown(object sender, MouseEventArgs e)
        {
            Button btnSender = (Button)sender;
            classMinMaxDouble cMM = (classMinMaxDouble)btnSender.Tag;
            if (e.Button == MouseButtons.Left)
            {
                if (cMM == cMinMaWidth_Width)
                {
                    double dblX = classRND.Get_Double(cMinMaWidth_Width);
                    txtInput_Width.Text = dblX.ToString();
                }
                else
                {
                    double dblY = classRND.Get_Double(cMinMaWidth_Height);
                    txtInput_Height.Text = dblY.ToString();
                }
            }
            else if (e.Button == MouseButtons.Right)
            {
                grbMinMaxEditor.Tag = (object)cMM;
                txtMinMaWidth_Editor_Min.Text = cMM.Min.ToString();
                txtMinMaWidth_Editor_Max.Text = cMM.Max.ToString();
                grbMinMaxEditor.Text = "Edit MinMax Rnd - "
                                        + (cMM == cMinMaWidth_Width ? "X" : "Y");
                grbMinMaxEditor.Location = new Point(10, 10);
                grbMinMaxEditor.Show();
            }
        }

        void buildMinMaxEditor()
        {
            grbMinMaxEditor.Controls.Add(txtMinMaWidth_Editor_Min);
            grbMinMaxEditor.Controls.Add(txtMinMaWidth_Editor_Max);
            grbMinMaxEditor.Controls.Add(lblMinMaWidth_Editor_Min);
            grbMinMaxEditor.Controls.Add(lblMinMaWidth_Editor_Max);
            grbMinMaxEditor.Controls.Add(btnEditor_Ok);

            grbMinMaxEditor.Text = "Random MinMax Editor";
            lblMinMaWidth_Editor_Min.AutoSize
                = lblMinMaWidth_Editor_Max.AutoSize
                = btnEditor_Ok.AutoSize
                = true;
            btnEditor_Ok.AutoSizeMode = AutoSizeMode.GrowAndShrink;

            lblMinMaWidth_Editor_Min.Text = "min:";
            lblMinMaWidth_Editor_Max.Text = "max:";
            btnEditor_Ok.Text = "ok";

            txtMinMaWidth_Editor_Max.Size
                = txtMinMaWidth_Editor_Min.Size
                = new Size(120, 25);

            grbMinMaxEditor.Location = new Point();
            grbMinMaxEditor.BringToFront();

            lblMinMaWidth_Editor_Min.Location = new Point(5, 15);
            lblMinMaWidth_Editor_Max.Location = new Point(lblMinMaWidth_Editor_Min.Left, lblMinMaWidth_Editor_Min.Bottom + 5);

            txtMinMaWidth_Editor_Min.Location = new Point(lblMinMaWidth_Editor_Max.Right, lblMinMaWidth_Editor_Min.Top);
            txtMinMaWidth_Editor_Max.Location = new Point(lblMinMaWidth_Editor_Max.Right, lblMinMaWidth_Editor_Max.Top);

            btnEditor_Ok.Location = new Point(txtMinMaWidth_Editor_Max.Right + 5, txtMinMaWidth_Editor_Max.Top);

            grbMinMaxEditor.Size = new Size(btnEditor_Ok.Right + 5, btnEditor_Ok.Bottom + 5);

            txtMinMaWidth_Editor_Min.KeyDown += TxtInput_KeyDown;
            txtMinMaWidth_Editor_Max.KeyDown += TxtInput_KeyDown;
            btnEditor_Ok.Click += BtnEditor_Ok_Click;
            grbMinMaxEditor.VisibleChanged += GrbMinMaxEditor_VisibleChanged;
            grbMinMaxEditor.Hide();
        }

        private void GrbMinMaxEditor_VisibleChanged(object sender, EventArgs e)
        {
            txtInput_Width.Visible
                = txtInput_Height.Visible
                = btnRnd_Width.Visible
                = btnRnd_Height.Visible
                = btnOk.Visible
                = btnCancel.Visible
                = lblWidth.Visible
                = lblHeight.Visible
                = !grbMinMaxEditor.Visible;
        }

        private void BtnEditor_Ok_Click(object sender, EventArgs e)
        {
            classMinMaxDouble cMM = (classMinMaxDouble)grbMinMaxEditor.Tag;
            try
            {
                double dbl1 = Convert.ToDouble(txtMinMaWidth_Editor_Min.Text);
                double dbl2 = Convert.ToDouble(txtMinMaWidth_Editor_Max.Text);
                cMM.Min = dbl1 < dbl2 ? dbl1 : dbl2;
                cMM.Max = dbl1 > dbl2 ? dbl1 : dbl2;
            }
            catch (Exception err)
            {
            }
            grbMinMaxEditor.Hide();
        }

        private void FormGet_Size_VisibleChanged(object sender, EventArgs e)
        {
            txtInput_Width.Focus();
        }

        bool bolRnd = true;
        public bool RND
        { 
        get { return bolRnd; }
        set 
            {
                bolRnd = value;
                btnRnd_Height.Visible
                        = btnRnd_Width.Visible
                        = bolRnd;
            }
        }

        public Size size
        {
            get
            {
                double dbl_Width = 0, dbl_Height = 0;
                try
                {
                    dbl_Width = Convert.ToDouble(txtInput_Width.Text);
                    dbl_Height = Convert.ToDouble(txtInput_Height.Text);
                    return new Size((int)dbl_Width, (int)dbl_Height);
                }
                catch (Exception e)
                {
                    return new Size();
                }
            }
            set
            {
                txtInput_Width.Text = value.Width.ToString();
                txtInput_Height.Text = value.Height.ToString();
            }
        }
        private void TxtInput_KeyDown(object sender, KeyEventArgs e)
        {
            string strAllowedChar = "0123456789";
            TextBox txtSender = (TextBox)sender;
            switch (e.KeyCode)
            {
                case Keys.Back:
                case Keys.Left:
                case Keys.Delete:
                case Keys.Insert:
                case Keys.Home:
                case Keys.End:
                case Keys.OemMinus:
                case Keys.Decimal:
                    break;

                default:
                    char chrPressed = (char)e.KeyCode;
                    if (!strAllowedChar.Contains(chrPressed))
                        e.SuppressKeyPress = true;
                    break;
            }
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            Hide();
        }

        private void BtnOk_Click(object sender, EventArgs e)
        {
            Hide();
            object objSender = (object)this;
            eventHandler_ValueEntered(objSender, new EventArgs());
        }
    }
    public class classMinMax
    {
        public int Max = 100;
        public int Min = 0;
        public classMinMax(int intMin, int intMax)
        {
            Min = intMin;
            Max = intMax;
        }

        public classMinMax Factored(double dblFactor)
        {
            return new classMinMax((int)(Min * dblFactor), (int)(Max * dblFactor));
        }

        public classMinMax Copy()
        {
            classMinMax cRetVal = new classMinMax(Min, Max);
            return cRetVal;
        }
    }

    public class classMinMaxDouble
    {
        public double Max = 100;
        public double Min = 0;
        public classMinMaxDouble(double dblMin, double dblMax)
        {
            Min = dblMin;
            Max = dblMax;
        }
    }

    public class classPointFPair
    {
        public PointF ptf1;
        public PointF ptf2;

        public classPointFPair(ref PointF _ptf1, ref PointF _ptf2)
        {
            ptf1 = _ptf1;
            ptf2 = _ptf2;
        }

        public classPointFPair(PointF _ptf1, PointF _ptf2)
        {
            ptf1 = _ptf1;
            ptf2 = _ptf2;
        }
    }

    public class classPointF3D
    {
        public double X = 0;
        public double Y = 0;
        public double Z = 0;
        public classPointF3D() { }
        public classPointF3D(double _X, double _Y, double _Z)
        {
            X = _X;
            Y = _Y;
            Z = _Z;
        }
    }
    public class classRND
    {
        public static Random rnd = new Random();
        public const double twoPi = Math.PI * 2;

        public static double Get_Double()
        {
            return rnd.NextDouble();
        }

        public static double Get_Double(classMinMax cMM)
        {
            double dblRnd = rnd.NextDouble();
            double dblRange = cMM.Max - cMM.Min;
            double dblBase = cMM.Min;
            return dblBase + dblRnd * dblRange;
        }

        public static double Get_Double(classMinMaxDouble cMM)
        {
            double dblRnd = rnd.NextDouble();
            double dblRange = cMM.Max - cMM.Min;
            double dblBase = cMM.Min;
            return dblBase + dblRnd * dblRange;
        }

        public static PointF Get_Point(float fltRange)
        {
            //return new PointF(200, 0);

            return new PointF((float)(Get_Sign() * Get_Double() * fltRange), (float)(Get_Sign() * Get_Double() * fltRange));
        }
        public static PointF Get_Point(classMinMax cMM_X, classMinMax cMM_Y)
        {
            return new PointF((float)(Get_Sign() * Get_Double() * Get_Int(cMM_X)), (float)(Get_Sign() * Get_Double() * Get_Int(cMM_Y)));
        }


        public static void Shuffle(ref List<int> lstInt)
        {
            for (int intCounter = 0; intCounter < lstInt.Count * 8; intCounter++)
            {
                int intA = 0, intB = 0;
                while (intA == intB)
                {
                    intA = Get_Int(0, lstInt.Count);
                    intB = Get_Int(0, lstInt.Count);
                }
                int intTemp = lstInt[intA];
                lstInt[intA] = lstInt[intB];
                lstInt[intB] = intTemp;
            }
        }

        public static void Shuffle(ref List<object> lstObj)
        {
            if (lstObj.Count > 3)
                for (int intCounter = 0; intCounter < lstObj.Count * 8; intCounter++)
                {
                    int intA = 0, intB = 0;
                    while (intA == intB)
                    {
                        intA = Get_Int(0, lstObj.Count);
                        intB = Get_Int(0, lstObj.Count);
                    }
                    object objTemp = lstObj[intA];
                    lstObj[intA] = lstObj[intB];
                    lstObj[intB] = objTemp;
                }
            else
            {
                List<object> lstTemp = new List<object>();

                for (int intCounter = lstObj.Count - 1; intCounter >= 0; intCounter--)
                    lstTemp.Add(lstObj[intCounter]);

                lstObj = lstTemp;
            }

        }


        public static int Get_Sign()
        {
            double dblMax = Math.Pow(2, 16);

            int intRnd = (int)(rnd.NextDouble() * dblMax) % 2;


            return intRnd == 0
                           ? -1
                           : 1;
        }

        public static int Get_Int(classMinMax cMM)
        {
            if (cMM == null)
            {
                System.Windows.Forms.MessageBox.Show("error: Get_In(classMinMax cMM) received a null parameter", "null parameter");
                return 0;
            }

            return Get_Int(cMM.Min, cMM.Max);
        }
        public static int Get_Int(int intMin, int intMax)
        {
            if (intMin > intMax) return 0;
            if (intMin == intMax) return intMax;
            int intRange = intMax - intMin;
            int intBase = 0;
            intBase = (int)((rnd.NextDouble() * Math.Pow(2, 32)) % intRange);
            return intMin + intBase;
        }

        public static Color Get_Color()
        {
            int intR = (int)(rnd.NextDouble() * Math.Pow(2, 16) % 256);
            int intG = (int)(rnd.NextDouble() * Math.Pow(2, 16) % 256);
            int intB = (int)(rnd.NextDouble() * Math.Pow(2, 16) % 256);
            return Color.FromArgb(intR, intG, intB);
        }


        public static char Get_Alpha()
        {
            return (char)('A' + rnd.NextDouble() * 26);
        }
    }

    public class classRadialCoordinate
    {
        public classRadialCoordinate() { }
        public classRadialCoordinate(PointF pt1, PointF pt2) { init(pt2.X - pt1.X, pt2.Y - pt1.Y); }
        public classRadialCoordinate(PointF ptf) { init(ptf.X, ptf.Y); }
        public classRadialCoordinate(double radians, double magnitude)
        {
            _magnitude = magnitude;
            _radians = radians;
        }

        private void init(double x, double y)
        {
            _magnitude = Math.Sqrt(x * x + y * y);
            _radians = Math.Atan2(y, x);
        }

        public classRadialCoordinate Copy()
        {
            classRadialCoordinate cRetVal = new classRadialCoordinate(_radians, _magnitude);
            return cRetVal;
        }

        double _radians;
        public double Radians
        {
            get { return _radians; }
            set { _radians = value; }
        }
        public double Degrees
        {
            get { return (_radians * 360.0 / (Math.PI * 2.0) % 360); }
            set { _radians = value / 360.0 * (2.0 * Math.PI); }
        }

        double _magnitude;
        public double Magnitude
        {
            get { return _magnitude; }
            set { _magnitude = value; }
        }



        public Point toPoint()
        {
            return new Point((int)(_magnitude * Math.Cos(_radians)), (int)(_magnitude * Math.Sin(_radians)));
        }

        public PointF toPointF()
        {
            return new PointF((float)(_magnitude * Math.Cos(_radians)), (float)(_magnitude * Math.Sin(_radians)));
        }


    }


    public class classCircle
    {
        public PointF ptfCenter;
        public double dblRadius;

        public classCircle() { }
        public classCircle(PointF center, double radius)
        {
            ptfCenter = center;
            dblRadius = radius;
        }
    }

    public class classLine
    {
        public classLine() { }
        Rectangle recAABB = new Rectangle();
        public classLine(PointF pt1, PointF pt2)
        {
            _pt1.X = pt1.X;
            _pt1.Y = pt1.Y;

            _pt2.X = pt2.X;
            _pt2.Y = pt2.Y;
            setMCRad();
        }

        float _M;
        public float M
        {
            get { return _M; }
        }

        float _B;
        public float B
        {
            get { return _B; }
        }

        PointF _pt1;
        public PointF pt1
        {
            get { return _pt1; }
            set
            {
                if (value.X != _pt1.X
                    ||
                    value.Y != _pt1.Y)
                {
                    _pt1.X = value.X;
                    _pt1.Y = value.Y;
                    setMCRad();
                    recAABB.Width 
                        = recAABB.Height 
                        =0;
                }
            }
        }



        PointF _pt2;
        public PointF pt2
        {
            get { return _pt2; }
            set
            {
                if (value.X != _pt2.X
                    ||
                    value.Y != _pt2.Y)
                {
                    _pt2.X = value.X;
                    _pt2.Y = value.Y;
                    setMCRad();
                    recAABB.Width
                        = recAABB.Height
                        = 0;
                }
            }
        }

        PointF ptTL = new PointF();
        PointF ptBR = new PointF();

        void setMCRad()
        {
            float dblDelta_X = _pt2.X - _pt1.X;
            float dbLDelta_Y = _pt2.Y - _pt1.Y;
            if (dblDelta_X == 0)
                _M = float.MaxValue;
            else
                _M = dbLDelta_Y / dblDelta_X;
            // Y=mX+C -> C=Y-mX
            _B = _pt1.Y - _M * _pt1.X;
            _cRad.Radians = Math.Atan2(dbLDelta_Y, dblDelta_X);
            _cRad.Magnitude = Math.Sqrt(dblDelta_X * dblDelta_X + dbLDelta_Y * dbLDelta_Y);

            ptTL.X = pt1.X < pt2.X ? pt1.X : pt2.X;
            ptBR.X = pt1.X > pt2.X ? pt1.X : pt2.X;

            ptTL.Y = pt1.Y < pt2.Y ? pt1.Y : pt2.Y;
            ptBR.Y = pt1.Y > pt2.Y ? pt1.Y : pt2.Y;
        }

        classRadialCoordinate _cRad = new classRadialCoordinate();
        public classRadialCoordinate cRad
        {
            get { return _cRad; }
        }

        public bool ContainsPointF(PointF pt)
        {
            return (pt.X >= ptTL.X
                    && pt.X <= ptBR.X
                    && pt.Y >= ptTL.Y
                    && pt.Y <= ptBR.Y);
        }
        void setRec()
        {
            Point ptLocation = new Point((int)((pt1.X < pt2.X) ? pt1.X : pt2.X),(int)((pt1.Y < pt2.Y) ? pt1.Y : pt2.Y));
            Size sz = new Size((int)Math.Abs(pt1.X - pt2.X), (int)Math.Abs(pt1.Y - pt2.Y));
            recAABB = new Rectangle(ptLocation, sz);
        }

        public bool IntersectsLine(ref classLine cLine, ref PointF ptIntersection)
        {
            if (M == cLine.M) // lines are parallel
                return false;
            if (recAABB.Height == 0)
                setRec();
            if (cLine.recAABB.Width == 0)
                cLine.setRec();

            if (classMath3.RectanglesIntersect(recAABB, cLine.recAABB))
            {
                // y=Mx+C
                // y1=M1x1+C1 & y2=M2x2+C2
                // at y1=y2 & x1=x2
                // y = m1X+c1 = m2X+c2
                // c1-c2 = X(m2-m1)
                // x = (c1-c2)/(m2-m1)

                if (Math.Abs(cLine.M) > Math.Pow(2, 32))
                    ptIntersection.X = cLine.pt1.X;
                else if (Math.Abs(M) > Math.Pow(2, 32))
                    ptIntersection.X = pt1.X;
                else
                    ptIntersection.X = ((B - cLine.B) / (cLine.M - M));
                // y = xM+C -> x = (y-C)/M
                ptIntersection.Y = (ptIntersection.X * M + B);
                int intTolerance = 2;
                if ((pt1.X < pt2.X ? pt1.X : pt2.X) <= ptIntersection.X + intTolerance)
                {
                    if ((pt1.X > pt2.X ? pt1.X : pt2.X) >= ptIntersection.X - intTolerance )
                    {
                        if ((pt1.Y < pt2.Y ? pt1.Y : pt2.Y) <= ptIntersection.Y + intTolerance )
                        {
                            if ((pt1.Y > pt2.Y ? pt1.Y : pt2.Y) >= ptIntersection.Y -intTolerance )
                            {
                                if ((cLine.pt1.X < cLine.pt2.X ? cLine.pt1.X : cLine.pt2.X) <= ptIntersection.X + intTolerance )
                                {
                                    if ((cLine.pt1.X > cLine.pt2.X ? cLine.pt1.X : cLine.pt2.X) >= ptIntersection.X -intTolerance )
                                    {
                                        if ((cLine.pt1.Y < cLine.pt2.Y ? cLine.pt1.Y : cLine.pt2.Y) <= ptIntersection.Y + intTolerance )
                                        {
                                            if ((cLine.pt1.Y > cLine.pt2.Y ? cLine.pt1.Y : cLine.pt2.Y) >= ptIntersection.Y- intTolerance )
                                            {
                                                return true;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return false;
        }
    }

    public enum enuCorner { TL, TR, BR, BL, _num };
    public class classLineF
    {
        public classLineF() { }

        public classLineF(PointF ptf1, PointF ptf2)
        {
            _ptf1.X = ptf1.X;
            _ptf1.Y = ptf1.Y;

            _ptf2.X = ptf2.X;
            _ptf2.Y = ptf2.Y;
            setMCRad();
        }

        float _M;
        public float M
        {
            get { return _M; }
        }

        float _B;
        public float B
        {
            get { return _B; }
        }

        PointF _ptf1;
        public PointF ptf1
        {
            get { return _ptf1; }
            set
            {
                if (value.X != _ptf1.X
                    ||
                    value.Y != _ptf1.Y)
                {
                    _ptf1.X = value.X;
                    _ptf1.Y = value.Y;
                    setMCRad();
                }
            }
        }

        PointF _ptf2;
        public PointF ptf2
        {
            get { return _ptf2; }
            set
            {
                if (value.X != _ptf2.X
                    ||
                    value.Y != _ptf2.Y)
                {
                    _ptf2.X = value.X;
                    _ptf2.Y = value.Y;
                    setMCRad();
                }
            }
        }

        PointF ptfTL = new PointF();
        PointF ptfBR = new PointF();

        void setMCRad()
        {
            float dblDelta_X = _ptf2.X - _ptf1.X;
            float dbLDelta_Y = _ptf2.Y - _ptf1.Y;
            if (dblDelta_X == 0)
                _M = float.MaxValue;
            else
                _M = dbLDelta_Y / dblDelta_X;
            // Y=mX+C -> C=Y-mX
            _B = _ptf1.Y - _M * _ptf1.X;
            _cRad.Radians = Math.Atan2(dbLDelta_Y, dblDelta_X);
            _cRad.Magnitude = Math.Sqrt(dblDelta_X * dblDelta_X + dbLDelta_Y * dbLDelta_Y);

            ptfTL.X = ptf1.X < ptf2.X ? ptf1.X : ptf2.X;
            ptfBR.X = ptf1.X > ptf2.X ? ptf1.X : ptf2.X;

            ptfTL.Y = ptf1.Y < ptf2.Y ? ptf1.Y : ptf2.Y;
            ptfBR.Y = ptf1.Y > ptf2.Y ? ptf1.Y : ptf2.Y;
        }

        classRadialCoordinate _cRad = new classRadialCoordinate();
        public classRadialCoordinate cRad
        {
            get { return _cRad; }
        }

        public bool ContainsPointF(PointF ptf)
        {
            return (ptf.X >= ptfTL.X
                    && ptf.X <= ptfBR.X
                    && ptf.Y >= ptfTL.Y
                    && ptf.Y <= ptfBR.Y);
        }


        public bool IntersectsLine(ref classLineF cLine, ref PointF ptfIntersection)
        {
            if (M == cLine.M) // lines are parallel
                return false;

            // y=Mx+C
            // y1=M1x1+C1 & y2=M2x2+C2
            // at y1=y2 & x1=x2
            // y = m1X+c1 = m2X+c2
            // c1-c2 = X(m2-m1)
            // x = (c1-c2)/(m2-m1)

            if (Math.Abs(cLine.M) > Math.Pow(2, 32) || Math.Abs(M) > Math.Pow(2, 32))
            {
                ptfIntersection.X = Math.Abs(cLine.M) > Math.Abs(M)
                                                      ? (cLine.ptf1.X + cLine.ptf2.X) / 2
                                                      : (ptf1.X + ptf2.X) / 2;
            }
            else
                ptfIntersection.X = (B - cLine.B) / (cLine.M - M);


            // y = xM+C -> x = (y-C)/M
            if (Math.Abs(cLine.M) < Math.Pow(2, -32) || Math.Abs(M) < Math.Pow(2, -32))
            {
                ptfIntersection.Y = Math.Abs(cLine.M) < Math.Abs(M)
                                                       ? (cLine.ptf1.Y + cLine.ptf2.Y) / 2
                                                       : (ptf1.Y + ptf2.Y) / 2;
            }
            else
            {
                if (Math.Abs(M) > Math.Pow(2, 32))
                    ptfIntersection.Y = ptfIntersection.X * cLine.M + cLine.B;
                else
                    ptfIntersection.Y = ptfIntersection.X * M + B;
            }

            if ((ptf1.X < ptf2.X ? ptf1.X : ptf2.X) <= ptfIntersection.X)
            {
                if ((ptf1.X > ptf2.X ? ptf1.X : ptf2.X) >= ptfIntersection.X)
                {
                    if ((ptf1.Y < ptf2.Y ? ptf1.Y : ptf2.Y) <= ptfIntersection.Y)
                    {
                        if ((ptf1.Y > ptf2.Y ? ptf1.Y : ptf2.Y) >= ptfIntersection.Y)
                        {
                            if ((cLine.ptf1.X < cLine.ptf2.X ? cLine.ptf1.X : cLine.ptf2.X) <= ptfIntersection.X)
                            {
                                if ((cLine.ptf1.X > cLine.ptf2.X ? cLine.ptf1.X : cLine.ptf2.X) >= ptfIntersection.X)
                                {
                                    if ((cLine.ptf1.Y < cLine.ptf2.Y ? cLine.ptf1.Y : cLine.ptf2.Y) <= ptfIntersection.Y)
                                    {
                                        if ((cLine.ptf1.Y > cLine.ptf2.Y ? cLine.ptf1.Y : cLine.ptf2.Y) >= ptfIntersection.Y)
                                        {
                                            return true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return false;
        }
    }


}