获取给定角度的屏幕边缘坐标

Ant*_*ict 2 java math coordinates

我知道起点(屏幕中间)和角度(在我的示例中为 20\xc2\xb0)。现在我想知道屏幕边缘的位置,就像以给定角度从中心到边缘绘制一条看不见的线一样。为了更好的解释,我附上了一张图片:

\n\n

例子\n

\n

sam*_*gak 5

一种方法是计算半径等于或大于最大对角线的圆上的点,然后将其剪切到屏幕边界。

使用毕达哥拉斯定理,最大对角线的长度将是

float d = Math.sqrt((width/2)*(width/2) + (height/2)*(height/2));
Run Code Online (Sandbox Code Playgroud)

因此,您可以像这样计算圆上的点(角度以弧度为单位,从顶部顺时针旋转):

float x = Math.sin(angle) * d;
float y = -Math.cos(angle) * d;
Run Code Online (Sandbox Code Playgroud)

然后,您必须将向量从原点到该点剪切到 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;
}
Run Code Online (Sandbox Code Playgroud)

对 height/2 和 -height/2 也执行此操作。最后你可以将宽度/2,高度/2添加到x和y上以获得最终位置(屏幕中心是宽度/2,高度/2而不是0,0):

x += width/2
y += height/2
Run Code Online (Sandbox Code Playgroud)