コード例 #1
0
        public static void DrawTwoPoints(this Graphics g, Pen pen, PointF pf1, PointF pf2, Rectangle rect, bool smooth = true)
        {
            if (pf1.X.IsNan() || pf1.Y.IsNan() || pf2.X.IsNan() || pf2.Y.IsNan())
            {
                return;
            }
            bool haveLargePixel = Math.Abs(pf1.X - pf2.X) >= rect.Width * 100 || Math.Abs(pf1.Y - pf2.Y) >= rect.Height * 100;

            //两点都在区域内
            if (pf1.InRect(rect) && pf2.InRect(rect))
            {
                g.Smooth(smooth);
                g.DrawLine(pen, pf1, pf2);
                g.Smooth(false);
                return;
            }

            //无大坐标像素
            if (!haveLargePixel)
            {
                g.Smooth(smooth);
                g.DrawLine(pen, pf1, pf2);
                g.Smooth(false);
                return;
            }

            //垂直线
            if (pf1.X.Equals(pf2.X))
            {
                if (pf1.X <= rect.Left)
                {
                    return;
                }
                if (pf1.X >= rect.Right)
                {
                    return;
                }
                if (pf1.Y <= rect.Top && pf2.Y <= rect.Top)
                {
                    return;
                }
                if (pf1.Y >= rect.Bottom && pf2.Y >= rect.Bottom)
                {
                    return;
                }

                g.Smooth(smooth);
                float yy1 = Math.Min(pf1.Y, pf2.Y);
                float yy2 = Math.Max(pf1.Y, pf2.Y);
                if (yy1 <= rect.Top)
                {
                    if (yy2 <= rect.Bottom)
                    {
                        g.DrawLine(pen, pf1.X, rect.Top, pf1.X, yy2);
                    }
                    else
                    {
                        g.DrawLine(pen, pf1.X, rect.Top, pf1.X, rect.Bottom);
                    }
                }
                else
                {
                    if (yy2 <= rect.Bottom)
                    {
                        g.DrawLine(pen, pf1.X, yy1, pf1.X, yy2);
                    }
                    else
                    {
                        g.DrawLine(pen, pf1.X, yy1, pf1.X, rect.Bottom);
                    }
                }

                g.Smooth(false);
                return;
            }

            //水平线
            if (pf1.Y.Equals(pf2.Y))
            {
                if (pf1.Y <= rect.Top)
                {
                    return;
                }
                if (pf1.Y >= rect.Bottom)
                {
                    return;
                }
                if (pf1.X <= rect.Left && pf2.X <= rect.Left)
                {
                    return;
                }
                if (pf1.X >= rect.Right && pf2.X >= rect.Right)
                {
                    return;
                }

                g.Smooth(smooth);
                float xx1 = Math.Min(pf1.X, pf2.X);
                float xx2 = Math.Max(pf1.X, pf2.X);
                if (xx1 <= rect.Left)
                {
                    if (xx2 <= rect.Right)
                    {
                        g.DrawLine(pen, rect.Left, pf1.Y, xx2, pf1.Y);
                    }
                    else
                    {
                        g.DrawLine(pen, rect.Left, pf1.Y, rect.Right, pf1.Y);
                    }
                }
                else
                {
                    if (xx2 <= rect.Right)
                    {
                        g.DrawLine(pen, xx1, pf1.Y, xx2, pf1.Y);
                    }
                    else
                    {
                        g.DrawLine(pen, xx1, pf1.Y, rect.Right, pf1.Y);
                    }
                }

                g.Smooth(false);
                return;
            }

            //判断两个区域是否相交
            RectangleF rect1 = pf1.CreateRectangleF(pf2);

            if (!rect1.IsOverlap(rect))
            {
                return;
            }

            double x1 = Drawing.CalcX(pf1, pf2, rect.Top);
            double x2 = Drawing.CalcX(pf1, pf2, rect.Bottom);
            double y1 = Drawing.CalcY(pf1, pf2, rect.Left);
            double y2 = Drawing.CalcY(pf1, pf2, rect.Right);

            //判断线段是否和区域有交点
            bool isExist = x1.InRange(rect.Left, rect.Right) || x2.InRange(rect.Left, rect.Right) || y1.InRange(rect.Top, rect.Bottom) || y2.InRange(rect.Top, rect.Bottom);

            if (!isExist)
            {
                return;
            }

            List <PointF> TwoPoints = new List <PointF>();

            if (!pf1.InRect(rect) && !pf2.InRect(rect))
            {
                if (x1.InRange(rect.Left, rect.Right))
                {
                    TwoPoints.Add(new PointF((float)x1, rect.Top));
                }
                if (x2.InRange(rect.Left, rect.Right))
                {
                    TwoPoints.Add(new PointF((float)x2, rect.Bottom));
                }
                if (y1.InRange(rect.Top, rect.Bottom))
                {
                    TwoPoints.Add(new PointF(rect.Left, (float)y1));
                }
                if (y2.InRange(rect.Top, rect.Bottom))
                {
                    TwoPoints.Add(new PointF(rect.Right, (float)y2));
                }
            }
            else
            {
                PointF center = pf1.InRect(rect) ? pf1 : pf2;
                PointF border = pf2.InRect(rect) ? pf1 : pf2;
                TwoPoints.Add(center);
                if (border.X >= center.X)
                {
                    if (border.Y >= center.Y)
                    {
                        TwoPoints.Add(x2 <= rect.Right ? new PointF((float)x2, rect.Bottom) : new PointF(rect.Right, (float)y2));
                    }
                    else
                    {
                        TwoPoints.Add(x1 <= rect.Right ? new PointF((float)x1, rect.Top) : new PointF(rect.Right, (float)y2));
                    }
                }
                else
                {
                    if (border.Y >= center.Y)
                    {
                        TwoPoints.Add(x2 >= rect.Left ? new PointF((float)x2, rect.Bottom) : new PointF(rect.Left, (float)y1));
                    }
                    else
                    {
                        TwoPoints.Add(x1 >= rect.Left ? new PointF((float)x1, rect.Bottom) : new PointF(rect.Left, (float)y1));
                    }
                }
            }

            if (TwoPoints.Count == 2)
            {
                g.Smooth(smooth);
                g.DrawLine(pen, TwoPoints[0], TwoPoints[1]);
                g.Smooth(false);
            }
            else
            {
                Console.WriteLine(TwoPoints.Count);
            }

            TwoPoints.Clear();
        }