用另一个向量就地更新一个向量的快速方法
我有一个向量 A,由角度和长度表示。我想添加向量 B,更新原始 A。B 来自查找表,因此可以用任何一种使计算更容易的方式表示它。
具体来说,A 的定义如下:
uint16_t A_angle; // 0-65535 = 0-2π
int16_t A_length;
近似值很好。没有必要检查溢出。可以使用快速正弦/余弦近似。
我能想到的最快的方法是将B表示为分量向量,将A转换为分量,将A和B相加,将结果转换回角度/长度并替换A。(这需要添加快速asin/acos)
我不太擅长数学,想知道我是否缺少一种更明智的方法?
我主要是在寻找一种通用方法,但关于 C 中有用的微优化的具体答案/评论也很有趣。
I have a vector A, represented by an angle and a length. I want to add vector B, updating the original A. B comes from a lookup table, so it can be represented in which ever way makes the computation easier.
Specifically, A is defined thusly:
uint16_t A_angle; // 0-65535 = 0-2π
int16_t A_length;
Approximations are fine. Checking for overflow is not necessary. A fast sin/cos approximation is available.
The fastest way I can think is to have B represented as a component vector, convert A to component, add A and B, convert the result back to angle/length and replace A. (This requires the addition of a fast asin/acos)
I am not especially good at math and wonder if I am missing a more sensible approach?
I am primarily looking for a general approach, but specific answers/comments about useful micro-optimizations in C is also interesting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您需要进行大量加法运算,则可能值得考虑将所有内容存储在笛卡尔坐标中,而不是极坐标中。
Polar 非常适合旋转操作(我猜还有缩放),但坚持使用笛卡尔(旋转是四次乘法,见下文)可能比每次需要时使用 cos/sin/acos/asin 更便宜进行向量加法。当然,这取决于您的案例中的操作分布。
仅供参考,笛卡尔坐标系中的旋转如下(请参阅 http://en.wikipedia.org/wiki/Rotation_matrix ):
如果提前知道
a
,则可以预先计算cos(a)
和sin(a)
。If you need to do a lot of additive operations, it would probably be worth considering storing everything in Cartesian coordinates, rather than polar.
Polar is well-suited to rotation operations (and scaling, I guess), but sticking with Cartesian (where a rotation is four multiplies, see below) is probably going to be cheaper than using cos/sin/acos/asin every time you want to do a vector addition. Although, of course, it depends on the distribution of operations in your case.
FYI, a rotation in Cartesian coordinates is as follows (see http://en.wikipedia.org/wiki/Rotation_matrix):
If
a
is known ahead of time, thencos(a)
andsin(a)
can be precomputed.