【问题标题】:c# calculate overlaps between arraysc# 计算数组之间的重叠
【发布时间】:2017-05-05 00:10:04
【问题描述】:

我不是在寻找一个直接的答案,而是某种类型的指导。如果您要发布直接代码,请您解释一下,因为我想尽可能多地学习。

我正在尝试计算和输出两个数组之间的重叠。到目前为止,这是我的代码。

 class Tester
{
    static void Main(string[] args)
    {

        Box[] boxArray1 = {
                                new Box(4, 3, 2, "white"),
                                new Box(9, 5, 6, "red"),
                                new Box(3, 6, 12, "purple"),
                                new Box(15, 10, 4, "orange"),
                                new Box(4, 14, 10, "black"),
                          };

        Box[] boxArray2 = {
                                new Box(3, 4, 2, "pink"),
                                new Box(10, 2, 4, "red"),
                                new Box(8, 5, 7, "white"),
                                new Box(14, 4, 10, "blue"),
                                new Box(10, 15, 4, "bindle"),
                          };


    }//end of static main
    static void calculate1(Box[] box1) // <== Here's my attempt at calculating the overlaps.
    {
        string[] name = box1[i].Split(' ');
        int gr1 = box1.Length;
        int[] gg1 = new int[gr1];
        for (int i = 0; i < box1.Length; i++)
        {
            gg1[i] = int.Parse(box1[i]);
        }
    }

}//end of class Tester
class Container
{
    public string Colour { get; set; }
    public Container(string co)
    {
        Colour = co;
    }
    public virtual string GetContainerType()
    {
        return "Unknown";
    }
}//end of class Container

class Box : Container
{
    public double Length { get; set; }
    public double Height { get; set; }
    public double Width { get; set; }

    public Box(double Le, double He, double Wi, string co)
        : base(co)
    {
        Length = Le;
        Height = He;
        Width = Wi;
    }
    public override string GetContainerType()
    {
        return "Box";
    }
    public double GetVolume()
    {
        return Length * Height * Width;
    }


}//end of class Box

如上图所示,除了颜色外,还有盒子的尺寸。我想找到这两个数组之间的重叠,例如“白色”和“红色”,都显示在两个数组中。然后我想计算然后输出“两个数组之间有 2 个颜色重叠的 Box 对象。”,然后对维度执行相同操作。

谢谢。

【问题讨论】:

  • 这是否编译string[] name = box1[i].Split(' ');?怀疑确实如此。 i 到那时还没有声明,Box 没有 Split 方法(至少在提供的代码中)?
  • “重叠”是指盒子在它们之间相交吗?如果是这种情况,请使用Rectangle.IntersectsWith
  • @OusmaneMahyDiaw 是的,它无法编译。我只是在弄乱以前的代码,看看它是否有效。是的,这就是我的全部代码。在这一点上,我不知道它可能是什么,所以我只是在尝试随机的东西
  • @Gusman 我什么都没画? - 这只是我放入数组中的随机值。
  • @John.555 Rectangle 结构不绘制任何东西,只是一个表示任意坐标中矩形的结构,如果您将框的限制存储在 Rectangle 内而不是普通变量中,那么您可以使用它具有的 IntersectsWith 函数。

标签: c# arrays visual-studio overlap


【解决方案1】:

回答您问题中的最后一条评论,检查它创建一个双循环并检查属性:

List<string> duplicatedColors = new List<string>();

for (int i = 0; i < boxArray1.Length; i++) //Loop through the first array
{
    for (int j= 0; j < boxArray2.Length; j++) //Loop through the second array
    {
        if(boxArray1[i].Color == boxArray2[j].Color) //Do element at array 1 have the same color that the item at array 2?
        {
            //Yes, store color and break inner loop
            duplicatedColors.Add(boxArray1[i].Color);
            break;
        }

        //No, continue loop
    }
} 

//Here now you have all the duplicated colors in duplicatedColors
//You can use duplicatedColors.Count to get the number of items dupicated.

好的,现在是卷:

List<double> duplicatedVolumes = new List<double>();

for (int i = 0; i < boxArray1.Length; i++) //Loop through the first array
{
    for (int j= 0; j < boxArray2.Length; j++) //Loop through the second array
    {

        var volumeA = boxArray1[i].Length * boxArray1[i].Width * boxArray1[i].Height; //Compute box A volume
        var volumeB = boxArray2[j].Length * boxArray2[j].Width * boxArray2[j].Height; //Compute box B volume

        if(volumeA - double.Epsilon < volumeB && volumeA + double.Epsilon > volumeB)
        {
          //Check if the volumes are equal, note the use of the Epsilon, that's because 
          //double values aren't exact and we must have that into account. double.Epsilon 
          //is the smallest (not lowest) value a double can store so it's very 
          //thightened, you can increase this value if you have errors.

            duplicatedVolumes.Add(volumeA);
            break;
        }

        //No, continue loop
    }
} 

【讨论】:

  • 这确实属于静态主要内容,对吗? -- 这里显示了大量错误:i.imgur.com/1Uao6iW.jpg
  • 对不起,更新了答案,不知道为什么我在 for 声明之外写了 int 说明符...
  • 太棒了!谢谢你为颜色工作:),我只需要把它全部写出来然后它就起作用了。盒子的体积会是类似的吗?我注意到音量有 3 个重叠,分别是“4、3、2”、“15、10、4”和“14、4、10”,因为在计算音量时它们都相等
  • 对不起,我不完全理解你。由于框存在于坐标 0,0,0(您尚未定义任何位置)上,任何尺寸大于 0 的框都将与任何其他尺寸非零的框重叠。如果您想检查是否存在具有完全相同长度、宽度和高度的框,则将这些属性添加到 if 中,如下所示:if(boxArray1[i].Color == boxArray2[j].Color &amp;&amp; boxArray1[i].Length == boxArray2[j].Length &amp;&amp; boxArray1[i].Width== boxArray2[j].Width &amp;&amp; boxArray1[i].Height == boxArray2[j].Height)。如果不是这两种情况之一更好地解释真正的目的
  • 这似乎给了我 0 的输出。我认为这将更好地解释它。我上面提到的那 3 卷,它们在每个数组中。这些卷的结果是相同的。例如,4*3*2 与 boxArray2 中的 3*4*2 相同。由于这些方程的结果是相同的,这增加了duplicatedVolume 的总数。
猜你喜欢
  • 1970-01-01
  • 2021-07-23
  • 2016-01-28
  • 1970-01-01
  • 1970-01-01
  • 2018-11-16
  • 1970-01-01
  • 1970-01-01
  • 2023-01-19
相关资源
最近更新 更多