最佳方法检查数组中两个元素是否在对角线上。

huangapple go评论74阅读模式
英文:

Best way to check if two elements of an array of array are in diagonal

问题

我有一个数组的数组的索引(不是基本对象的索引):
第一个元素 = tab[x0][y0]
第二个元素 = tab[x1][y1]

我想要检查这两个元素是否在对角线上,如下所示:

_________________
|   | * |   |   |
-----------------
|   |   |   |   |
-----------------
|   |   |   | * |
-----------------
|   |   |   |   |
-----------------

在这里,这两个点都在对角线上,所以我们应该返回 true。
但是,最好的检查方法是什么?

英文:

I have indexes of two elements of an array of array (not of primitive objects) :
first element = tab[x0][y0]
second element = tab[x1][y1]

And i want to check if both of these elements are in diagonal like :

_________________
|   | * |   |   |
-----------------
|   |   |   |   |
-----------------
|   |   |   | * |
-----------------
|   |   |   |   |
-----------------

Here both of these points are in diagonal, so we should return true.
But what is the best way to check it ?

答案1

得分: 5

将数组视为笛卡尔坐标空间,并计算连接两个元素(点)的斜率(上升与水平位移的比值):

    int rise = y1 - y0;
    int run = x1 - x0;

如果rise除以run等于1或-1,则这两个元素形成对角线。1或-1决定了对角线的方向。不必进行实际除法运算,只需检查它们是否具有相同的符号和大小:

    if (Math.abs(rise) == Math.abs(run)) {
        if (Integer.signum(rise) == Integer.signum(run)) {
            // 对角线
        } else {
            // 另一个方向的对角线
        }
    } else {
        // 非对角线
    }
英文:

Treat the array like a Cartesian coordinate space, and calculate the slope (rise over run) of the line joining the two elements (points):

int rise = y1 - y0;
int run = x1 - x0;

If rise divided by run is 1 or -1, then the two elements form a diagonal. 1 or -1 determines the direction of the diagonal. Rather than dividing, you can just check if they have the same sign and magnitude:

if (Math.abs(rise) == Math.abs(run)) {
    if (Integer.signum(rise) == Integer.signum(run)) {
        // diagonal
    } else {
        // diagonal that is another direction
    }
} else {
    // not diagonal
}

答案2

得分: 3

两个点在对角线上,如果它们的x坐标和y坐标之间的差相同。

x0 - x1 == y0 - y1
英文:

Two points are diagonal if the difference between the x coordinates and the y coordinates is the same.

x0 - x1 == y0 - y1

答案3

得分: 2

只需看坐标。位于对角线上意味着 x 坐标和 y 坐标上的差值相同。因此:

boolean is_diagonal = (x1 - x0) == (y1 - y0);
英文:

Just look at the coordinates. Being on a diagonal means that the differences in the x coordinate and in the y coordinate are the same. Thus:

boolean is_diagonal = (x1 - x0) == (y1 - y0);

答案4

得分: 2

对角线条件如下:

int x = x0 - x1 = 2
int y = y0 - y1 = 2

boolean diagonal = x == y;

任何其他索引组合都会得到一个等式,这意味着它们不是对角线。同时可能需要使用 Math.abs()

英文:

diagonal if

int x = x0 - x1 = 2
int y = y0 - y1 = 2

boolean diagonal = x == y;

any other combination of index will to give you an equation which means they are not diagonal. also probably you will have to use Math.abs()

huangapple
  • 本文由 发表于 2020年10月2日 17:30:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/64169278.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定