从 3 个点检索正角或负角
我正在围绕二维空间中的中心点旋转点。这些点是中心点、旧鼠标位置和新鼠标位置。我的旋转功能运行良好,我可以完美地计算角度。但如果用户沿应解释为逆时针方向移动鼠标,我想计算负角度。
例如,如果您位于(小于)中心点的 y 值之上(小于),则向右(正 x 轴)移动鼠标应顺时针旋转,但如果您实际上位于(大于)中心点的 y 值,则应逆时针旋转中心点的 y 值。
这就是我所拥有的:
PointF centerPoint;
PointF oldPoint;
PointF newPoint;
double Xc = centerPoint.X;
double Yc = centerPoint.Y;
double Xb = oldPoint.X;
double Yb = oldPoint.Y;
double Xa = newPoint.X;
double Ya = newPoint.Y;
double c2 = (Math.Pow(Xb - Xa, 2) + Math.Pow(Yb - Ya, 2));
double a2 = (Math.Pow(Xb - Xc, 2) + Math.Pow(Yb - Yc, 2));
double b2 = (Math.Pow(Xa - Xc, 2) + Math.Pow(Ya - Yc, 2));
double a = Math.Sqrt(a2);
double b = Math.Sqrt(b2);
double val = (a2 + b2 - c2) / (2 * a * b);
double angle = Math.Acos(val);
所以我需要一种在需要时使角度为负值的方法,以便这些点顺时针或逆时针旋转以跟随鼠标位置。
I am rotating points around a center point in 2D space. The points are the center point, the old mouse position, and the new mouse position. My rotation function works fine, and I can calculate the angle perfectly. But I want to calculate a negative angle if the user is moving their mouse in a direction which should be interpreted as counter-clockwise.
For example, moving the mouse toward the right (positive x-axis) should rotate clockwise if you are above (less than) the y value of the center point, but it should rotate counter-clockwise if you are actually below (greater than) the y value of the center point.
Here's what I have:
PointF centerPoint;
PointF oldPoint;
PointF newPoint;
double Xc = centerPoint.X;
double Yc = centerPoint.Y;
double Xb = oldPoint.X;
double Yb = oldPoint.Y;
double Xa = newPoint.X;
double Ya = newPoint.Y;
double c2 = (Math.Pow(Xb - Xa, 2) + Math.Pow(Yb - Ya, 2));
double a2 = (Math.Pow(Xb - Xc, 2) + Math.Pow(Yb - Yc, 2));
double b2 = (Math.Pow(Xa - Xc, 2) + Math.Pow(Ya - Yc, 2));
double a = Math.Sqrt(a2);
double b = Math.Sqrt(b2);
double val = (a2 + b2 - c2) / (2 * a * b);
double angle = Math.Acos(val);
So I need a way to make angle negative when it needs to be, so the points rotate clockwise or counter-clockwise to follow the mouse position.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个,但我不确定:
Try this, but I'm not sure:
看起来您所需要做的就是
在代码末尾。这将使您在由 centerPoint 和 oldPoint 定义的线的右侧顺时针旋转,并在其左侧逆时针旋转,无论方向如何。
It seems like all you need to do is
at the end of your code. That will give you a clockwise rotation to the right of the line defined by centerPoint and oldPoint, and counter-clockwise to the left of it, regardless of orientation.
给定向量 (x1,y1) 和 (x2,y2),我建议计算叉积和点积,然后对它们使用 Atan2() 。这适用于两个向量均非零且向量长度“合理”的所有情况。
Given vectors (x1,y1) and (x2,y2), I would suggest computing the cross product and dot product, and then using Atan2() on them. That will work in all cases where both vectors are non-zero and vector lengths are "reasonable".