问题描述
给定一个正方形(由 x、y、宽度、高度描述)和一个角度(以弧度为单位),我需要计算一个矢量,该矢量起源于正方形中心并终止于与正方形边缘碰撞的点处给定的角度.
Given a square (described by x, y, width, height) and an angle (in radians) I need to calculate a vector that originates at the squares centre and terminates at the point that collides with the edge of the square at the given angle.
我真的对它碰撞的点最感兴趣,所以如果这会使计算更有效,请告诉我.
I'm really most interested in the point it collides at so if that would make calculation more efficient let me know.
这可以推广到矩形吗?一般的多边形怎么样?
Can this be generalized to Rectangles? How about polygons in general?
推荐答案
向量将为 center + (cos(angle), sin(angle))*magnitude
.鉴于您想将其与正方形相交,您需要确定大小.你可以用一个正方形来得到它:
The vector will be center + (cos(angle), sin(angle))*magnitude
. Given that you want to intersect this with a square, you need to determine magnitude. You can get that with a square with:
float abs_cos_angle= fabs(cos(angle));
float abs_sin_angle= fabs(sin(angle));
if (width/2/abs_cos_angle <= height/2/abs_sin_angle)
{
magnitude= fabs(width/2/abs_cos_angle);
}
else
{
magnitude= height/2/abs_sin_angle;
}
但是,cos(angle) 或 sin(angle) 可能为零,因此您应该将其相乘得到:
However, cos(angle) or sin(angle) could be zero, so you should cross multiply that out to get:
float abs_cos_angle= fabs(cos(angle));
float abs_sin_angle= fabs(sin(angle));
if (width/2*abs_sin_angle <= height/2*abs_cos_angle)
{
magnitude= width/2/abs_cos_angle;
}
else
{
magnitude= height/2/abs_sin_angle;
}
你可以很容易地从中得到终点.
And you can trivially get the end point from that.
这里有一个片段,您可以放在适当的位置以验证它是否适用于当前接受的答案:
Here's a snippet you can drop in place to verify this works with the currently accepted answer:
double magnitude;
double abs_cos_angle= fabs(cos(angle));
double abs_sin_angle= fabs(sin(angle));
if (width/2*abs_sin_angle <= height/2*abs_cos_angle)
{
magnitude= width/2/abs_cos_angle;
}
else
{
magnitude= height/2/abs_sin_angle;
}
double check_x= x + cos(angle)*magnitude;
double check_y= y + sin(angle)*magnitude;
printf(" a = %d deg: x = %lf; y = %lf
",(int)(angle/pi*180),check_x,check_y);
显然这适用于轴对齐的矩形.您可以通过找到测试向量和多边形中每条边之间最近的交点来做类似的事情.(您可以进一步优化它,但这留给读者作为练习.)
Clearly this is applies to an axis aligned rectangle. You can do something similar by finding the closest intersection between the testing vector and every edge in a polygon. (You can optimize that further, but that's left as an exercise to the reader.)
这篇关于根据半径计算从正方形中心到边缘的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!