publicclassVector2D{privatedouble x;privatedouble y;publicVector2D(){
x =0;
y =0;}publicVector2D(double _x,double _y){
x = _x;
y = _y;}//获取弧度publicdoublegetRadian(){returnMath.atan2(y, x);}//获取角度publicdoublegetAngle(){returngetRadian()/Math.PI *180;}publicVector2Dclone(){returnnewVector2D(x,y);}publicdoublegetLength(){returnMath.sqrt(getLengthSQ());}publicdoublegetLengthSQ(){return x * x + y * y;}//向量置零publicVector2DZero(){
x =0;
y =0;returnthis;}publicbooleanisZero(){return x ==0&& y ==0;}//向量的长度设置为我们期待的valuepublicvoidsetLength(double value){double _angle =getAngle();
x =Math.cos(_angle)* value;
y =Math.sin(_angle)* value;}//向量的标准化(方向不变,长度为1)publicVector2Dnormalize(){double length =getLength();
x = x / length;
y = y / length;returnthis;}//是否已经标准化publicbooleanisNormalized(){returngetLength()==1.0;}//向量的方向翻转publicVector2Dreverse(){
x =-x;
y =-y;returnthis;}//2个向量的数量积(点积)publicdoubledotProduct(Vector2D v){return x * v.x + y * v.y;}//2个向量的向量积(叉积)publicdoublecrossProduct(Vector2D v){return x * v.y - y * v.x;}//计算2个向量的夹角弧度//参考点积公式:v1 * v2 = cos<v1,v2> * |v1| *|v2|publicstaticdoubleradianBetween(Vector2D v1,Vector2D v2){if(!v1.isNormalized()) v1 = v1.clone().normalize();// |v1| = 1if(!v2.isNormalized()) v2 = v2.clone().normalize();// |v2| = 1returnMath.acos(v1.dotProduct(v2));}//弧度 = 角度乘以PI后再除以180、 推理可得弧度换算角度的公式//弧度转角度publicstaticdoubleradian2Angle(double radian){return radian /Math.PI *180;}//向量加publicVector2Dadd(Vector2D v){returnnewVector2D(x + v.x, y + v.y);}//向量减publicVector2Dsubtract(Vector2D v){returnnewVector2D(x - v.x, y - v.y);}//向量乘publicVector2Dmultiply(double value){returnnewVector2D(x * value, y * value);}//向量除publicVector2Ddivide(double value){returnnewVector2D(x / value, y / value);}publicdoublegetX(){return x;}publicdoublegetY(){return y;}}