Retrieve a positive or a negative angle from 3 points

眉间皱痕 提交于 2019-11-29 04:51:59
Ingemar

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);
Jim McGivney
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
}

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!