我现在坐标轴上有N个动态点存在数组里,
我鼠标点击的时候获取了一个新的点pointA,
我现在想算出pointA靠数组里的点距离近的点,

解决方案 »

  1.   


     List<XY> lxys = new List<XY>();
            private void Form3_Load(object sender, EventArgs e)
            {
                lxys.Add(new XY(10, 10));
                lxys.Add(new XY(200, 200));
                lxys.Add(new XY(400, 400));
                lxys.Add(new XY(800, 800));
                lxys.Add(new XY(1000, 1000));
            }        public class XY
            {
                public XY(int x, int y)
                {
                    this.x = x;
                    this.y = y;
                }
                private int x;
                public int X
                {
                    get { return x; }
                    set { x = value; }
                }
                private int y;            public int Y
                {
                    get { return y; }
                    set { y = value; }
                }            public int GetXY(int ex, int ey)
                {
                    return Math.Abs(ex - x) + Math.Abs(ey - y);            }
            }        private void Form3_MouseDown(object sender, MouseEventArgs e)
            {
                XY lsxy = null;
                foreach (XY xy in lxys)
                {
                    int x = Math.Abs(e.X - xy.X);
                    int y = Math.Abs(e.Y - xy.Y);
                    if (lsxy == null)
                        lsxy = xy;
                    else if (lsxy.GetXY(e.X, e.Y) > x + y)
                        lsxy = xy;
                }
                MessageBox.Show("鼠标点的坐标是x:" + e.X + " y:" + e.Y + "\n\r最接近的点是 x:" + lsxy.X + " y:" + lsxy.Y);
            }
      

  2.   

            private Point GetPoint(Point p)
            {
                Point pp = new Point();
                for (int i = 0; i < Pointes.Count; i++)
                {
                    for (int z = 1; z < Pointes.Count; z++)
                    {
                        if (Math.Sqrt(Math.Pow(Math.Abs(p.X - Pointes[i].X), 2) + Math.Pow(Math.Abs(p.Y - Pointes[i].Y), 2)) > Math.Sqrt(Math.Pow(Math.Abs(p.X - Pointes[z].X), 2) + Math.Pow(Math.Abs(p.Y - Pointes[z].Y), 2)) ? true : false)
                        {
                            pp=Pointes[z];
                        }
                        else
                        {
                            pp=Pointes[i];
                        }                }
                }
               
                return pp;
            }帮我看看有没有什么问题?