【发布时间】: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