using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Text;

namespace 试验版
{
    [Serializable]//可序列化
    public class liness
    {
        public RectangleF rect;
        public PointF pos;
        public PointF pos2;
        public PointF middlePos;
        public Pen pen = new Pen(Color.Black);

        public dian fatherdian;
        
        public String sbname;



        public liness(PointF p1, PointF p2, dian ddd, Graphics g)
        {
            Bitmap bitmap = new Bitmap(200, 200);

            fatherdian = ddd;
            pos = p1;
            pos2 = p2;
            
            sbname = "ll";
                
           
        }


        public virtual bool ispick(Point p)
        {
            PointF middlePos = new PointF((pos.X + pos2.X) / 2, (pos.Y + pos2.Y) / 2);

            if ((p.X >= pos.X) && (p.X <= middlePos.X) && (p.Y >= pos.Y - 10) && (p.Y <= pos.Y + 10))
            {
                return true;
            }
            else if ((p.X >= middlePos.X - 10) && (p.X <= middlePos.X + 10) && (p.Y >= pos.Y - 10) && (p.Y <= pos.Y + 10))
            {
                return true;
            }
            else if ((p.X >= middlePos.X - 10) && (p.X <= middlePos.X + 10) && (p.Y >= pos.Y - 10) && (p.Y <= pos.Y + 10))
            {
                return true;
            }
            else
            {
                return false;
            }

        }



        // 判断鼠标点击是否在线上
        public bool IsPointNearLine(PointF clickPos)
        {
            float tolerance = 5.0f;
            // 计算鼠标点击到线的距离是否小于容差
            PointF middlePos = new PointF((pos.X + pos2.X) / 2, (pos.Y + pos2.Y) / 2);

            if(PointToLineDistance(clickPos, pos, middlePos) <= tolerance)
            {
                return true;
            }
            else if(PointToLineDistance(clickPos, middlePos, new PointF(middlePos.X, pos2.Y)) <= tolerance)
            {
                return true;
            }
            else if (PointToLineDistance(clickPos, new PointF(middlePos.X, pos2.Y), pos2) <= tolerance)
            {
                return true;
            }
            else
                return false;

        }

        // 计算点到线段的距离
        private float PointToLineDistance(PointF p, PointF lineStart, PointF lineEnd)
        {
            float A = p.X - lineStart.X;
            float B = p.Y - lineStart.Y;
            float C = lineEnd.X - lineStart.X;
            float D = lineEnd.Y - lineStart.Y;
            
            float dot = A * C + B * D;
            float lenSq = C * C + D * D;
            float param = (lenSq != 0) ? dot / lenSq : -1;

            float xx, yy;
            if (param < 0)
            {
                xx = lineStart.X;
                yy = lineStart.Y;
            }
            else if (param > 1)
            {
                xx = lineEnd.X;
                yy = lineEnd.Y;
            }
            else
            {
                xx = lineStart.X + param * C;
                yy = lineStart.Y + param * D;
            }

            float dx = p.X - xx;
            float dy = p.Y - yy;
            return (float)Math.Sqrt(dx * dx + dy * dy);
        }
    







        public virtual void draw(Graphics g, Pen mypen)
        {
            PointF middlePos = new PointF((pos.X + pos2.X) / 2, (pos.Y + pos2.Y) / 2);
            g.DrawLine(pen, pos.X, pos.Y, middlePos.X, pos.Y);
            g.DrawLine(pen, middlePos.X, pos.Y, middlePos.X, pos2.Y);
            g.DrawLine(pen, middlePos.X, pos2.Y, pos2.X, pos2.Y);
        }









    }
}
