c#计算数组之间的重叠
我不是在寻找一个直截了当的答案,而是某种类型的指导。如果你要发布直接的代码,请你解释一下,因为我想尽可能多地学习 我试图计算并输出两个数组之间的重叠。这是到目前为止我的代码c#计算数组之间的重叠,c#,arrays,visual-studio,overlap,C#,Arrays,Visual Studio,Overlap,我不是在寻找一个直截了当的答案,而是某种类型的指导。如果你要发布直接的代码,请你解释一下,因为我想尽可能多地学习 我试图计算并输出两个数组之间的重叠。这是到目前为止我的代码 class Tester { static void Main(string[] args) { Box[] boxArray1 = { new Box(4, 3, 2, "white"),
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
类测试器
{
静态void Main(字符串[]参数)
{
方框[]方框1={
新盒子(4,3,2,“白色”),
新盒子(9,5,6,“红色”),
新盒子(3,6,12,“紫色”),
新盒子(15,10,4,“橙色”),
新盒子(4,14,10,“黑色”),
};
方框[]方框2={
新盒子(3,4,2,“粉色”),
新盒子(10,2,4,“红色”),
新盒子(8,5,7,“白色”),
新盒子(14,4,10,“蓝色”),
新盒子(10,15,4,“活页夹”),
};
}//静总管末端
静态void calculate1(框[]框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 duplicatedColors=new List();
for(int i=0;i
好的,现在来看看卷:
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
}
}
List duplicatedVolumes=new List();
for(int i=0;ivolumeB)
{
//检查体积是否相等,注意Epsilon的用法,这是因为
//双精度值并不精确,我们必须考虑到这一点。double.Epsilon
//是一个double可以存储的最小(不是最低)值,因此它非常有用
//如果有错误,可以增加此值。
重复体积。添加(体积a);
打破
}
//不,继续循环
}
}
这是否编译string[/name=box1[i].Split(“”);
?对此表示怀疑。i
在当时还没有声明,而且Box
没有Split
方法(至少在提供的代码中)?通过“重叠”你的意思是框在它们之间相交?如果是这样的话,那么使用矩形。与@OusmaneMahyDiaw相交是的,它不编译。我只是在和之前的代码混在一起,看看它是否有效。是的,这就是我的全部代码。在这一点上,我不知道它可能是什么,所以我只是随机尝试stuff@Gusman我不是在画画这是什么?-这只是我放入数组中的随机值。@John.555 Rectangle struct不画任何东西,它只是一个以任意坐标表示矩形的结构,如果你将框的限制存储在矩形中而不是在普通变量中,那么你可以使用它所具有的IntersectsWith函数。这与在静态主右图中?--这里显示了大量错误:抱歉,更新了答案,不确定我为什么在for声明之外写int说明符…太棒了!谢谢你为color:)工作了,我只需要把它全部写出来,然后它就工作了。对于框的体积,这会是类似的事情吗?我有3个重叠注意“4,3,2”,“15,10,4”和“14,4,10”的体积,因为它们在计算体积时都是相同的,但我不完全理解你。因为框存在于坐标0,0,0上(你没有定义任何位置)尺寸大于0的任何框都将与尺寸非零的任何其他框重叠。如果要检查是否存在长度、宽度和高度完全相同的框,请将这些属性添加到If中,类似于:If(boxaray1[i]。Color==boxaray2[j]。Color&&boxaray1[i]。length==boxaray2[j]。length&&boxarary1[i].Width==boxArray2[j]。Width&&boxArray1[i]。Height==boxArray2[j]。Height)
。如果这两种情况中没有一个能更好地解释我输出0的真正目的。我认为这会更好地解释它。我上面提到的3个卷,它们在每个数组中。这些卷的结果是相同的。例如,4*3*2与boxArray2中的3*4*2相同。因为这些方程的结果是相同的,所以t加上重复体积的总计数。