public jumptoanchor(int _x, int _y, int _h, node _thisnode)
 {
     x = _x;
     y = _y;
     h = _h;
     thisnode = _thisnode;
 }
        public void func_mle_dvhop()
        {
            double ci = func_mle_getci();
            func_mle_searchpath(mle_testNode);
            List<node> attendanchor = new List<node>();
            List<double> d = new List<double>();
            foreach (jumptoanchor temp_jump in mle_testNode.list_anchor)
            {
                d.Add(ci * temp_jump.h);
                attendanchor.Add(temp_jump.thisnode);
            }

            //获得a矩阵
            Matrix a = new Matrix(attendanchor.Count() - 1, 2);
            for (int i = 0; i < attendanchor.Count() - 1; i++)
            {
                a[i, 0] = 2 * (attendanchor[i].X - attendanchor.Last().X);
                a[i, 1] = 2 * (attendanchor[i].Y - attendanchor.Last().Y);
            }
            Matrix at = Matrix.transpose(a);

            //获得b矩阵
            Matrix b = new Matrix(attendanchor.Count() - 1, 1);
            double temp_lastnodex2 = Math.Pow(attendanchor.Last().X, 2);
            double temp_lastnodey2 = Math.Pow(attendanchor.Last().Y, 2);
            for (int i = 0; i < attendanchor.Count() - 1; i++)
            {
                b[i, 0] = Math.Pow(attendanchor[i].X, 2) - temp_lastnodex2 + Math.Pow(attendanchor[i].Y, 2) - temp_lastnodey2 + Math.Pow(d.Last(), 2) - Math.Pow(d[i], 2);
            }

            Matrix atb = at * b;
            Matrix ata = at * a;
            Matrix ata_1 = Matrix.converse(ata);
            if (ata_1 == null)
            {
                label_mle_dvHOP.Text = "锚点位置有误,无法计算位置。";
                return;
            }
            Matrix x = ata_1 * atb;

            node tempnode = new node();
            tempnode.Location = new Point((int)x[0, 0], (int)x[1, 0]);
            double wuchadouble = func_getDistence(tempnode, mle_testNode);
            label_mle_dvHOP.Text = "DV-HOP法估计的坐标为:" + tempnode.X.ToString() + "," + tempnode.Y.ToString()
                + "\n实际坐标为:" + mle_testNode.X.ToString() + "," + mle_testNode.Y.ToString()
                + "\n误差为:" + wuchadouble.ToString("0.00");
        }
 //复制一个_tempnode节点的复件
 public node(node _tempnode)
 {
     list_neighbour = _tempnode.list_neighbour;
     list_anchor = _tempnode.list_anchor;      
     this.BackColor = _tempnode.BackColor;
     this.Location = _tempnode.Location;
     this.Size = _tempnode.Size;
     this.ClientSize = _tempnode.ClientSize;
     this.r = _tempnode.r;
     this.type = _tempnode.type;
     timer_changenodeposition = new Timer();
 }
        public double func_mle_jidasiran(node _node, int _wucha)
        {
            //用Random随机制造到所有锚点的距离(有误差)储存在d中
            Random temp_random = new Random();
            List<double> d = new List<double>();
            foreach (node temp_node in mle_anchorList)
            {
                d.Add(func_getDistence(_node, temp_node) + (temp_random.NextDouble() - 0.5) * _wucha * 2);
            }
            //获得a矩阵
            Matrix a = new Matrix(mle_anchorList.Count() - 1, 2);
            for (int i = 0; i < mle_anchorList.Count() - 1; i++)
            {
                a[i, 0] = 2 * (mle_anchorList[i].X - mle_anchorList.Last().X);
                a[i, 1] = 2 * (mle_anchorList[i].Y - mle_anchorList.Last().Y);
            }
            Matrix at = Matrix.transpose(a);

            //获得b矩阵
            Matrix b = new Matrix(mle_anchorList.Count() - 1, 1);
            double temp_lastnodex2 = Math.Pow(mle_anchorList.Last().X, 2);
            double temp_lastnodey2 = Math.Pow(mle_anchorList.Last().Y, 2);
            for (int i = 0; i < mle_anchorList.Count() - 1; i++)
            {
                b[i, 0] = Math.Pow(mle_anchorList[i].X, 2) - temp_lastnodex2 + Math.Pow(mle_anchorList[i].Y, 2) - temp_lastnodey2 + Math.Pow(d.Last(), 2) - Math.Pow(d[i], 2);
            }

            Matrix atb = at * b;
            Matrix ata = at * a;
            Matrix ata_1 = Matrix.converse(ata);
            if (ata_1 == null)
            {
                label_mle_mleAnswer.Text = "锚点位置有误,无法计算位置。";
                return -1;
            }
            Matrix x = ata_1 * atb;

            node tempnode = new node();
            tempnode.Location = new Point((int)x[0, 0], (int)x[1, 0]);
            double wuchadouble = func_getDistence(tempnode, _node);
            label_mle_mleAnswer.Text = "极大似然法估计的坐标为:" + tempnode.X.ToString() + "," + tempnode.Y.ToString()
                + "\n实际坐标为:" + _node.X.ToString() + "," + _node.Y.ToString()
                + "\n误差为:" + wuchadouble.ToString("0.00");
            return wuchadouble;
        }
 public double func_mle_zhixindingwei(node _node)
 {
     node tempnode = new node();
     int zhixinx = 0, zhixiny = 0;
     int temp_nodecount = 0;
     foreach (node temp_node in mle_anchorList)
     {
         if (func_getDistence(temp_node, _node) < _node.r)
         {
             zhixinx += temp_node.X;
             zhixiny += temp_node.Y;
             temp_nodecount++;
         }
     }
     if (temp_nodecount == 0)
     {
         label_mle_cl.Text = "当前范围内未找到节点,无法估计位置。";
         return -1;
     }
     zhixinx /= temp_nodecount;
     zhixiny /= temp_nodecount;
     tempnode.Location = new Point(zhixinx, zhixiny);
     double wuchadouble = func_getDistence(tempnode, _node);
     label_mle_cl.Text = "质心定位法估计的坐标为:" + tempnode.X.ToString() + "," + tempnode.Y.ToString()
         + "\n实际坐标为:" + _node.X.ToString() + "," + _node.Y.ToString()
         + "\n误差为:" + wuchadouble.ToString("0.00");
     return wuchadouble;
 }
        private void event_button_mle_start_Click(object sender, EventArgs e)
        {
            int mle_nodeNumber;
            int mle_anchorNumber;
            int mle_nodeR;
            if (mle_anchorList == null)
            {
                mle_anchorList = new List<node>();
            }
            if (mle_nodeList == null)
            {
                mle_nodeList = new List<node>();
            }
            //清空
            mle_nodeList.Clear();
            mle_anchorList.Clear();
            panel_mle_mainArea.Controls.Clear();

            try
            {
                mle_anchorNumber = int.Parse(textBox_mle_anchorNumber.Text);
                mle_maxTolerance = int.Parse(textBox_mle_tolerance.Text);
                mle_nodeNumber = int.Parse(textBox_mle_normalNumber.Text);
                mle_nodeR = int.Parse(textBox_mle_nodeR.Text);
            }
            catch
            {
                wrongTip_mle_input.Show();
                return;
            }
            Point test_point;
            for (int i = 0; i < mle_anchorNumber; i++)
            {
                test_point = func_mle_getARandomPoint();
                node mle_tempNode = new node(test_point.X, test_point.Y, mle_nodeR, node.nodeType.anchor);
                func_mle_addNode(mle_tempNode);
            }
            for (int i = 0; i < mle_nodeNumber; i++)
            {
                test_point = func_mle_getARandomPoint();
                node mle_tempNode = new node(test_point.X, test_point.Y, mle_nodeR, node.nodeType.normal);
                func_mle_addNode(mle_tempNode);
            }
            test_point = func_mle_getARandomPoint();
            mle_testNode = new node(test_point.X, test_point.Y, mle_nodeR, node.nodeType.test);
            mle_testNode.BackColor = Color.AliceBlue;
            func_mle_addNode(mle_testNode);
            this.Refresh();
        }
 public void func_mle_addNode(node _temp_node)
 {
     _temp_node.MouseDown += event_node_MouseDown;
     _temp_node.MouseUp += event_node_MouseUp;
     if (_temp_node.type == node.nodeType.normal)
     {
         _temp_node.BackColor = Color.DarkOrange;
         mle_nodeList.Add(_temp_node);
     }
     else if (_temp_node.type == node.nodeType.anchor)
     {
         mle_anchorList.Add(_temp_node);
         _temp_node.BackColor = Color.DarkCyan;
     }
     else
     {
         _temp_node.BackColor = Color.DarkRed;
     }
     panel_mle_mainArea.Controls.Add(_temp_node);
 }
        private void timer_flooding_onceJump_Tick(object sender, EventArgs e)
        {
            if (flooding_lastStartNode == null || flooding_lastEndNode == null)
            {
                flooding_lastStartNode = flooding_startNode;
                flooding_lastEndNode = flooding_endNode;
            }
            else
            {
                if (flooding_startNode != flooding_lastStartNode || flooding_endNode != flooding_lastEndNode)
                {
                    foreach (node temp_node in flooding_nodeList)
                    {
                        temp_node.BackColor = Color.DarkGray;
                    }
                    flooding_startNode.BackColor = Color.Blue;
                    flooding_endNode.BackColor = Color.Green;
                    flooding_tickNode = new List<node>();
                    flooding_tickNode.Add(flooding_startNode);
                    flag_flooding_isfound = false;
                    flooding_lastStartNode = flooding_startNode;
                    flooding_lastEndNode = flooding_endNode;
                }
            }
            if (flag_flooding_isfound)
            {
                foreach (node temp_node in flooding_nodeList)
                {
                    temp_node.BackColor = Color.DarkGray;
                }
                flooding_startNode.BackColor = Color.Blue;
                flooding_endNode.BackColor = Color.Green;
                timer_flooding_onceJump.Stop();
                flooding_tickNode = new List<node>();
                flooding_tickNode.Add(flooding_startNode);
                flag_flooding_isfound = false;
                return;
            }

            List<node> flooding_temp_nodeList = new List<node>();
            if (flooding_tickNode == null)
            {
                flooding_tickNode = new List<node>();
                flooding_tickNode.Add(flooding_startNode);
            }
            foreach (node temp_node in flooding_tickNode)
            {
                foreach (node temp_nei_node in temp_node.list_neighbour)
                {
                    if (!flooding_temp_nodeList.Contains(temp_nei_node))
                        flooding_temp_nodeList.Add(temp_nei_node);
                    temp_nei_node.BackColor = Color.Orange;
                    if (temp_nei_node == flooding_endNode)
                        flag_flooding_isfound = true;
                }
            }
            //this.Refresh();
            foreach (node temp_node in flooding_tickNode)
            {
                temp_node.BackColor = Color.DarkRed;
            }
            flooding_startNode.BackColor = Color.Blue;
            flooding_endNode.BackColor = Color.Green;

            flooding_tickNode = flooding_temp_nodeList;
        }
 private void button_flooding_rebuild_Click(object sender, EventArgs e)
 {
     panel_flooding_mainArea.Controls.Clear();
     flooding_nodeList.Clear();
     button_flooding_start.PerformClick();
     timer_flooding_onceJump.Stop();
     flooding_tickNode = null;
     flag_flooding_isfound = false;
     flooding_endNode = null;
     flooding_startNode = null;
 }
 public void func_flooding_createNodeList(int _numebr, int _r, Rectangle _sensorArea)
 {
     Random temp_random = new Random();
     List<node> temp_flooding_nodeList = new List<node>();
     for (int i = 0; i < _numebr; i++)
     {
         int temp_x = temp_random.Next(_sensorArea.Width);
         int temp_y = temp_random.Next(_sensorArea.Height);
         node temp_node = new node(temp_x, temp_y, _r, node.nodeType.normal);
         temp_node.MouseDown += event_node_MouseDown;
         temp_node.MouseUp += event_node_MouseUp;
         temp_flooding_nodeList.Add(temp_node);
     }
     flooding_nodeList = temp_flooding_nodeList;
 }
 public void func_flooding_getPath()
 {
     flooding_jumpTime = 0;
     if (flooding_BFS_queue == null)
     {
         flooding_BFS_queue = new List<node>();
     }
     flooding_BFS_queue.Clear();
     flooding_BFS_queue.Add(flooding_startNode);
     foreach (node temp_node in flooding_nodeList)
     {
         temp_node.bufferNode = null;
         if (temp_node != flooding_startNode && temp_node != flooding_endNode)
             temp_node.BackColor = Color.DarkGray;
     }
     if (flooding_startNode == flooding_endNode)
     {
         label_flooding_answer.Text = "一共需要0跳";
         return;
     }
     func_flooding_searchPath();
     if (flooding_endNode.bufferNode == null)
     {
         label_flooding_answer.Text = "找不到路径";
         return;
     }
     node beforenode = flooding_endNode;
     beforenode.BringToFront();
     while (beforenode != flooding_startNode)
     {
         if (beforenode != flooding_startNode && beforenode != flooding_endNode)
             beforenode.BackColor = Color.DarkOrange;
         beforenode.BringToFront();
         beforenode = beforenode.bufferNode;
         flooding_jumpTime++;
     }
     node temp_flooding_startNode = new node(flooding_startNode);
     label_flooding_answer.Text = "一共需要" + flooding_jumpTime.ToString() + "跳";
 }
        private void button_flooding_start_Click(object sender, EventArgs e)
        {
            int temp_flooding_nodeNumber;
            int temp_flooding_R;
            if (flooding_nodeList == null)
            {
                flooding_nodeList = new List<node>();
            }
            //读入数据
            try
            {
                temp_flooding_nodeNumber = int.Parse(textBox_flooding_pointNumber.Text);
                temp_flooding_R = int.Parse(textBox_flooding_R.Text);
                if (temp_flooding_nodeNumber < 1 || temp_flooding_R < 3)
                    throw new Exception();
            }
            catch
            {
                wrongTip_flooding_tip3.Show();
                return;
            }

            label_flooding_answer.Text = "";
            foreach (node temp_node in flooding_nodeList)
            {
                temp_node.BackColor = Color.DarkGray;
            }
            //如果要求节点数与原节点数不同则随机生成节点
            if (flooding_nodeList.Count != temp_flooding_nodeNumber)
            {
                flooding_startNode = null;
                flooding_endNode = null;
                panel_flooding_mainArea.Controls.Clear();
                flooding_nodeList.Clear();
                func_flooding_createNodeList(temp_flooding_nodeNumber, temp_flooding_R, new Rectangle(new Point(0, 0), panel_flooding_mainArea.Size));
                foreach (node temp_node in flooding_nodeList)
                {
                    temp_node.MouseDown += event_node_MouseDown;
                    temp_node.MouseUp += event_node_MouseUp;
                    temp_node.Click += event_hideSideMenu;
                }
            }
            foreach (node temp_node in flooding_nodeList)
            {
                temp_node.changer(temp_flooding_R);
                temp_node.bufferNode = null;
                this.panel_flooding_mainArea.Controls.Add(temp_node);
            }
            func_flooding_setLink();
            if (flooding_startNode != null && flooding_endNode != null)
            {
                flooding_startNode.BackColor = Color.Blue;
                flooding_endNode.BackColor = Color.Green;
                if (checkBox_flooding_onceJump.Checked)
                    timer_flooding_onceJump.Start();
                else
                    func_flooding_getPath();
            }
        }
        private void event_node_MouseDown(object sender, MouseEventArgs e)
        {
            moving = (node)sender;
            Point temp_location = ((node)sender).Location;
            dx = temp_location.X - MousePosition.X;
            dy = temp_location.Y - MousePosition.Y;


            if (nowShowingPanel == mainPanel.flooding)
            {
                #region flooding

                timer_changeNodePosition.Start();
                if (e.Button == MouseButtons.Left)
                {
                    if (flooding_startNode != null)
                    {
                        flooding_startNode.BackColor = Color.DarkGray;
                    }
                    if (flooding_endNode != null)
                        flooding_endNode.BackColor = Color.Green;
                    ((node)sender).BackColor = Color.Blue;
                    ((node)sender).BringToFront();
                    flooding_startNode = (node)sender;
                }
                if (e.Button == MouseButtons.Right)
                {
                    if (flooding_endNode != null)
                        flooding_endNode.BackColor = Color.DarkGray;
                    if (flooding_startNode != null)
                        flooding_startNode.BackColor = Color.Blue;
                    ((node)sender).BackColor = Color.Green;
                    ((node)sender).BringToFront();
                    flooding_endNode = (node)sender;
                }
                if (flooding_startNode != null && flooding_endNode != null)
                {
                    if (checkBox_flooding_onceJump.Checked)
                        timer_flooding_onceJump.Start();
                    else
                        func_flooding_getPath();
                }

                #endregion
                return;
            }
            timer_changeNodePosition.Start();
        }
 public double func_getDistence(node _nodea, node _nodeb)
 {
     double distence = (Math.Pow((_nodea.Location.X - _nodeb.Location.X), 2)) + (Math.Pow((_nodea.Location.Y - _nodeb.Location.Y), 2));
     distence = Math.Sqrt(distence);
     return distence;
 }
 //消除节点数据痕迹
 public void func_mle_clearnodeimformation(node _node)
 {
     _node.list_neighbour.Clear();
     _node.list_anchor.Clear();
     _node.bufferNode = null;
     _node.jumptimes = -1;
 }
 //搜索_start节点到所有可到达锚点的最小跳数
 public void func_mle_searchpath(node _start)
 {
     func_mle_setlink();
     List<node> nowmovingpath = new List<node>();
     foreach (node temp_node in mle_anchorList)
     {
         temp_node.jumptimes = -1;
     }
     foreach (node temp_node in mle_nodeList)
     {
         temp_node.jumptimes = -1;
     }
     _start.jumptimes = -1;
     nowmovingpath.Add(_start);
     int jumptimes = 0;
     while (nowmovingpath.Count != 0)
     {
         jumptimes++;
         List<node> temp_searchlist = new List<node>();
         foreach (node temp_node in nowmovingpath)
         {
             foreach (node temp_node_j in temp_node.list_neighbour)
             {
                 if (temp_node_j.jumptimes == -1)
                 {
                     temp_node_j.bufferNode = temp_node;
                     temp_node_j.jumptimes = jumptimes;
                     temp_searchlist.Add(temp_node_j);
                 }
             }
         }
         nowmovingpath = temp_searchlist;
     }
     foreach (node temp_node in mle_anchorList)
     {
         if (temp_node.jumptimes != -1)
         {
             jumptoanchor temp_anchor = new jumptoanchor(temp_node.X, temp_node.Y, temp_node.jumptimes, temp_node);
             _start.list_anchor.Add(temp_anchor);
         }
     }
 }