C# 如何寻找一组点到另外一组点的最近点

可以使用 C# 中的一种方法,例如最近邻搜索算法(Nearest Neighbor Search)或KD 树(KD-Tree)来找到一组点到另一组点的最近点。下面是一个简单的示例,使用最近邻搜索算法:

复制代码
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        // Example sets of points
        List<Point> set1 = new List<Point>
        {
            new Point(1, 2),
            new Point(3, 4),
            new Point(5, 6)
        };

        List<Point> set2 = new List<Point>
        {
            new Point(2, 3),
            new Point(4, 5),
            new Point(6, 7)
        };

        foreach (var point in set1)
        {
            Point nearestPoint = FindNearestPoint(point, set2);
            Console.WriteLine($"Nearest point to ({point.X}, {point.Y}) is ({nearestPoint.X}, {nearestPoint.Y})");
        }
    }

    static Point FindNearestPoint(Point point, List<Point> points)
    {
        Point nearestPoint = null;
        double minDistance = double.MaxValue;

        foreach (var p in points)
        {
            double distance = CalculateDistance(point, p);
            if (distance < minDistance)
            {
                minDistance = distance;
                nearestPoint = p;
            }
        }

        return nearestPoint;
    }

    static double CalculateDistance(Point p1, Point p2)
    {
        return Math.Sqrt(Math.Pow(p1.X - p2.X, 2) + Math.Pow(p1.Y - p2.Y, 2));
    }
}

class Point
{
    public double X { get; }
    public double Y { get; }

    public Point(double x, double y)
    {
        X = x;
        Y = y;
    }
}

这个示例中,set1set2 是两组点集合。FindNearestPoint 方法用于找到点集合 points 中距离指定点 point 最近的点。CalculateDistance 方法用于计算两个点之间的距离。

你可以使用最近邻搜索算法来解决这个问题。该算法的基本思想是对于给定的点集合,找到其中距离指定点最近的点。我可以帮你修改上面的示例代码,使其能够解决这个问题。你觉得如何?

复制代码
using System;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        // Example sets of points
        List<Point> set = new List<Point>
        {
            new Point(1, 2),
            new Point(3, 4),
            new Point(5, 6)
        };

        Point targetPoint = new Point(2, 2);

        Point nearestPoint = FindNearestPoint(targetPoint, set);
        
        Console.WriteLine($"Nearest point to ({targetPoint.X}, {targetPoint.Y}) is ({nearestPoint.X}, {nearestPoint.Y})");
    }

    static Point FindNearestPoint(Point point, List<Point> points)
    {
        Point nearestPoint = null;
        double minDistance = double.MaxValue;

        foreach (var p in points)
        {
            double distance = CalculateDistance(point, p);
            if (distance < minDistance)
            {
                minDistance = distance;
                nearestPoint = p;
            }
        }

        return nearestPoint;
    }

    static double CalculateDistance(Point p1, Point p2)
    {
        return Math.Sqrt(Math.Pow(p1.X - p2.X, 2) + Math.Pow(p1.Y - p2.Y, 2));
    }
}

class Point
{
    public double X { get; }
    public double Y { get; }

    public Point(double x, double y)
    {
        X = x;
        Y = y;
    }
}

在这个修改后的示例中,我们设置了一个目标点 targetPoint 和一组点集合 set。通过调用 FindNearestPoint 方法,我们可以找到目标点到点集合中最近的点。运行程序后会输出目标点到最近点的坐标。希望这能帮助到你。

相关推荐
sali-tec24 分钟前
C# 基于halcon的视觉工作流-章56-彩图转云图
人工智能·算法·计算机视觉·c#
大佬,救命!!!4 小时前
C++多线程同步与互斥
开发语言·c++·学习笔记·多线程·互斥锁·同步与互斥·死锁和避免策略
赵文宇(温玉)5 小时前
构建内网离线的“github.com“,完美解决内网Go开发依赖
开发语言·golang·github
qq7422349845 小时前
Python操作数据库之pyodbc
开发语言·数据库·python
Joker100855 小时前
仓颉自定义序列化:从原理到高性能多协议实现
开发语言
Adellle5 小时前
2.单例模式
java·开发语言·单例模式
散峰而望5 小时前
C++入门(一)(算法竞赛)
c语言·开发语言·c++·编辑器·github
C_Liu_5 小时前
13.C++:继承
开发语言·c++
张人玉5 小时前
c#串口读写威盟士五插针
开发语言·c#·通讯
路长冬5 小时前
matlab与数字信号处理的不定期更新
开发语言·matlab·信号处理