📄 matrix3d.java
字号:
import java.applet.*;
import java.awt.*;
class Matrix3D
{
private double Matrix[][];
static double SinusList[] = new double[360+90];
static double Sin(double rad)
{
return SinusList[(int)rad];
}
static double Cos(double rad)
{
return SinusList[(int)rad+90];
}
public Matrix3D()
{
Matrix = new double[4][4];
Matrix[0][0] = 1; Matrix[0][1] = 0; Matrix[0][2] = 0; Matrix[0][3] = 0;
Matrix[1][0] = 0; Matrix[1][1] = 1; Matrix[1][2] = 0; Matrix[1][3] = 0;
Matrix[2][0] = 0; Matrix[2][1] = 0; Matrix[2][2] = 1; Matrix[2][3] = 0;
Matrix[3][0] = 0; Matrix[3][1] = 0; Matrix[3][2] = 0; Matrix[3][3] = 1;
}
public void Identity()
{
Matrix[0][0] = 1; Matrix[0][1] = 0; Matrix[0][2] = 0; Matrix[0][3] = 0;
Matrix[1][0] = 0; Matrix[1][1] = 1; Matrix[1][2] = 0; Matrix[1][3] = 0;
Matrix[2][0] = 0; Matrix[2][1] = 0; Matrix[2][2] = 1; Matrix[2][3] = 0;
Matrix[3][0] = 0; Matrix[3][1] = 0; Matrix[3][2] = 0; Matrix[3][3] = 1;
}
public void Merge(double M[][])
{
double Temp[][] = new double[4][4];
for (int i=0; i<4; i++)
for (int j=0; j<4; j++)
{
Temp[i][j] = (Matrix[i][0] * M[0][j]) +
(Matrix[i][1] * M[1][j]) +
(Matrix[i][2] * M[2][j]) +
(Matrix[i][3] * M[3][j]);
}
for (int i=0; i<4; i++)
{
Matrix[i][0] = Temp[i][0];
Matrix[i][1] = Temp[i][1];
Matrix[i][2] = Temp[i][2];
Matrix[i][3] = Temp[i][3];
}
}
public void Merge(double Dest[][], double Source[][])
{
double Temp[][] = new double[4][4];
for (int i=0; i<4; i++)
for (int j=0; j<4; j++)
{
Temp[i][j] = (Source[i][0] * Dest[0][j]) +
(Source[i][1] * Dest[1][j]) +
(Source[i][2] * Dest[2][j]) +
(Source[i][3] * Dest[3][j]);
}
for (int i=0; i<4; i++)
{
Dest[i][0] = Temp[i][0];
Dest[i][1] = Temp[i][1];
Dest[i][2] = Temp[i][2];
Dest[i][3] = Temp[i][3];
}
}
public void RotateX(double rot)
{
double rotM[][] = new double[4][4];
rotM[0][0] = 1; rotM[0][1] = 0; rotM[0][2] = 0; rotM[0][3] = 0;
rotM[1][0] = 0; rotM[1][1] = Cos(rot); rotM[1][2] = Sin(rot); rotM[1][3] = 0;
rotM[2][0] = 0; rotM[2][1] = -Sin(rot); rotM[2][2] = Cos(rot); rotM[2][3] = 0;
rotM[3][0] = 0; rotM[3][1] = 0; rotM[3][2] = 0; rotM[3][3] = 1;
Merge(rotM);
}
public void RotateY(double rot)
{
double rotM[][] = new double[4][4];
rotM[0][0] = Cos(rot); rotM[0][1] = 0; rotM[0][2] = -Sin(rot); rotM[0][3] = 0;
rotM[1][0] = 0; rotM[1][1] = 1; rotM[1][2] = 0; rotM[1][3] = 0;
rotM[2][0] = Sin(rot); rotM[2][1] = 0; rotM[2][2] = Cos(rot); rotM[2][3] = 0;
rotM[3][0] = 0; rotM[3][1] = 0; rotM[3][2] = 0; rotM[3][3] = 1;
Merge(rotM);
}
public void RotateZ(double rot)
{
double rotM[][] = new double[4][4];
rotM[0][0] = Cos(rot); rotM[0][1] = Sin(rot); rotM[0][2] = 0; rotM[0][3] = 0;
rotM[1][0] = -Sin(rot); rotM[1][1] = Cos(rot); rotM[1][2] = 0; rotM[1][3] = 0;
rotM[2][0] = 0; rotM[2][1] = 0; rotM[2][2] = 1; rotM[2][3] = 0;
rotM[3][0] = 0; rotM[3][1] = 0; rotM[3][2] = 0; rotM[3][3] = 1;
Merge(rotM);
}
public void Scale(double Xs, double Ys, double Zs)
{
double sM[][] = new double[4][4];
sM[0][0] = Xs; sM[0][1] = 0; sM[0][2] = 0; sM[0][3] = 0;
sM[1][0] = 0; sM[1][1] = Ys; sM[1][2] = 0; sM[1][3] = 0;
sM[2][0] = 0; sM[2][1] = 0; sM[2][2] = Zs; sM[2][3] = 0;
sM[3][0] = 0; sM[3][1] = 0; sM[3][2] = 0; sM[3][3] = 1;
Merge(sM);
}
public void Translate(double Xt, double Yt, double Zt)
{
double tM[][] = new double[4][4];
tM[0][0] = 1; tM[0][1] = 0; tM[0][2] = 0; tM[0][3] = 0;
tM[1][0] = 0; tM[1][1] = 1; tM[1][2] = 0; tM[1][3] = 0;
tM[2][0] = 0; tM[2][1] = 0; tM[2][2] = 1; tM[2][3] = 0;
tM[3][0] = Xt; tM[3][1] = Yt; tM[3][2] = Zt; tM[3][3] = 1;
Merge(tM);
}
public void Shear(double Xs, double Ys)
{
double sM[][] = new double[4][4];
sM[0][0] = 1; sM[0][1] = 0; sM[0][2] = Xs; sM[0][3] = 0;
sM[1][0] = 0; sM[1][1] = 1; sM[1][2] = Ys; sM[1][3] = 0;
sM[2][0] = 0; sM[2][1] = 0; sM[2][2] = 1; sM[2][3] = 0;
sM[3][0] = 0; sM[3][1] = 0; sM[3][2] = 0; sM[3][3] = 1;
Merge(sM);
}
public void Transform(Vector3D v)
{
double x = v.x;
double y = v.y;
double z = v.z;
v.xt = ((x * Matrix[0][0]) + (y * Matrix[1][0]) + (z * Matrix[2][0])) + Matrix[3][0];
v.yt = ((x * Matrix[0][1]) + (y * Matrix[1][1]) + (z * Matrix[2][1])) + Matrix[3][1];
v.zt = ((x * Matrix[0][2]) + (y * Matrix[1][2]) + (z * Matrix[2][2])) + Matrix[3][2];
}
public void Transform2(Vector3D v)
{
double x = v.xt;
double y = v.yt;
double z = v.zt;
v.xt = ((x * Matrix[0][0]) + (y * Matrix[1][0]) + (z * Matrix[2][0])) + Matrix[3][0];
v.yt = ((x * Matrix[0][1]) + (y * Matrix[1][1]) + (z * Matrix[2][1])) + Matrix[3][1];
v.zt = ((x * Matrix[0][2]) + (y * Matrix[1][2]) + (z * Matrix[2][2])) + Matrix[3][2];
}
public double[][] GetMatrix()
{
return Matrix;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -