简体   繁体   English

计算每个x和y坐标的距离

[英]Calculate the distance for each x and y coordinate

I am writing a function that calculates distance from x and y coordinate. 我正在写一个函数来计算x和y坐标的距离。 I have a two dimensional array that has bunch of x and y coordinates. 我有一个具有一堆x和y坐标的二维数组。 Function returns a list of x and y coordinates in the order of distance from a point. 函数返回到点的距离顺序的x和y坐标列表。 To calculate the distance from each point there is a formula(Square root of sum of coordinates). 为了计算到每个点的距离,有一个公式(坐标和的平方根)。

I can calculate the distance for each x and y coordinate. 我可以计算每个x和y坐标的距离。 I am adding that to list. 我将其添加到列表中。 How do I store distance as another property as it is for that particular coordinate and then sort it. 如何将距离存储为该特定坐标的另一个属性,然后对其进行排序。

public static List<List<int>> calculateDistance(int[,] Coordinates)
        {
            List<List<int>> result = new List<List<int>>();
            int bound0 = Coordinates.GetUpperBound(0);
            List<double> distance = new List<double>();

            for (int i = 0;i <= bound0; i++)
            {
                distance.Add(Math.Sqrt(Coordinates[i, 0]) + Coordinates[i,1]));
            }  

            return result;
        }
    }

Based on your description you don't need to create a calculateDistance method. 根据您的描述,您无需创建calculateDistance方法。 Formula to calculate distance can be given with lambda expression. 可以使用lambda表达式给出计算距离的公式。 Anywhere in your code you can create the list you need and get it sorted with Linq. 您可以在代码中的任何位置创建所需的列表,并使用Linq对其进行排序。

Example

var list = Enumerable
                .Range(0, Coordinates.GetLength(0))
                .Select(i => new { X = Coordinates[i, 0], Y = Coordinates[i, 1], D = Math.Sqrt(Math.Pow(Coordinates[i, 0], 2) + Math.Pow(Coordinates[i, 1], 2)) });

Math.Sqrt(Math.Pow(Coordinates[i, 0], 2) + Math.Pow(Coordinates[i, 1], 2)) is used here for demonstration. Math.Sqrt(Math.Pow(Coordinates[i, 0], 2) + Math.Pow(Coordinates[i, 1], 2))进行演示。 Instead use your own expression to calculate the distance. 而是使用您自己的表达式来计算距离。

To sort this you can simply use 要对此进行排序,您可以简单地使用

var list2 = list.OrderBy(a => a.D);

It would be more helpful if you try to define proper data structures for your inputs and your output. 如果您尝试为输入和输出定义适当的数据结构,则将更有帮助。 It could be something as simple as a tuple, or something more idiomatic like a struct or a class. 它可能像元组一样简单,也可能是结构或类之类的更惯用的东西。

struct Coordinate {
    public Coordinate(int x, int y) {
        X = x;
        Y = y;
    }

    public int X { get; }
    public int Y { get; }
}

Then define a result structure, something like: 然后定义一个结果结构,如下所示:

struct Result {
    public Result(Coordinate coordinate, double distance) {
         Coordinate = coordinate;
         Distance = distance;
    }

    public Coordinate Coordinate { get; }
    public double Distance { get; }
}

Then you can create a list of those result items like: 然后,您可以创建这些结果项的列表,例如:

public List<Result> ComputeDistances(List<Coordinate> coordinates) {
    List<Result> results = new List<Result>();
    foreach (var coordinate in coordinates) {
        double distance = Math.Sqrt(coordinate.X + coordinate.Y); // *
        results.Add(new Result(coordinate, distance));
    }
    return results;
}

(*) Note that the specified distance function is a little bit odd. (*)请注意,指定的距离函数有些奇怪。 Normally you would like to sum the squares of the X and Y coordinate 通常,您想对X和Y坐标的平方求和

If you like a more functional style, you can change that code quite a bit: 如果您喜欢一种更具功能性的样式,则可以对代码进行大量更改:

public IEnumerable<Result> ComputeDistances(IEnumerable<Coordinate> coordinates) {
    return from coordinate in coordinates
        let distance = Math.Sqrt(coordinate.X + coordinate.Y)
        select new Result(coordinate, distance);
}

Changing from List to IEnumerable allows you with the proper care, to delay the execution of the computation. 从列表更改为IEnumerable可以让您谨慎处理,以延迟计算的执行。

After you have the sequence of results, the easiest way to sort them is using the OrderBy extension method. 获得结果序列后,对它们进行排序的最简单方法是使用OrderBy扩展方法。

public IEnumerable<Result> SortByDistance(IEnumerable<Result> results)
{
    return results.OrderBy(result => result.Distance);
}

And then combine all: 然后结合所有:

List<Coordinate> coordinates = .... // get the list of coordinates somehow;
IEnumerable<Result> distances = ComputeDistances(coordinates);
IEnumerable<Result> sortedByDistance = SortByDistance(distances);
// if you want to get back a list, in order to avoid enumerating multiple times
List<Result> results = sortedByDistance.ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM