我有两个点A、B 和角度x。假设 vector Aa 和 Bb 的角度为 x。
如果 vector Bb中存在A'满足 AA'b = 90 degree
然后
- 如果A' = B 那么
Compare(A, B) = 0
- 其他
Compare(A, B) > 0
其他 Compare(A, B) < 0
POINT a, b;
float x; // in radian
int Compare(POINT a, POINT b, float x)
{
}
示例:
在下图中, vector Bb中存在A',但 vector Aa'中不存在B'> 满足给定条件,所以 A> B。
问题:在 Compare()
函数我需要使用什么代码来获得所需的值?
如果您知道如何使用 Dot Product 就很简单了两个 vector 的。
// Assumed structure:
struct POINTF{
float x, y;
};
// include math.h for sin() and cos()
int Compare(POINTF a, POINTF b, float x)
{
POINTF diff = {a.x-b.x, a.y-b.y};
POINTF base = {cos(x), sin(x)};
float r = diff.x * base.x + diff.y * base.y;
if (r == 0.0f) return 0;
return (r > 0.0f) ? 1 : -1;
}
实际上您不需要两个临时 vector 。上面的代码旨在告诉你两个 vector 的点积的计算。您可以将代码简化为
int Compare(POINTF a, POINTF b, float x)
{
float r = (a.x - b.x) * cos(x) + (a.y - b.y) * sin(x);
return (r == 0.0f) ? 0 : ((r > 0.0f) ? 1 : -1);
}