Learn AspNet
Learn AspNet

Reputation: 1612

Calculate the distance for each x and y coordinate

I am writing a function that calculates distance from x and y coordinate. I have a two dimensional array that has bunch of x and y coordinates. Function returns a list of x and y coordinates in the order of distance from a point. 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. 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;
        }
    }

Upvotes: 0

Views: 1431

Answers (2)

DotNet Developer
DotNet Developer

Reputation: 3018

Based on your description you don't need to create a calculateDistance method. Formula to calculate distance can be given with lambda expression. Anywhere in your code you can create the list you need and get it sorted with 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. Instead use your own expression to calculate the distance.

To sort this you can simply use

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

Upvotes: 1

Fede
Fede

Reputation: 4016

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

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.

After you have the sequence of results, the easiest way to sort them is using the OrderBy extension method.

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();

Upvotes: 0

Related Questions