检索一个正的或从3个点的负角 [英] Retrieve a positive or a negative angle from 3 points

查看:137
本文介绍了检索一个正的或从3个点的负角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我旋转点周围在二维空间中的中心点。点是中心点,旧的鼠标位置,而新的鼠标位置。我的旋转功能工作正常,我可以计算出完美的角度。但我想计算,如果用户移动他们的鼠标,其中应该被解释为逆时针的方向上的负角度

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.

例如,移向鼠标右(正x轴)应当如果您是上述(小于)的中心点的y值顺时针旋转,但它应该如果实际上低于(大于)的中心点的y值逆时针转动。

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.

下面是我有:

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.

推荐答案

试试这个,但我不是舒尔:

try this, but i'm not shure:

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);

这篇关于检索一个正的或从3个点的负角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆