参考:http://www.cppblog.com/lovedday/archive/2009/01/15/22890.html
本文主要实现了利用向量的叉积和点积来计算向量之间的夹角的问题。
向量的点积公式是:
假设向量
u(
ux,
uy)和
v(
vx,
vy),
u和
v之间的夹角为α,从三角形的边角关系等式出发,可作出如下简单推导:
|
u -
v||
u -
v| = |
u||
u| + |
v||
v|
- 2|
u||
v|cosα
===>
(
ux -
vx)
2 + (
uy -
vy)
2
=
ux2 +
uy2 +
vx2+
vy2-
2|
u||
v|cosα
===>
-2
uxvx - 2
uyvy
= -2|
u||
v|cosα
===>
cosα = (
uxvx +
uyvy) / (|
u||
v|)
向量的叉积公式是:
u
x * vy - vx * uy = (|
u||
v|) * sina
这样,就可以根据向量
u和
v的坐标值计算出它们之间的夹角。
POINT BPoint;
BPoint.x = AOrigin.x;
BPoint.y = AOrigin.y - 10;//以屏幕的左上角为中心点
double ALength = sqrt((APoint.x - AOrigin.x)*(APoint.x - AOrigin.x)+(APoint.y - AOrigin.y)*(APoint.y - AOrigin.y));
double BLength = 10;
int dJ = (AOrigin.x - APoint.x) * (AOrigin.x - BPoint.x) + (AOrigin.y - APoint.y) * (AOrigin.y - BPoint.y);
int cJ = (AOrigin.x - APoint.x) * (AOrigin.y - BPoint.y) - (AOrigin.x - BPoint.x) * (AOrigin.y - APoint.y);
double cosResult = dJ * 1.0/(ALength*BLength);
sinResult = cJ*1.0/(ALength*BLength);
|
阅读(2848) | 评论(0) | 转发(0) |