我正在尝试创建多维数组:
Line[,] lines = new Line[10, 10];
但是执行该代码时出现此错误:
System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
Line
类是自定义类:
class Line
{
// Kameran kommer egentligen vara en mägnd vektorlinjer. Om man tex vill flytta kameran 1 steg i x-led lopar man igenom alla vektorer och ökar deras m1-värde med 1.
// En vektorlinje i parametisk form: ( a2 * t + m1 , b2 * t + m2 , c2 * t + m3 )
public float a2;
public float m1;
public float b2;
public float m2;
public float c2;
public float m3;
public Line(float a2_, float m1_, float b2_, float m2_, float c2_, float m3_)
{
a2 = a2_;
m1 = m1_;
b2 = b2_;
m2 = m2_;
c2 = c2_;
m3 = m3_;
}
}
当我将此复制到其他解决方案时,它的工作原理非常好。我被这条小线困住了。
它是WPF应用程序的一部分。如果有帮助,请参见以下完整代码:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace _3D_Cube_Render
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Rectangle rect = new Rectangle()
{
Width = 1,
Height = 1,
Fill = Brushes.Red,
};
Face[] faces = new Face[5];
faces[0] = new Face(1, 0, 0);
faces[1] = new Face(-1, 0, 0);
faces[2] = new Face(0, 1, 0);
faces[3] = new Face(0, -1, 0);
faces[4] = new Face(0, 0, 1);
faces[5] = new Face(0, 0, -1);
Line[,] lines = new Line[10, 10];
/*
for(int i = 0; i < 10; i++)
{
lines[i] = new Line[10];
for(int j = 0; j < 10; j++)
{
lines[i][j] = new Line(0, i, 1, -5, j, 0);
}
}
*/
/*
Line[][] lines = new Line[5][];
lines[0][0] = new Line(-1, 3.5f, -1, 6.5f, 0, 0);
lines[1][0] = new Line(-1, 4, -1, 6f, 0, 0);
lines[2][0] = new Line(-1, 4.5f, -1, 5.5f, 0, 0);
lines[3][0] = new Line(-1, 5f, -1, 5f, 0, 0);
lines[4][0] = new Line(-1, 5.5f, -1, 4.5f, 0, 0);
lines[0][1] = new Line(-1, 3.5f, -1, 6.5f, 0, 1);
lines[1][1] = new Line(-1, 4, -1, 6f, 0, 1);
lines[2][1] = new Line(-1, 4.5f, -1, 5.5f, 0, 1);
lines[3][1] = new Line(-1, 5f, -1, 5f, 0, 1);
lines[4][1] = new Line(-1, 5.5f, -1, 4.5f, 0, 1);
*/
// i är x-led, j är y-led ( där canvas är ett kordinatsystem med x- och y-axlar )
/*
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 10; j++)
{
for(int k = 0; k < 6; k++)
{
float t1 = (1 - lines[i][j].m1 * faces[k].a1 - lines[i][j].m2 * faces[k].b1 - lines[i][j].m3 * faces[k].c1) /
(faces[k].a1 * lines[i][j].a2 + faces[k].b1 * lines[i][j].b2 * faces[k].c1 * lines[i][j].c2);
if(lines[i][j].a2 * t1 + lines[i][j].m1 < -1)
{
Canvas.SetLeft(rect, i);
Canvas.SetTop(rect, j);
canvas.Children.Add(rect);
}
}
}
}*/
}
/*
private Face[] FaceCreator()
{
Face[] faces = new Face[5];
faces[0] = new Face(1, 0, 0);
faces[1] = new Face(-1, 0, 0);
faces[2] = new Face(0, 1, 0);
faces[3] = new Face(0, -1, 0);
faces[4] = new Face(0, 0, 1);
faces[5] = new Face(0, 0, -1);
return faces;
}*/
}
/*
class Camera
{
// Antal totala linjer motsvarar upplösningen man får. Därför: mer vektorlinjer/pixlar = sämre prestanda
public Line[][] lines;
// Går igenom alla vektorlinjer och ökar alla m-värden med korresporerande värde
public void Move(float x, float y, float z)
{
for(int i = 0; i < this.lines.Length; i++)
{
for (int j = 0; j < this.lines[i].Length; j++)
{
this.lines[i][j].m1 += x;
this.lines[i][j].m2 += y;
this.lines[i][j].m3 += z;
}
}
}
}
*/
class Face
{
// Ett plan i parametisk form: ( a * x + b * y + c * z = 1 )
public float a1;
public float b1;
public float c1;
public Face(float a1_, float b1_, float c1_)
{
a1 = a1_;
b1 = b1_;
c1 = c1_;
}
}
class Line
{
// Kameran kommer egentligen vara en mägnd vektorlinjer. Om man tex vill flytta kameran 1 steg i x-led lopar man igenom alla vektorer och ökar deras m1-värde med 1.
// En vektorlinje i parametisk form: ( a2 * t + m1 , b2 * t + m2 , c2 * t + m3 )
public float a2;
public float m1;
public float b2;
public float m2;
public float c2;
public float m3;
public Line(float a2_, float m1_, float b2_, float m2_, float c2_, float m3_)
{
a2 = a2_;
m1 = m1_;
b2 = b2_;
m2 = m2_;
c2 = c2_;
m3 = m3_;
}
}
}
和xaml:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:_3D_Cube_Render"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Canvas x:Name="canvas" Height="450" Width="800">
<Button x:Name="startButton" Content="Start" Width="30" Height="20" Click="Button_Click" Canvas.Left="23" Canvas.Top="14" />
<Rectangle Height="1" Width="1" Fill="Red" Canvas.Left="44" Canvas.Top="11" />
</Canvas>
</Grid>
</Window>
更改
Face[] faces = new Face[5];
收件人
Face[] faces = new Face[6];
更新:
正如乔恩·斯基特(Jon Skeet)在评论中提到的,原因是当您要访问此行中的face [5]:
faces[5] = new Face(0, 0, -1);
并且此面孔在面孔中不存在(定义上,面孔有5个面孔:0、1、2、3、4)此行中引发了异常