📄 vector4d.java
字号:
/**
*
*/
package Math;
/**
* @author PanXu
*
*/
public class Vector4D {
/**
*
*/
public static final int X = 0 ;
public static final int Y = 0 ;
public static final int Z = 0 ;
public static final int W = 0 ;
public float M[] = null;
public Vector4D() {
super();
// TODO 自动生成构造函数存根
M = new float [4];
M[0] = 0 ;
M[1] = 0 ;
M[2] = 0 ;
M[3] = 0 ;
}
public Vector4D(float x , float y ,float z) {
super();
// TODO 自动生成构造函数存根
M = new float [4];
M[0] = x ;
M[1] = y ;
M[2] = z ;
M[3] = 1.0f;
}
/*
* Build
*/
public void Init (float x ,float y , float z)
{
this .M[0] = x;
this .M[1] = y;
this .M[2] = z;
this .M[3] = 1.0f;
}
public static Vector4D Build_Vec4D_Stack (Point4D s , Point4D e)
{
return new Vector4D (e.M[0] - s.M[0] , e.M[1] - s.M[1] , e.M[2] - s.M[2]);
}
public void Build_Vec4D (Point4D s , Point4D e)
{
this .M[0] = e.M[0] - s.M[0];
this .M[1] = e.M[1] - s.M[1];
this .M[2] = e.M[2] - s.M[2];
this .M[3] = 1.0f;
}
public void Set_Zero ()
{
this .M[0] = 0 ; this .M[1] = 0; this .M[2] = 0;this .M[3] = 1.0f;
}
/*
* 规格化
*/
public void DIVW(){
float Divw = 1 / this .M[3];
this .M[0] *= Divw;
this .M[1] *= Divw;
this .M[2] *= Divw;
}
/*
* 归一化
*/
public void NORMALIZE (float DivLen)
{
this .M[0] *= DivLen ;
this .M[1] *= DivLen ;
this .M[2] *= DivLen ;
this .M[3] *= 1.0f ;
}
public Vector4D NORMALIZE (Vector4D res , float DivLen)
{
res .M[0] *= DivLen;
res .M[1] *= DivLen;
res .M[2] *= DivLen;
res .M[3] = 1.0f;
return res ;
}
/*
* 运算(要求准确,必须为规格化对象)
*/
public Vector4D Vec4D_ADD_Stack (Vector4D a , Vector4D b){
Vector4D res = new Vector4D();
res .M[0] = a .M[0] + b.M[0];
res .M[1] = a .M[1] + b.M[1];
res .M[2] = a .M[2] + b.M[2];
res .M[3] = 1.0f;
return res ;
}
public void Vec4D_ADD (Vector4D a , Vector4D b){
this .M[0] = a .M[0] + b.M[0];
this .M[1] = a .M[1] + b.M[1];
this .M[2] = a .M[2] + b.M[2];
this .M[3] = 1.0f;
}
//减
public Vector4D Vec4D_SUB_Stack (Vector4D a , Vector4D b){
Vector4D res = new Vector4D();
res .M[0] = a .M[0] - b.M[0];
res .M[1] = a .M[1] - b.M[1];
res .M[2] = a .M[2] - b.M[2];
res .M[3] = 1.0f;
return res ;
}
public void Vec4D_SUB (Vector4D a , Vector4D b){
this .M[0] = a .M[0] - b.M[0];
this .M[1] = a .M[1] - b.M[1];
this .M[2] = a .M[2] - b.M[2];
this .M[3] = 1.0f;
}
//点积
public float Vec4D_DOT (Vector4D a , Vector4D b)
{
return (a.M[0] * b.M[0] + a.M[1] * b.M[1] + a.M[2] * b.M[2]);
}
//叉积
public Vector4D Vec4D_Cross (Vector4D a , Vector4D b)
{
Vector4D res = new Vector4D();
res .M[X] = a .M[Y] * b.M[Z] - a.M[Z] * b.M[Y];
res .M[Y] = a .M[X] * b.M[Z] - a.M[Z] * b.M[X];
res .M[Z] = a .M[X] * b.M[Y] - a.M[Y] * b.M[X];
res .M[W] = 1.0f;
return res ;
}
//矩阵乘以向量
public Vector4D MAT4X4_MUL_VEC4D (Matrix4X4 Mat)
{
Vector4D res = new Vector4D();
res .M[0] = M[0] * Mat.M[0][0] + M[1] * Mat.M[1][0] + M[2] * Mat.M[2][0] + M[3] * Mat.M[3][0];
res .M[1] = M[0] * Mat.M[0][1] + M[1] * Mat.M[1][1] + M[2] * Mat.M[2][1] + M[3] * Mat.M[3][1];
res .M[2] = M[0] * Mat.M[0][2] + M[1] * Mat.M[1][2] + M[2] * Mat.M[2][2] + M[3] * Mat.M[3][2];
res .M[3] = M[0] * Mat.M[0][3] + M[1] * Mat.M[1][3] + M[2] * Mat.M[2][3] + M[3] * Mat.M[3][3];
return res ;
}
/**
* 打印(调试)
*/
public void Point ()
{
System .out .println(this .M[0] + " " + this .M[1] + " " +
this .M[2] + " " + this .M[3]);
System .out .println();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -