【问题标题】:Distance between array of array数组数组之间的距离
【发布时间】:2017-07-25 15:03:27
【问题描述】:

我有以下场景:

public class World{
  public City[,] cities { get; set; }
}


public class City
{
    public int number_city { get; set; }        
    public int x { get; set; }
    public int y { get; set; }
    public World world { get; set; }
    public Slot[,] slots { get; set; }
}

public class Slot
{
    public City city { get; set; }

    public int x { get; set; }
    public int y { get; set; }
}

也就是说,它作为一个国际象棋场,我有几个城市的对象世界,每个城市都有许多插槽。每个城市的槽位排列成一个网格,每个城市的网格具有相同的行数和列数。

通过这种方法我可以得到同一个城市的插槽之间的距离:

public static double GetDistance(int x0, int y0, int x1, int y1)
{
    int dX = y1 - y0;
    int dY = x1 - x0;
    return Math.Sqrt(dX * dX + dY * dY);
}

只有我需要得到不同城市插槽之间的距离

在每个城市中,插槽的位置从零开始,因此我不能直接使用它们来计算城市之间的位置。

世界之间不需要距离。

世界(一个世界有几个城市)

城市和插槽

例如,我需要黄色 PC 之间的距离,结果是 7

【问题讨论】:

  • 标签complexity-theory 在这里没有帮助,这是不同的。
  • 暗示CitySlot 中的坐标单位相同。因为它们不是(因为否则你有重叠的城市),你不能对它们做任何事情,除非你把它们带到同一个单位,例如。通过声明一个城市距离单位为 10 个时隙单位并将city1.x * 10 + slot1.x 引入公式。这是五年级的几何而不是编程。
  • 所有城市都有相同大小的网格吗? IE。他们都是7x7?当@GSerg 说坐标不同时,如果他们没有,你会有重叠的城市是什么意思?假设所有城市都有相同大小的网格,那么您只需将用于插槽的“城市坐标”值转换为“世界坐标”,只需将城市的 x/y 值乘以 7 到插槽的“城市坐标”值。如果该假设不正确,您需要提供更多详细信息。
  • " 第一个城市的最后一个元素和第二个城市的第一个元素之间的距离只有1" -- 为什么会有问题?在您的示例中,城市#1 可能具有坐标(0,0),而城市#2 具有坐标(1,0)。在该示例中,3x3 城市意味着插槽 A 的世界坐标为 (0*3+2,0*3+0)=(2,0),插槽 B 的 W 为 (1*3+0,0*3+ 0)=(3,0)。 (2,0) 和 (3,0) 之间的距离为 1,就像您想要的那样。有什么问题?
  • 同理(同例),C 的 W 为 (0*3+1,0*3+2)=(1,2),D 的 W 为 (1*3+1,0 *3+2)=(4,2),并且 (1,2) 和 (4,3) 之间的距离为 3,同样如您所愿。

标签: c# arrays algorithm matrix multidimensional-array


【解决方案1】:

根据您在 cmets 中发布的说明,我已编辑您的问题以更好地反映情况。您有占据WorldCity 对象。每个CityWorld 中都有一个X/Y 坐标,并包含在City 中具有X/Y 坐标的Slot 对象。每个City 的大小相同,即具有相同的行数和列数,并且在世界范围内,每个City 都与其邻居完全相邻。 IE。没有间隙、没有重叠、没有奇数大小的城市等。

鉴于此,一旦您将其视为简单的参考框架问题,您的问题就很容易解决。也就是说,虽然Slot 坐标相对于包含SlotCity,但它们仍然存在于World 中。借用物理学或计算机图形学中的一个概念,两者都有“参考系”,其中一个参考系中的坐标可以通过简单的变换映射到另一个参考系,我们可以将此概念应用于您的问题。

特别是,您可以将Slot 的X/Y 坐标从City 参考系转换为World 参考系,只需将City X/Y 坐标乘以宽度和单个城市的高度,然后添加Slot X/Y 坐标:

struct SlotPoint
{
    public readonly int X;
    public readonly int Y;

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

const int _kcityWidth = 7;
const int _kcityHeight = 7;

SlotPoint ConvertCityToWorld(Slot slot)
{
    return new SlotPoint(
        slot.city.x * _kcityWidth + slot.x, slot.city.y * _kcityHeight + slot.y);
}

有了这个转换,计算任意两个槽之间的距离就很简单了:

double GetDistance(Slot slot1, Slot slot2)
{
    SlotPoint point1 = ConvertCityToWorld(slot1), point2 = ConvertCityToWorld(slot2);

    return GetDistance(point1.X, point1.Y, point2.X, point2.Y);
}

【讨论】:

    【解决方案2】:

    如果我正确理解了您的问题,那应该可以解决问题:

     public static double GetDistance(int x0, int y0, int x1, int y1)
            {
                // mirror image dictionary to manipulate the parallel columns
                Dictionary<int, int> mirrorMatrixCols = new Dictionary<int, int>();
                mirrorMatrixCols.Add(0, 7);
                mirrorMatrixCols.Add(1, 8);
                mirrorMatrixCols.Add(2, 9);
                mirrorMatrixCols.Add(3, 10);
                mirrorMatrixCols.Add(4, 11);
                mirrorMatrixCols.Add(5, 12);
                mirrorMatrixCols.Add(6, 13);
    
                var result = 0;
                // the distance of cells to the point of y1 column
                result = (x1 - x0) * 7;
                // add the distance to the manipulated column
                result = result + (mirrorMatrixCols[y1] - y0);
                return result;    
            }
    

    【讨论】:

      猜你喜欢
      • 2017-04-21
      • 1970-01-01
      • 2011-07-28
      • 1970-01-01
      • 2015-02-25
      • 2016-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多