zoukankan      html  css  js  c++  java
  • 蛮力法03

    ——可视化,是如此的充满魅力。

    最近距离问题:平面中n(n>1)个点,求距离最近的两个点。

    分析:设定集合 pts[n] , minLen=INT_MAX;

    for i=0, to n

    {

       for j=i+1, to n //计算两点间的距离不需要重复计算,所以全部计算过的顶点需要排除

       {

           if minLen > Dis(pts[i],pts[j])

           {

               //求出了当前最近的连个点距离

           }

       }

    }

    /// <summary>
    /// 最近点对距离计算
    /// </summary>
    class PointPairCompute
    {
        private int imgWidth;
        private int imgHeight;
        private int createCount;
        private List<Point> ptLst;
        private readonly int radius;
     
        public PointPairCompute(int width,int height,int count)
        {
            this.imgWidth = width;
            this.imgHeight = height;
            this.createCount = count;
            
            this.ptLst = new List<Point>();
            this.radius = 2;
        }
     
     
        /// <summary>
        /// 获取随机生成点的图片
        /// </summary>
        /// <returns>图片</returns>
        public Image GetRandomImag()
        {
            //随机产生点
            RandomCratePtLst();
            //绘制图片
            Bitmap map = new Bitmap(this.imgWidth, this.imgHeight);
            Graphics gfx = Graphics.FromImage(map);
            gfx.Clear(Color.Gray);
     
            foreach (Point item in this.ptLst)
            {
                //map.SetPixel(item.X, item.Y, Color.Red);
                gfx.FillEllipse(Brushes.Red, item.X - radius, item.Y - radius, radius * 2, radius * 2);
            }
     
            gfx.Dispose();            
     
            return map;
        }
     
        /// <summary>
        /// 获取点对最近距离结果图片
        /// </summary>
        /// <returns>结果图片</returns>
        public Image GetPointPariResult()
        {
            if (this.ptLst.Count <2)
            {
                return null;
            }
     
            int leftIndex = -1;
            int rightIndex = -1;
            ComputerPointPari(ref leftIndex, ref rightIndex);
     
            Bitmap map = new Bitmap(this.imgWidth, this.imgHeight);
            Graphics gfx = Graphics.FromImage(map);
            gfx.Clear(Color.White);
            Pen pen = new Pen(Color.Green, 4);
            gfx.DrawLine(pen, ptLst[leftIndex], ptLst[rightIndex]);
                        
            foreach (Point item in this.ptLst)
            {
                //map.SetPixel(item.X, item.Y, Color.Red);
                gfx.FillEllipse(Brushes.Red, item.X - radius, item.Y - radius, radius * 2, radius * 2);
            }
     
            gfx.Dispose();
            return map;
        }
     
        //随机生成点
        private void RandomCratePtLst()
        {
            this.ptLst.Clear();
     
            Random rd = new Random();
            for (int i = 0; i < this.createCount; i++)
            {
                this.ptLst.Add(new Point(rd.Next(0, this.imgWidth), rd.Next(0, this.imgHeight)));
            }
        }
     
        //蛮力计算最短距离
        private void ComputerPointPari(ref int leftIndex, ref int rightIndex)
        {
            int minLen = Int32.MaxValue;
     
            for (int i = 0; i < this.ptLst.Count; ++i)
            {
                for (int j = i+1; j < this.ptLst.Count; j++)
                {
                    int tempLen = (ptLst[i].X - ptLst[j].X) * (ptLst[i].X - ptLst[j].X) +
                        (ptLst[i].Y - ptLst[j].Y) * (ptLst[i].Y - ptLst[j].Y);
     
                    if (minLen > tempLen)
                    {
                        minLen = tempLen;
                        leftIndex = i;
                        rightIndex = j;
                    }
                }
            }
        }
    }

    image

    image

  • 相关阅读:
    js 解压缩编码列表
    js 拥有最多糖果
    js 所有奇数长度子数组的和
    js和jquery中有关透明度操作的问题
    python pandas初体验
    NumPy基础及取值操作
    python之pandas简介
    Study Plan The FortyThird day
    Study Plan The ThirtyNine Day
    Study Plan The FortySeventh Day
  • 原文地址:https://www.cnblogs.com/sharpfeng/p/2661573.html
Copyright © 2011-2022 走看看