蛮力法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

posted @ 2012-08-29 10:19  sharpfeng  阅读(243)  评论(0编辑  收藏  举报