Ejemplo n.º 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);
        }