Esempio n. 1
0
        /// <summary>
        /// 返回矩形中心点与指定点的向量,与矩形相交的点。
        /// 简单地认为,这个点即是矩形边上最靠近指定点的点。
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="point"></param>
        /// <returns></returns>
        public static Point NearestPointOnLine(Rect rect, Point point)
        {
            Point res = point;

            if (rect.Contains(res))
            {
                var center = rect.GetCenterPoint();

                //没有斜率
                if (res.Y == center.Y)
                {
                    if (res.X < center.X)
                    {
                        res = RectAlgorithm.GetRectAbsolute(rect, new Point(0, 0.5));
                    }
                    else
                    {
                        res = RectAlgorithm.GetRectAbsolute(rect, new Point(1, 0.5));
                    }
                    return(res);
                }
                else
                {
                    //使用向量继续向该方向延长。
                    Vector v = new Vector(res.X - center.X, res.Y - center.Y);
                    v *= 10000;
                    while (true)
                    {
                        res += v;
                        if (!rect.Contains(res))
                        {
                            break;
                        }
                    }
                }
            }

            res = CalcCrossPoint(rect, point);

            return(res);
        }