Android的 - 从位置A(​​指数)检查是否现在的位置是B(指数)是对角线它在GridView布局不管接近 [英] Android - Check from position A (index) if postion B (index) is diagonal to it in a gridview layout regardless of proximity

查看:118
本文介绍了Android的 - 从位置A(​​指数)检查是否现在的位置是B(指数)是对角线它在GridView布局不管接近的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有A和B位置的位置或行/列,检查是否B是对角线阿?

  1 2 3
 4 5 6
 7 8 9

我如何检查是否例如,如果5角至7?

另外,如果我检查,看看是否是4角3,它不应该得到真正的回报也?

解答从下面集成在我的处境。

我就可以从另一个函数调用

 !isDiagonal(goodguyposition,positionadd1)公共静态布尔isDiagonal(INT A,INT B)
    {
        // X =列数
        // Y =行数
        // S =指数开始(1)
        // A =的指数
        // B =的索引        INT X = 11;
        // INT Y = 11;
        INT S = 0;
        INT AX =(S - A)%X,AY =(S - A)/ X,BX =(S - B)%X,用=(S - B)/ X;        如果((斧== BX - 1 ||斧== BX + 1)及及(AY ==由 - 1 || AY ==由+ 1))
        {
            返回true;
        }
        其他
        {
            返回false;
        }    }


解决方案

响应清洗可读性:

 公共静态布尔isAdjacentDiagonal(INT X,int类型,int类型的,INT B){
    // X =列数
    // S =指数开始
    // A =的指数
    // B =的索引
    INT AX =(A - S)为%x,AY =(A - S)/ X,BX =(B - S)为%x,通过=(B - S)/ X;
    回报(BX ==斧 - 1 || BX == AX + 1)及和放大器; (由== AY - 1 ||由== AY + 1);
}

使用Math.abs且仅当你的索引从0开始的:

 公共静态布尔isAdjacentDiagonal(INT X,int类型的,整数B){
    INT AX =一%×,AY = A / X,BX = B%X,由= B / X;
    返回Math.abs(斧 - BX)== 1和;&放大器; Math.abs(AY - 通过)== 1;
}公共静态布尔isOneOfDiagonals(INT X,int类型的,整数B){
    INT AX =一%×,AY = A / X,BX = B%X,由= B / X;
    返回= B和;!&安培; Math.abs(AX - BX)== Math.abs(AY - 通过);
}

If I have a position or row/column for both A and B positions, check to see if B is diagonal to A?

 1 2 3
 4 5 6
 7 8 9

How do I check if for example if 5 is diagonal to 7?

Also if I check to see if 4 is diagonal 3, it shouldn't get a true return also?

Answer from below integrated in my situation.

My call on it from another function

!isDiagonal(goodguyposition, positionadd1)

public static boolean isDiagonal(int a, int b) 
    {
        // x = number of columns
        // y = number of rows
        // s = index start (1)
        // a = index of a
        // b = index of b

        int x = 11;
        //int y = 11;
        int s = 0;
        int ax = (s - a) % x, ay = (s - a) / x, bx = (s - b) % x, by = (s - b) / x;

        if ((ax == bx - 1 || ax == bx + 1) && (ay == by - 1 || ay == by + 1))
        {
            return true;
        }
        else
        {
            return false;
        }

    }

解决方案

Response cleaned for readability:

public static boolean isAdjacentDiagonal(int x, int s, int a, int b) {
    // x = number of columns
    // s = index start
    // a = index of a
    // b = index of b
    int ax = (a - s) % x, ay = (a - s) / x, bx = (b - s) % x, by = (b - s) / x;
    return (bx == ax - 1 || bx == ax + 1) && (by == ay - 1 || by == ay + 1);
}

With Math.abs and only if your index starts at 0:

public static boolean isAdjacentDiagonal(int x, int a, int b) {
    int ax = a % x, ay = a / x, bx = b % x, by = b / x;
    return Math.abs(ax - bx) == 1 && Math.abs(ay - by) == 1;
}

public static boolean isOneOfDiagonals(int x, int a, int b) {
    int ax = a % x, ay = a / x, bx = b % x, by = b / x;
    return a != b && Math.abs(ax - bx) == Math.abs(ay - by);
}

这篇关于Android的 - 从位置A(​​指数)检查是否现在的位置是B(指数)是对角线它在GridView布局不管接近的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆