根据半径计算从正方形中心到边缘的矢量
给定一个正方形(由 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
该向量将为
center + (cos(angle), sin(angle))*magnitude
。鉴于您想要将其与正方形相交,您需要确定大小。你可以用一个平方得到:但是,cos(角度) 或 sin(角度) 可能为零,所以你应该交叉乘以得到:
你可以从中轻松得到终点。
编辑:这是一个您可以放置的片段,以验证这是否适用于当前接受的答案:
显然,这适用于轴对齐的矩形。您可以通过查找测试向量与多边形中每条边之间最近的交点来执行类似的操作。 (您可以进一步优化,但这留给读者作为练习。)
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:However, cos(angle) or sin(angle) could be zero, so you should cross multiply that out to get:
And you can trivially get the end point from that.
EDIT: Here's a snippet you can drop in place to verify this works with the currently accepted answer:
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.)
编辑:矩形的正确解决方案。如果宽度或高度为零,它甚至不会崩溃!
语言:C++。
tan(89.99999) 感谢 James Fassett 测试我的代码。
EDIT: the correct solution for rectangles. It even does not crash if width or height are zeros!
Language: C++.
tan(89.99999) thanks to James Fassett for testing my code.
编辑: Pavel 现在有另一个工作实现(他致力于调试他的解决方案),但我将把它留在这里作为另一种仅适用于正方形的替代方案(Pavel 的适用于矩形) )。
Edit: There is another working implementation from Pavel now (good dedication from him to put in effort debugging his solution) but I'll leave this here as another alternative that works only for squares (Pavel's works for Rectangles).
给定正方形的宽度和高度,您就可以确定正方形的中心 (x+.5w, y+.5h)。
从这里,您可以使用一些三角函数来确定矢量线的长度:
tan(angle) = 0.5x / a
其中 a = 正方形中心与正方形边缘之间的距离。那么你的点就是 x = a, y = (高度)。
请温柔一些,因为我已经有一段时间没有大量使用这些数学了! :-)
Given the square's width and height you can then determine the center of the square (x+.5w, y+.5h).
From there, you can use some trigonometry to determine the length of the vector line:
tan(angle) = 0.5x / a
where a = the distance between the center of the square and the edge of the square. Your points are then x = a, y = (height).
Please be gentle, as it has been some time since I've used a lot of this math! :-)
推广到矩形,如果 a = 向量与水平方向顺时针递增的角度,则可以通过以下方式计算点坐标:
则该点的坐标为:
Generalized to rectangles, if a = the angle of vector from the horizontal increasing counter cloclkwise, then the points coordinates can be calculated by the following:
Then coordinates of the point are: