Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从中心到角读取二维阵列_Java_Arrays_Loops_2d_Center - Fatal编程技术网

Java 从中心到角读取二维阵列

Java 从中心到角读取二维阵列,java,arrays,loops,2d,center,Java,Arrays,Loops,2d,Center,因此,对于我正在进行的这个java项目,我需要一个循环,通过一个2d数组首先从中心读取4个邻接值,然后再读取该邻接值的各个角,并继续这样做,直到它到达并完成最外层。我需要它来处理所有奇数大小的二维正方形阵列。我制作此图像是为了阐明我的目标:。我个人没能做到这一点。谢谢你抽出时间!欢迎使用任何伪代码或java代码 可以从逻辑上重新定义栅格,使中心为0,0。 然后,根据距中心的距离对每个栅格坐标进行排序。 这假定栅格的宽度为奇数 例如: class Point { int x; in

因此,对于我正在进行的这个java项目,我需要一个循环,通过一个2d数组首先从中心读取4个邻接值,然后再读取该邻接值的各个角,并继续这样做,直到它到达并完成最外层。我需要它来处理所有奇数大小的二维正方形阵列。我制作此图像是为了阐明我的目标:。我个人没能做到这一点。谢谢你抽出时间!欢迎使用任何伪代码或java代码

可以从逻辑上重新定义栅格,使中心为0,0。 然后,根据距中心的距离对每个栅格坐标进行排序。 这假定栅格的宽度为奇数

例如:

class Point
{
    int x;
    int y;
}

Point gridCenter;


Comparator<Point> comparator = new Comparator<Point>()
{
    @Override
    public int compare(Point arg0, Point arg1) 
    {
        int x = arg0.x - gridCenter.x;
        int y = arg0.y - gridCenter.y;

        int distance0 = x*x + y*y;

        x = arg1.x - gridCenter.x;
        y = arg1.y - gridCenter.y;
        int distance1 = x*x + y*y;


        return distance0 - distance1;
    }
};

void test()
{
    int width = 11;
    int height = 13;

    gridCenter = new Point();
    gridCenter.x = width/2;
    gridCenter.y = height/2;

    List<Point> points = new ArrayList<>();

    for(int x=0;x<width;x++)
    {
        for(int y=0;y<height;y++)
        {
            Point p = new Point();
            p.x = x;
            p.y = y;

            points.add(p);
        }
    }

    Collections.sort(points, comparator);

    for(Point p : points)
    {
        System.out.println(p.x + "," + p.y);
    }

}
类点
{
int x;
int-y;
}
点网格中心;
比较器比较器=新比较器()
{
@凌驾
公共整数比较(点arg0,点arg1)
{
intx=arg0.x-gridCenter.x;
int y=arg0.y-gridCenter.y;
整数距离0=x*x+y*y;
x=arg1.x-gridCenter.x;
y=arg1.y—gridCenter.y;
整数距离1=x*x+y*y;
返回距离0-距离1;
}
};
无效测试()
{
整数宽度=11;
整数高度=13;
gridCenter=新点();
gridCenter.x=宽度/2;
gridCenter.y=高度/2;
列表点=新的ArrayList();
对于(int x=0;x