因此,如果您有一个在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};
}
答案 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