Home > Software design >  Distributing k points among n entities to find distance between points?
Distributing k points among n entities to find distance between points?

Time:11-10

I have a known list of n entries. I also have a known quantity of k points.

I need to distribute those k points as equally distant to one another as I can among the n entries. The end result is that I know what distance (d) is between the points.

Example:

So if we had n={0, 1, 2, 3, 4, 5} entries (n=6) and k=3, and we assume we can start from 0 always, then we'd end up with a result of 0, 2 and 4 getting a point each as the "distance" between them all is 1 (d=1).

I've been racking my brain on how to calculate the d for an hour now. I'm not sure how to do it.

  • n will always be a list of integers.
  • k will always be less than n and an integer
  • d should always be an integer where k*d <= n.

I tried using the sqrt of n but that wouldn't work if I get fractions.

I know this sounds like a math assignment but it's not homework. It's just a problem I'm sitting with for a game.

How do I calculate d? I use C#.


Turns out what I actually needed was to discretely distribute k indices across n elements.

See an example of it in action here; https://youtu.be/SC0qwkNzvmo

You can see how bombs are distributed among players. That is what I needed but had difficulty explaining.

CodePudding user response:

It seems to me that you just need this:

int[] indices = new [] { 0, 2, 4, };

double distance =
    Enumerable
        .Range(0, indices.Length)
        .SelectMany(
            i => Enumerable.Range(i   1, indices.Length - i - 1),
            (i, j) => Math.Sqrt(indices[i] * indices[i]   indices[j] * indices[j]))
        .Average();

If you're trying to discretely distribute the k indices across the n elements, then this is it:

int n = 6;
int k = 3;

int[] bs =
    Enumerable
        .Range(0, k)
        .Select(i => i * n / k)
        .ToArray();

That gives 0, 2, 4.

  • Related