예제 #1
0
        public static StylusPointCollection GetArrowCorner(StylusPointCollection collection, double ratio = 5)
        {
            try
            {
                StylusPointCollection corners = GetTriangleCorner(collection);
                if (corners != null && corners.Count == 4)
                {
                    StylusPoint first       = collection[0];
                    StylusPoint end         = collection[collection.Count - 1];
                    StylusPoint firstCorner = corners[0];
                    StylusPoint endCorner   = corners[1];

                    double minFistDis = double.MaxValue;
                    double minEndDis  = double.MaxValue;
                    foreach (StylusPoint point in corners)
                    {
                        double dis    = MathMethods.GetPowDistanceBetTowPoints(point.X, point.Y, first.X, first.Y);
                        double endDis = MathMethods.GetPowDistanceBetTowPoints(point.X, point.Y, end.X, end.Y);

                        if (minFistDis > dis)
                        {
                            minFistDis  = dis;
                            firstCorner = point;
                        }

                        if (minEndDis > endDis)
                        {
                            minEndDis = endDis;
                            endCorner = point;
                        }
                    }

                    StylusPoint centerCorner = corners.FirstOrDefault(item => item != firstCorner && item != endCorner);
                    if (centerCorner != null && centerCorner.X > 0 && centerCorner.Y > 0)
                    {
                        double cf = MathMethods.GetDistanceBetTowPoints(centerCorner.X, centerCorner.Y, firstCorner.X, firstCorner.Y);
                        double ce = MathMethods.GetDistanceBetTowPoints(centerCorner.X, centerCorner.Y, endCorner.X, endCorner.Y);

                        double cf_ce = Math.Abs(cf - ce);
                        if (cf_ce > 150)
                        {
                            return(null);
                        }
                        double fe = MathMethods.GetDistanceBetTowPoints(firstCorner.X, firstCorner.Y, endCorner.X, endCorner.Y);
                        // cosA = (c^2 + b^2 - a^2) / (2·b·c)
                        double cosA = (cf * cf + ce * ce - fe * fe) / (2 * cf * ce);
                        if (cosA < 0.04 || cosA > 0.93)
                        {
                            return(null);
                        }

                        #region Check bound is two line
                        //Get line one function
                        double line1K = (centerCorner.Y - firstCorner.Y) / (centerCorner.X - firstCorner.X);
                        double line1A = line1K;
                        double line1B = -1;
                        double line1C = centerCorner.Y - line1K * centerCorner.X;

                        //Get line two function
                        double line2K = (centerCorner.Y - endCorner.Y) / (centerCorner.X - endCorner.X);
                        double line2A = line2K;
                        double line2B = -1;
                        double line2C = centerCorner.Y - line2K * centerCorner.X;

                        int line1Count = 0;
                        int line2Count = 0;

                        double lineDis1    = MathMethods.GetDistanceBetTowPoints(centerCorner.X, centerCorner.Y, firstCorner.X, firstCorner.Y);
                        double lineDis2    = MathMethods.GetDistanceBetTowPoints(centerCorner.X, centerCorner.Y, endCorner.X, endCorner.Y);
                        double line1offset = lineDis1 * ratio / 100;
                        double line2offset = lineDis2 * ratio / 100;
                        for (int i = 0; i < collection.Count; i++)
                        {
                            double dis1 = MathMethods.GetDistanceBetPointToLine(collection[i].X, collection[i].Y, line1A, line1B, line1C);


                            if (dis1 < line1offset)
                            {
                                line1Count++;
                            }
                            else
                            {
                                double dis2 = MathMethods.GetDistanceBetPointToLine(collection[i].X, collection[i].Y, line2A, line2B, line2C);
                                if (dis2 < line2offset)
                                {
                                    line2Count++;
                                }
                            }
                        }

                        if (collection.Count - (line1Count + line2Count) > collection.Count / 20)
                        {
                            return(null);
                        }
                        #endregion

                        StylusPoint maxDisPoint  = firstCorner;
                        StylusPoint minDisPoint  = endCorner;
                        double      maxDisCenter = cf;
                        double      minDisCenter = ce;
                        if (cf < ce)
                        {
                            maxDisPoint  = endCorner;
                            minDisPoint  = firstCorner;
                            maxDisCenter = ce;
                            minDisCenter = cf;
                        }

                        //line:y=a+bx;
                        double b = (maxDisPoint.Y - centerCorner.Y) / (maxDisPoint.X - centerCorner.X); //(y1-y2)/(x1-x2)
                        double a = 0 - b * centerCorner.X + centerCorner.Y;                             //-b*x1+y2

                        double startTest   = Math.Min(maxDisPoint.X, centerCorner.X);
                        double endTest     = Math.Max(maxDisPoint.X, centerCorner.X);
                        Point  targetPoint = new Point(maxDisPoint.X, maxDisPoint.Y);

                        for (double i = startTest; i < endTest; i += 1)
                        {
                            double y   = a + b * i;
                            double dis = MathMethods.GetDistanceBetTowPoints(i, y, centerCorner.X, centerCorner.Y);
                            if (Math.Abs(minDisCenter - dis) <= 5)
                            {
                                targetPoint.X = i;
                                targetPoint.Y = y;
                                break;
                            }
                        }

                        if (targetPoint.X <= 0 || centerCorner.X <= 0)
                        {
#if DEBUG
                            throw new Exception("targetPoint.X should noe be zero");
#endif
                        }
                        StylusPointCollection result = new StylusPointCollection();

                        result.Add(new StylusPoint(targetPoint.X, targetPoint.Y, 0.5f));
                        result.Add(centerCorner);
                        result.Add(minDisPoint);

                        return(result);
                    }
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.StackTrace);
#if DEBUG
                throw ex;
#endif
            }
            return(null);
        }
예제 #2
0
        public Point GetCenter(StylusPointCollection collection, out double aRadius, out double bRadius)
        {
            //y=a+bx;
            double xAverage = 0.0;
            double yAverage = 0.0;

            aRadius = 0.0;
            bRadius = 0.0;
            double b = 0.0;
            double a = 0.0;

            Point centerPoint = new Point();

            foreach (var point in collection)
            {
                xAverage += point.X;
                yAverage += point.Y;
            }

            xAverage = xAverage / collection.Count;
            yAverage = yAverage / collection.Count;

            double fenzi = 0.0;
            double fenmu = 0.0;

            foreach (var point in collection)
            {
                fenzi += point.X * point.Y;
                fenmu += point.X * point.X;
            }

            fenzi = fenzi - collection.Count * xAverage * yAverage;
            fenmu = fenmu - collection.Count * xAverage * xAverage;
            if (fenmu != 0)
            {
                b = fenzi / fenmu;
            }
            if (fenmu == 0 || fenzi == 0 || Math.Abs(b) > 6)
            {
                //vertical line
                double maxY = double.MinValue;
                double minY = double.MaxValue;
                double maxX = double.MinValue;
                double minX = double.MaxValue;

                foreach (var point in collection)
                {
                    if (maxY < point.Y)
                    {
                        maxY = point.Y;
                    }
                    if (minY > point.Y)
                    {
                        minY = point.Y;
                    }

                    if (maxX < point.X)
                    {
                        maxX = point.X;
                    }
                    if (minX > point.X)
                    {
                        minX = point.X;
                    }
                }
                double centerY = (maxY + minY) / 2;
                centerPoint.X = xAverage;
                centerPoint.Y = centerY;
                bRadius       = (maxY - minY) / 2;
                aRadius       = (maxX - minX) / 2;
                return(centerPoint);
            }
            else
            {
                a = yAverage - b * xAverage;
                StylusPoint maxDisPoint = collection[0];
                StylusPoint minDisPoint = collection[0];
                double      maxDis      = double.MinValue;
                double      minDis      = double.MaxValue;
                foreach (var point in collection)
                {
                    //bx-y+a=0;
                    double dis = MathMethods.GetDistanceBetPointToLine(point.X, point.Y, b, -1, a);
                    if (dis > maxDis)
                    {
                        maxDis      = dis;
                        maxDisPoint = point;
                    }
                    if (dis < minDis)
                    {
                        minDis      = dis;
                        minDisPoint = point;
                    }
                }
            }
            return(centerPoint);
        }