从 3 个点检索正角或负角

发布于 2024-12-29 16:47:26 字数 797 浏览 1 评论 0原文

我正在围绕二维空间中的中心点旋转点。这些点是中心点、旧鼠标位置和新鼠标位置。我的旋转功能运行良好,我可以完美地计算角度。但如果用户沿应解释为逆时针方向移动鼠标,我想计算负角度。

例如,如果您位于(小于)中心点的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你怎么这么可爱啊 2025-01-05 16:47:26

试试这个,但我不确定:

double v1x = Xb - Xc;
double v1y = Yb - Yc;
double v2x = Xa - Xc;
double v2y = Ya - Yc;

double angle = Math.Atan2(v1x, v1y) - Math.Atan2(v2x, v2y);

Try this, but I'm not sure:

double v1x = Xb - Xc;
double v1y = Yb - Yc;
double v2x = Xa - Xc;
double v2y = Ya - Yc;

double angle = Math.Atan2(v1x, v1y) - Math.Atan2(v2x, v2y);
雨的味道风的声音 2025-01-05 16:47:26
private double AngleFrom3PointsInDegrees(double x1, double y1, double x2, double y2, double x3, double y3)
{
    double a = x2 - x1;
    double b = y2 - y1;
    double c = x3 - x2;
    double d = y3 - y2;

    double atanA = Math.Atan2(a, b);
    double atanB = Math.Atan2(c, d);

    return (atanA - atanB) * (-180 / Math.PI); 
    // if Second line is counterclockwise from 1st line angle is 
    // positive, else negative
}
private double AngleFrom3PointsInDegrees(double x1, double y1, double x2, double y2, double x3, double y3)
{
    double a = x2 - x1;
    double b = y2 - y1;
    double c = x3 - x2;
    double d = y3 - y2;

    double atanA = Math.Atan2(a, b);
    double atanB = Math.Atan2(c, d);

    return (atanA - atanB) * (-180 / Math.PI); 
    // if Second line is counterclockwise from 1st line angle is 
    // positive, else negative
}
乱世争霸 2025-01-05 16:47:26

看起来您所需要做的就是

angle = angle > Math.PI ? angle - 2*Math.PI : angle;

在代码末尾。这将使您在由 centerPoint 和 oldPoint 定义的线的右侧顺时针旋转,并在其左侧逆时针旋转,无论方向如何。

It seems like all you need to do is

angle = angle > Math.PI ? angle - 2*Math.PI : angle;

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文