afVec2.cpp
资源名称:AirForce.rar [点击查看]
上传用户:kaiguan
上传日期:2007-10-28
资源大小:1074k
文件大小:1k
源码类别:
其他游戏
开发平台:
Visual C++
- #pragma warning(disable: 4514)
- #include "afVec2.h"
- #include "afMathTool.h"
- #include <assert.h>
- #include <math.h>
- float afVec2::cosine(const afVec2& left, Axis nAxis)
- {
- float alpha;
- switch(nAxis)
- {
- default:
- case X_AXIS:
- alpha = (float)acos(left[0]);
- if (left[1]<0.0f)
- alpha = afFTWOPI - alpha;
- return alpha;
- case Y_AXIS:
- alpha = (float)acos(left[1]);
- if (left[0]>0.0f)
- alpha = afFTWOPI - alpha;
- return alpha;
- }
- return -1.0f;
- }
- float afVec2::cosine(const afVec2& left, const afVec2& right)
- {
- float alpha,beta,dif;
- alpha = cosine(left);
- beta = cosine(right);
- dif = beta-alpha;
- if (dif > afFPI)
- dif = -afFTWOPI + dif;
- else
- if (dif < -afFPI)
- dif = afFTWOPI + dif;
- assert(dif<=afFPI && dif>=-afFPI);
- return dif;
- }
- float afVec2::distance(const afVec2& vec) const
- {
- return (float)sqrt((x-vec.x)*(x-vec.x)+(y-vec.y)*(y-vec.y));
- }
- float afVec2::length() const
- {
- return (float)sqrt(x*x+y*y);
- }
- float afVec2::normalize()
- {
- float len = x*x+y*y;
- if (len!=1.0)
- {
- if (len>0.00001f)
- {
- len = (float)sqrt(len);
- x /= len;
- y /= len;
- }
- else
- len = 0.0;
- }
- return len;
- }