如何检查是否要在手机外面绘制圆的两侧

时间:2015-05-05 08:57:20

标签: java android canvas

因此,如果您有一个在Android手机上绘制随机圆圈的应用程序,它将在屏幕上的任意位置绘制x,y。好吧,如果你的圆的半径为200,则绘制在0,0。只有一半的圆圈会出现在用户面前。另一半在网络空间。那么你如何测试是否在画布外画出一个圆圈?这是生成随机x和y

的方法
private int[] generateXY() {
        if (theRandom == null) {
            randomWidthOne =(int) (theRandom.nextInt((int) Math.abs(getWidth()-radiusOne/2)) + radiusOne/2f);
            randomHeightOne = (theRandom.nextInt((int)Math.abs((getHeight()-radiusOne/2 + radiusOne/2f))));
        } else {
            randomWidthOne =(int) (theRandom.nextInt((int) Math.abs(getWidth()-radiusOne/2)) + radiusOne/2f);
            randomHeightOne = (theRandom.nextInt((int)Math.abs((getHeight()-radiusOne/2 + radiusOne/2f))));
        }
        return new int[] {randomWidthOne, randomHeightOne};
    }

1 个答案:

答案 0 :(得分:1)

设x,y为圆的中心,r为半径。 设h为屏幕的高度,宽度为w。 我假设屏幕的下边缘的高度为y = 0, 顶部是y = h。

if(  ((x+radius)>w)  || ((x-radius)<0)  ||  ((y-r)<0) || ((y+r)>h) )
    return true; //circle falls outside the screen
else
    return false;  //the entire circle's are is inside the screen