C# 三维空间坐标阵列

C# 三维空间坐标阵列,c#,java,c++,C#,Java,C++,要记住点的空间坐标,哪种方法更正确 这个, 或者这个, // x y z spacial[100][200][10] = 1; // 1 set that a point is present 我会使用第二个: // x y z spacial[100][200][10] = 1; // 1 set that a point is present 但是,还有更多表示法:角度、半径,而不仅仅是坐标,有关更多信息,请查看Wiki我将使用第二种表示法:

要记住点的空间坐标,哪种方法更正确

这个,

或者这个,

//       x    y    z
spacial[100][200][10] = 1; // 1 set that a point is present

我会使用第二个:

//     x    y    z
spacial[100][200][10] = 1; // 1 set that a point is present

但是,还有更多表示法:角度、半径,而不仅仅是坐标,有关更多信息,请查看Wiki

我将使用第二种表示法:

//     x    y    z
spacial[100][200][10] = 1; // 1 set that a point is present

但是,还有更多的表示法:使用角度、半径,而不仅仅是坐标,有关更多信息,请查看Wiki

,这取决于代码的使用场景。创建三维阵列在资源(内存)方面非常昂贵,因此您应该仅在创建体素结构时使用它,或者您知道需要填充空间中的所有点
x*y*z
。对于这种情况,代码

int spacial[][][] = new int[1024][768][100];
spacial[100][200][10] = 1; // 1 set that a point is present
使用起来更有意义。如果您想快速找到是否存在某些空间坐标,它也很有用

对于其他情况,可以创建结构

struct Coord
{
    int x, y, z
}
然后创建此结构的实例数组。这样可以提高内存效率,因为不必表示每个坐标(即使不存在)。您仍然可以使用算法来高效地搜索,使用八叉树进行搜索,但它们的实现更加复杂。
您可以在我对其他人的回答中找到有关八叉树的更多信息。

这取决于代码的使用场景。创建三维阵列在资源(内存)方面非常昂贵,因此您应该仅在创建体素结构时使用它,或者您知道需要填充空间中的所有点
x*y*z
。对于这种情况,代码

int spacial[][][] = new int[1024][768][100];
spacial[100][200][10] = 1; // 1 set that a point is present
使用起来更有意义。如果您想快速找到是否存在某些空间坐标,它也很有用

对于其他情况,可以创建结构

struct Coord
{
    int x, y, z
}
然后创建此结构的实例数组。这样可以提高内存效率,因为不必表示每个坐标(即使不存在)。您仍然可以使用算法来高效地搜索,使用八叉树进行搜索,但它们的实现更加复杂。
您可以在我对另一个八叉树的回答中找到有关八叉树的更多信息。

使用三维数组意味着您可以同时存储1024x768x100=78 643 200个整数值。大多数值使用内存,但包含零-我认为这对于良好的性能来说太糟糕了

我认为您应该使用列表只存储包含有价值坐标的点:

  public struct Point3D
  {
  public   int x {get;set;}
  public   int y {get;set;}
  public   int z {get;set;}
  public   int value {get;set;}
 //any other properties....
  }

List<Point3D>MyPoints=new List<Point3D>();

//to check if something exists by my coordinates:

List<Point3D> ResultList=MyPoints.FindAll(coords=>coords.x==25&&coords.y==250&&coords.z==70);
if(ResultList.Count>0) //points exists
{
  // do something with ResultList[0], that should contains your point data
}
公共结构点3D
{
公共整数x{get;set;}
公共整数y{get;set;}
公共int z{get;set;}
公共int值{get;set;}
//任何其他财产。。。。
}
ListMyPoints=新列表();
//要通过我的坐标检查是否存在某些内容,请执行以下操作:
List ResultList=MyPoints.FindAll(coords=>coords.x==25&&coords.y==250&&coords.z==70);
如果(ResultList.Count>0)//点存在
{
//对结果列表[0]执行某些操作,该列表应包含您的点数据
}

使用3D数组意味着您可以同时存储1024x768x100=78 643 200个整数值。大多数值使用内存,但包含零-我认为这对于良好的性能来说太糟糕了

我认为您应该使用列表只存储包含有价值坐标的点:

  public struct Point3D
  {
  public   int x {get;set;}
  public   int y {get;set;}
  public   int z {get;set;}
  public   int value {get;set;}
 //any other properties....
  }

List<Point3D>MyPoints=new List<Point3D>();

//to check if something exists by my coordinates:

List<Point3D> ResultList=MyPoints.FindAll(coords=>coords.x==25&&coords.y==250&&coords.z==70);
if(ResultList.Count>0) //points exists
{
  // do something with ResultList[0], that should contains your point data
}
公共结构点3D
{
公共整数x{get;set;}
公共整数y{get;set;}
公共int z{get;set;}
公共int值{get;set;}
//任何其他财产。。。。
}
ListMyPoints=新列表();
//要通过我的坐标检查是否存在某些内容,请执行以下操作:
List ResultList=MyPoints.FindAll(coords=>coords.x==25&&coords.y==250&&coords.z==70);
如果(ResultList.Count>0)//点存在
{
//对结果列表[0]执行某些操作,该列表应包含您的点数据
}

这次我写了一个Java——作为例外——完整的代码:) 我没有运行过,可能在索引方面会遗漏一些东西,但如果要存储的点超过20点,我会使用类似的方法。超过1000点,毫无疑问,使用这个或列表是什么

public class Spatial {

    public static final int maxX = 1024;
    public static final int maxY = 768;
    public static final int maxZ = 100;

    // 1024x768x100= 78 643 200
    // int max value:2,147,483,647

    private byte[] indexData;

    public Spatial() {
        int totalDataCount = maxX * maxY * maxZ;

        int byteAarraySizeNeeded = totalDataCount / 8 + totalDataCount % 8;

        indexData = new byte[byteAarraySizeNeeded]; // inited with all 0
    }

    public void markPresent(int x, int y, int z, boolean present) {
        // TODO: check parameters!!! minimum and max values!

        int index = (z * 1 + y * maxZ + maxX * (maxX * maxY));
        // transform the index to our storage index : maybe a bug here, cheack t pls!

        int arrayIndex = index / 8 + index % 8;

        byte dataChunck = indexData[arrayIndex];

        if (present) { // bitwise Or with 1
            dataChunck = (byte) (dataChunck | (1 << index % 8));
        } else { // bitwise And with 0
            byte helper = (byte) (1 << index % 8);
            byte all1ExceptOne = (byte) (~helper & 0xFF);
            dataChunck = (byte) (dataChunck & all1ExceptOne);
        }
        // put back:
        indexData[arrayIndex] = dataChunck;
    }

    public boolean isPresent(int x, int y, int z) {
        // TODO: check parameters!!! minimum and max values!

        int index = (z * 1 + y * maxZ + maxX * (maxX * maxY));
        // transform the index to our storage index : maybe a bug here, cheack t pls!

        int arrayIndex = index / 8 + index % 8;

        byte dataChunck = indexData[arrayIndex];

        return (dataChunck & (1 << index % 8)) > 0;

    }
}
公共类空间{
公共静态最终整数maxX=1024;
公共静态最终整数maxY=768;
公共静态最终int maxZ=100;
//1024x768x100=78 643 200
//整数最大值:2147483647
私有字节[]索引数据;
公共空间(){
int totalDataCount=maxX*maxY*maxZ;
int bytearraysizeneeded=totalDataCount/8+totalDataCount%8;
indexData=新字节[ByteArraySizeneed];//使用所有0初始化
}
public void markPresent(整数x、整数y、整数z、布尔值present){
//TODO:检查参数!!!最小值和最大值!
int index=(z*1+y*maxZ+maxX*(maxX*maxY));
//将索引转换为我们的存储索引:这里可能有一个bug,请确认!
int arrayIndex=索引/8+索引%8;
字节dataChunck=indexData[arrayIndex];
如果(存在){//按位或与1

dataChunck=(字节)(dataChunck |(1这次我写了一个Java——作为例外——一个完整的代码:) 我没有运行可能会在索引方面遗漏一些东西,但如果要存储的点超过20点,我会使用类似的方法。如果要存储的点超过1000点,则毫无疑问要使用这个或列表

public class Spatial {

    public static final int maxX = 1024;
    public static final int maxY = 768;
    public static final int maxZ = 100;

    // 1024x768x100= 78 643 200
    // int max value:2,147,483,647

    private byte[] indexData;

    public Spatial() {
        int totalDataCount = maxX * maxY * maxZ;

        int byteAarraySizeNeeded = totalDataCount / 8 + totalDataCount % 8;

        indexData = new byte[byteAarraySizeNeeded]; // inited with all 0
    }

    public void markPresent(int x, int y, int z, boolean present) {
        // TODO: check parameters!!! minimum and max values!

        int index = (z * 1 + y * maxZ + maxX * (maxX * maxY));
        // transform the index to our storage index : maybe a bug here, cheack t pls!

        int arrayIndex = index / 8 + index % 8;

        byte dataChunck = indexData[arrayIndex];

        if (present) { // bitwise Or with 1
            dataChunck = (byte) (dataChunck | (1 << index % 8));
        } else { // bitwise And with 0
            byte helper = (byte) (1 << index % 8);
            byte all1ExceptOne = (byte) (~helper & 0xFF);
            dataChunck = (byte) (dataChunck & all1ExceptOne);
        }
        // put back:
        indexData[arrayIndex] = dataChunck;
    }

    public boolean isPresent(int x, int y, int z) {
        // TODO: check parameters!!! minimum and max values!

        int index = (z * 1 + y * maxZ + maxX * (maxX * maxY));
        // transform the index to our storage index : maybe a bug here, cheack t pls!

        int arrayIndex = index / 8 + index % 8;

        byte dataChunck = indexData[arrayIndex];

        return (dataChunck & (1 << index % 8)) > 0;

    }
}
公共类空间{
公共静态最终整数maxX=1024;
公共静态最终整数maxY=768;
公共静态最终int maxZ=100;
//1024x768x100=78 643 200
//整数最大值:2147483647
私有字节[]索引数据;
公共空间(){
int totalDataCount=maxX*maxY*maxZ;
int bytearraysizeneeded=totalDataCount/8+totalDataCount%8;
indexData=新字节[ByteArraySizeneed];//使用所有0初始化
}
public void markPresent(整数x、整数y、整数z、布尔值present){
//TODO:检查参数!!!最小值和最大值!
int index=(z*1+y*maxZ+maxX*(maxX*maxY));
//将索引转换为我们的存储索引:这里可能有一个bug,请确认!
int arrayIndex=索引/8+索引%8;
字节dataChunck=indexDat