|
2m
楼主 |
发表于 2010-11-14 09:47:14
|
只看该作者
本帖最后由 panhao1 于 2010-11-14 09:48 编辑
再一次强调 更好用的代码在我那个 processing犀牛化的类 的帖子上
下面是矩阵的
关于4*4矩阵平移 旋转 缩放的东西
class Matrix16
{
public double[] grid = new double[16];
public Matrix16()
{
grid = new double[16]{
1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,0};
}
public Matrix16(double[] f)
{
grid = f;
}
public Point3d applyTransformationToPoint(Point3d p)
{
double x, y, z;
//multiply 1x3 matrix with 3x3 matrix
x = p.X * get(0, 0) + p.Y * get(1, 0) + p.Z * get(2, 0);
y = p.X * get(0, 1) + p.Y * get(1, 1) + p.Z * get(2, 1);
z = p.X * get(0, 2) + p.Y * get(1, 2) + p.Z * get(2, 2);
//translate x, y, z
x += get(3, 0);
y += get(3, 1);
z += get(3, 2);
return new Point3d(x, y, z);
}
public void PrintConsole()
{
Console.WriteLine(">>");
Console.WriteLine(get(0, 0).ToString() + "/t," + get(1, 0).ToString() + "/t," + get(2, 0).ToString() + "/t," + get(3, 0).ToString());
Console.WriteLine(get(0, 1).ToString() + "/t," + get(1, 1).ToString() + "/t," + get(2, 1).ToString() + "/t," + get(3, 1).ToString());
Console.WriteLine(get(0, 2).ToString() + "/t," + get(1, 2).ToString() + "/t," + get(2, 2).ToString() + "/t," + get(3, 2).ToString());
Console.WriteLine(get(0, 3).ToString() + "/t," + get(1, 3).ToString() + "/t," + get(2, 3).ToString() + "/t," + get(3, 3).ToString());
}
public static Matrix16 makeMatrixScale(double s)
{
return new Matrix16(new double[16]{
s,0,0,0,
0,s,0,0,
0,0,s,0,
0,0,0,1
});
}
public static Matrix16 makeMatrixRotAroundVector(Point3d vector, double degree)
{
double d = degree * ((System.Math.PI * 2) / 360.0);
Point3d unitAxis = vector.getUnitVector();
double x = unitAxis.X;
double y = unitAxis.Y;
double z = unitAxis.Z;
double xp = Math.Pow((double)x, 2) - 1;
double yp = Math.Pow((double)y, 2) - 1;
double zp = Math.Pow((double)z, 2) - 1;
double c = Math.Cos(d);
double s = Math.Sin(d);
return new Matrix16(new double[16]{
1+((1-c)*xp), ((1-c)*x*y)-(z*s), (1-c)*x*z+y*s, 0,
(1-c)*x*y+z*s, 1+(1-c)*yp, (1-c)*y*z-x*s,0,
(1-c)*x*z-y*s, (1-c)*y*z+x*s, 1+(1-c)*zp,0,
0,0,0,1});
}
public static Matrix16 makeMatrixOriginatePoint(Point3d point)
{
double dx = -point.X;
double dy = -point.Y;
double dz = -point.Z;
return new Matrix16(new double[16] {
1,0,0,dx,
0,1,0,dy,
0,0,1,dz,
0,0,0,1
});
}
public static Matrix16 makeMatrixTranslation(double dx, double dy, double dz)
{
return new Matrix16(new double[16]{
1,0,0,dx,
0,1,0,dy,
0,0,1,dz,
0,0,0,1
});
}
// the the double value at x,y postion of the matrix 0-3
public double get(int x, int y)
{
return grid[(y * 4) + x];
}
public void set(int x, int y, double value)
{
grid[(y * 4) + x] = value;
}
} |
|