从给定角度获取屏幕边缘的坐标
Get the coordinates at the edge of the screen from a given angle
我知道起点(屏幕中间)和角度(在我的示例中为 20°)。现在我想知道屏幕边缘的位置,就像一条看不见的线以给定的角度从中心画到边缘。为了更好的解释,我附上了一张图片:
一种方法是计算半径等于或大于最大对角线的圆上的一个点,然后将其裁剪到屏幕边界。
利用毕达哥拉斯定理,最大对角线的长度为
float d = Math.sqrt((width/2)*(width/2) + (height/2)*(height/2));
所以你可以像这样计算圆上的点(角度是从顶部顺时针方向的弧度):
float x = Math.sin(angle) * d;
float y = -Math.cos(angle) * d;
然后你必须将向量从原点到点剪裁到 4 个边中的每一个,例如左右边:
if(x > width/2)
{
float clipFraction = (width/2) / x; // amount to shorten the vector
x *= clipFraction;
y *= clipFraction;
}
else if(x < -width/2)
{
float clipFraction = (-width/2) / x; // amount to shorten the vector
x *= clipFraction;
y *= clipFraction;
}
也为 height/2 和 -height/2 执行此操作。然后最后你可以将 width/2, height/2 添加到 x 和 y 以获得最终位置(屏幕中心是 width/2, height/2 而不是 0,0):
x += width/2
y += height/2
我知道起点(屏幕中间)和角度(在我的示例中为 20°)。现在我想知道屏幕边缘的位置,就像一条看不见的线以给定的角度从中心画到边缘。为了更好的解释,我附上了一张图片:
一种方法是计算半径等于或大于最大对角线的圆上的一个点,然后将其裁剪到屏幕边界。
利用毕达哥拉斯定理,最大对角线的长度为
float d = Math.sqrt((width/2)*(width/2) + (height/2)*(height/2));
所以你可以像这样计算圆上的点(角度是从顶部顺时针方向的弧度):
float x = Math.sin(angle) * d;
float y = -Math.cos(angle) * d;
然后你必须将向量从原点到点剪裁到 4 个边中的每一个,例如左右边:
if(x > width/2)
{
float clipFraction = (width/2) / x; // amount to shorten the vector
x *= clipFraction;
y *= clipFraction;
}
else if(x < -width/2)
{
float clipFraction = (-width/2) / x; // amount to shorten the vector
x *= clipFraction;
y *= clipFraction;
}
也为 height/2 和 -height/2 执行此操作。然后最后你可以将 width/2, height/2 添加到 x 和 y 以获得最终位置(屏幕中心是 width/2, height/2 而不是 0,0):
x += width/2
y += height/2