蛮力法03

本文介绍了一种计算平面上多个随机分布点之间的最近点对距离的方法,并通过C#实现了该算法。此外,还提供了生成包含这些点的图像的功能,并能够可视化显示距离最近的点对。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

最近距离问题:平面中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

转载于:https://www.cnblogs.com/sharpfeng/archive/2012/08/29/2661573.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值