예제 #1
0
        private void ScatterPoints(int n, PictureBox pb)
        {
            Rectangle rt  = new Rectangle(0, 0, pb.Width, pb.Height);
            Bitmap    bmp = new Bitmap(rt.Width, rt.Height);

            for (int i = 0; i < rt.Width; i++)
            {
                for (int j = 0; j < rt.Height; j++)
                {
                    bmp.SetPixel(i, j, Color.White);
                }
            }

            pointList.Clear();
            Random r = new Random(DateTime.Now.Millisecond);

            for (int k = 0; k < n; k++)
            {
                int x, y, minx, miny;
                minx = (int)(rt.Width * .2);
                miny = (int)(rt.Height * .2);
                x    = r.Next(minx, rt.Width - minx);
                y    = r.Next(miny, rt.Height - miny);
                KD2DPoint pp = new KD2DPoint(x, y);
                pointList.Add(pp);
                bmp.SetPixel(x, y, Color.Black);
            }

            //   pb.DrawToBitmap(bmp, rt);

            pb.Image = (Image)bmp;
        }
예제 #2
0
        private void button1_Click(object sender, EventArgs e)
        {
            //Point[] nPoints = getPoints((Bitmap)pictureBox1.Image);
            List <IPosition> al = new List <IPosition>();

            al = pointList;
            //for (int i = 0; i < nPoints.Length; i++)
            //{
            //   al.Add(new KD2DPoint(nPoints[i]));
            //   // all.Add(new KD2DPoint(nPoints[i]));
            //}
            // KDTree Ktree = KDTree.CreateKDTree(all);
            INearestNeighbor M2Mnn = new M2M_NN();
            INearestNeighbor KDTnn = new KDT_NN();
            //M2Mnn.Init(400, 400, 100, 5, 5);
            int runTimes = 10000;

            M2Mnn.PreProcess(al);

            KDTnn.PreProcess(al);
            Graphics g = Graphics.FromImage(pictureBox1.Image);

            int       px = int.Parse(textBox1.Text);
            int       py = int.Parse(textBox2.Text);
            KD2DPoint AskPoint = new KD2DPoint(px, py);
            long      timeMM, timeKD;
            Stopwatch sW       = new Stopwatch();
            KD2DPoint nearest  = null;
            KD2DPoint nearest2 = null;

            sW.Start();
            for (int i = 0; i < runTimes; i++)
            {
                nearest = (KD2DPoint)M2Mnn.NearestNeighbor(AskPoint);
            }
            sW.Stop();
            timeMM = sW.ElapsedTicks / runTimes;
            sW.Reset();
            sW.Start();
            for (int i = 0; i < runTimes; i++)
            {
                nearest2 = (KD2DPoint)KDTnn.NearestNeighbor(AskPoint);
            }
            if (!nearest.Equals(nearest2))
            {
                MessageBox.Show("结果不符!");
            }
            sW.Stop();
            timeKD = sW.ElapsedTicks / runTimes;
            sW.Reset();
            M2Mlabel.Text = timeMM.ToString();
            KDTlabel.Text = timeKD.ToString();

            //   label4.Text = "最近距离:" + dis.ToString();
            g.DrawLine(new Pen(Color.SteelBlue), new PointF(AskPoint.X, AskPoint.Y), new PointF(nearest.X, nearest.Y));


            pictureBox1.Refresh();
        }
예제 #3
0
        //生成随机点集
        List <IPosition> createPointSet(double pointCount)
        {
            RandomPositionSet_InFixedDistribution positionSet = new RandomPositionSet_InFixedDistribution((int)pointCount, dStyle);

            List <IPosition> posList = new List <IPosition>();

            positionSet.InitToTraverseSet();
            while (positionSet.NextPosition())
            {
                KD2DPoint point = new KD2DPoint(positionSet.GetPosition());
                posList.Add(point);
            }

            return(posList);
        }
예제 #4
0
        List <IPosition> createPointSetWithPrepare(double pointCount)
        {
            RandomPositionSet_InFixedDistribution positionSet = new RandomPositionSet_InFixedDistribution((int)pointCount, dStyle);

            List <IPosition> posList = new List <IPosition>();

            minX = float.MaxValue;
            minY = float.MaxValue;
            maxX = float.MinValue;
            maxY = float.MinValue;

            positionSet.InitToTraverseSet();
            while (positionSet.NextPosition())
            {
                KD2DPoint point = new KD2DPoint(positionSet.GetPosition());
                posList.Add(point);

                if (minX > point.GetX())
                {
                    minX = point.GetX();
                }
                else if (maxX < point.GetX())
                {
                    maxX = point.GetX();
                }

                if (minY > point.GetY())
                {
                    minY = point.GetY();
                }
                else if (maxY < point.GetY())
                {
                    maxY = point.GetY();
                }
            }

            runM2MNNPP(0, posList);
            runKDTNNPP(0, posList);
            return(posList);
        }
예제 #5
0
        static public bool NearestNeighbourTest()
        {
            int              range  = 4000;
            int              Pnum   = 100000;
            int              Tnum   = 1000;
            Random           r      = new Random();
            Stopwatch        sWatch = new Stopwatch();
            List <IPosition> pl     = new List <IPosition>();

            for (int i = 0; i < Pnum; i++)
            {
                // KD2DPoint pp = new KD2DPoint((float)r.NextDouble() * (range - 1), (float)r.NextDouble() * (range - 1));

                float     x  = (float)r.NextDouble() * r.Next((range - 1));
                KD2DPoint pp = new KD2DPoint(x, ((range - 1) - x));//(float)(200+Math.Sin(x)* 200));


                pl.Add(pp);
            }


            INearestNeighbor SearchNearestNeighborEngine = new M2M_NN();

            //SearchNearestNeighborEngine.Init(range, range, 8000, 5, 4);

            SearchNearestNeighborEngine.PreProcess(pl);

            INearestNeighbor KDTSearchNN = new KDT_NN();

            KDTSearchNN.PreProcess(pl);
            KD2DPoint expected, actual;
            long      timeKD, timeMM;

            timeKD = 0;
            timeMM = 0;

            List <IPosition> testPointList = new List <IPosition>();

            for (int i = 0; i < Tnum; i++)
            {
                KD2DPoint NearestNeighbour_target2 = new KD2DPoint((float)r.NextDouble() * (range - 1), (float)r.NextDouble() * (range - 1)); // TODO: 初始化为适当的值

                testPointList.Add(NearestNeighbour_target2);
            }

            //Console.WriteLine("x: " + NearestNeighbour_target2.GetX().ToString() + "   y: " + NearestNeighbour_target2.GetY().ToString());

            sWatch.Reset();
            sWatch.Start();
            KD2DPoint p1, p2;
            int       err = 0;

            foreach (IPosition testPoint in testPointList)
            {
                p1 = (KD2DPoint)
                     SearchNearestNeighborEngine.NearestNeighbor(testPoint);
                //   double b= Math.Sqrt(Math.Pow(p1.X - testPoint.GetX(), 2) + Math.Pow(p1.X - testPoint.GetX(), 2));
                //}
                //sWatch.Stop();

                //Console.WriteLine("M2M:" + sWatch.ElapsedMilliseconds);

                //sWatch.Reset();
                //sWatch.Start();
                //foreach (Position testPoint in testPointList)
                //{
                p2 = (KD2DPoint)
                     KDTSearchNN.NearestNeighbor(testPoint);
                if (!p2.Equals(p1))
                {
                    Console.WriteLine("不符!");

                    float dist1 = (float)Math.Sqrt((double)((testPoint.GetX() - p1.GetX()) * (testPoint.GetX() - p1.GetX()) + (testPoint.GetY() - p1.GetY()) * (testPoint.GetY() - p1.GetY())));

                    float dist2 = (float)Math.Sqrt((double)((testPoint.GetX() - p2.GetX()) * (testPoint.GetX() - p2.GetX()) + (testPoint.GetY() - p2.GetY()) * (testPoint.GetY() - p2.GetY())));

                    Console.WriteLine("m2mnn: " + dist1.ToString());
                    Console.WriteLine("kdnn: " + dist2.ToString());

                    for (int i = 0; i < 100; i++)
                    {
                        SearchNearestNeighborEngine.NearestNeighbor(testPoint);
                        KDTSearchNN.NearestNeighbor(testPoint);
                    }

                    err++;
                }
            }
            sWatch.Stop();
            Console.WriteLine("测试点数:" + Tnum.ToString() + ";不准确数:" + err.ToString());
            Console.WriteLine("KD:" + sWatch.ElapsedMilliseconds);



            //timeMM+=sWatch.ElapsedMilliseconds;
            //sWatch.Reset();
            //sWatch.Start();
            //expected = (KD2DPoint)KDTSearchNN.NearestNeighbor((Position)NearestNeighbour_target2);
            //sWatch.Stop();
            //timeKD += sWatch.ElapsedMilliseconds;
            //sWatch.Reset();
            //if (!expected.Equals(actual))
            //    Console.WriteLine("不符合!");


            //Console.WriteLine("测试完毕!");
            //Console.WriteLine("kd:" + timeKD.ToString());
            //Console.WriteLine("M2M:"+timeMM.ToString());


            return(true);
        }