Java 压缩代码以检查轴方向,并在点之间循环运行

Java 压缩代码以检查轴方向,并在点之间循环运行,java,optimization,Java,Optimization,我有两个X,Y点,分别表示左上角和右下角,从这里我想推断出轴的方向,并运行一个循环,在这些点之间改变当前位置 石头上设置的点的唯一信息是x/y是区域的左上角,x2/y2是右下角。但是,位置可能在任何位置(轴的正或负),轴可能在任一方向上运行 我写了一些东西来解释这一切。。。。但是有没有办法让它不那么笨重和丑陋呢 int width = 20; int height = 10; int x = 1; int y = 1; int x2 = 3; in

我有两个X,Y点,分别表示左上角和右下角,从这里我想推断出轴的方向,并运行一个循环,在这些点之间改变当前位置

石头上设置的点的唯一信息是x/y是区域的左上角,x2/y2是右下角。但是,位置可能在任何位置(轴的正或负),轴可能在任一方向上运行

我写了一些东西来解释这一切。。。。但是有没有办法让它不那么笨重和丑陋呢

    int width = 20;
    int height = 10;
    int x = 1;
    int y = 1;
    int x2 = 3;
    int y2 = 3;     
    //Variables designating which way the axes run
    int altx = (x<=x2) ? 1:-1;
    int alty = (y<=y2) ? 1:-1;

    while (
            (
            //Check which way the x-axis runs and if we are between the points 
                (altx==1 && currentposition.getX() >= x && currentposition.getX() <= x2 ) 
                || (altx==-1 && currentposition.getX() <= x && currentposition.getX() >= x2)
            )
            &&
            (
            //Check which way the y-axis runs and if we are between the points 
                (alty==1 && currentposition.getX() >= y && currentposition.getX() <= y2 ) 
                || (alty==-1 && currentposition.getX() <= y && currentposition.getX() >= y2)
            )
          ) 
                {           
                    currentposition.moveXRelative(width*alt);
                    currentposition.moveYRelative(height*alt);
                }
int-width=20;
整数高度=10;
int x=1;
int y=1;
int x2=3;
int y2=3;
//指定轴运行方向的变量

int altx=(xI个人会使用一些私有函数
currX()
左右,而不是
currentposition.getX()
。顺便说一句,对于正在使用的y轴
getX()
也是。可能是错误。如果您检查
>0
<0
而不是
==1
=-1
也会保存1个字母。如果您使用布尔值(
bool altx=xy),您可以为
currentposition.getX()>=x&¤tposition.getX()计算出一个函数
withinRange
“有没有办法让它不那么笨重和难看?”这个问题似乎更适合我。