【发布时间】:2020-08-28 08:36:23
【问题描述】:
我正在尝试在 Processing 中创建自定义 Matrix 类以处理任意数量的维度。类 RotationMatrix 派生自它。但我无法访问 RotationMatrix 的方法,就好像它们不存在一样。 我的矩阵类:
class Matrix{
float[][] list;
int[] dims;
public Matrix(int cols, int rows){
list = new float[cols][rows];
dims = new int[] {cols, rows};
}
void Identity(){
list = new float[dims[0]][dims[1]];
for(int v=0;v<min(dims[0], dims[1]); v++){
list[v][v] = 1;
}
}
float[] mult(float[] vector){
if(vector.length!=dims[0]) return vector;
float[] result = new float[dims[0]];
for (int i = 0; i<result.length;i++){
float d = dot(vector, list[i]);
result[i] = d;
}
return result;
}
float dot(float[] v1, float[] v2){
float result=0;
for(int i = 0; i<v1.length;i++){
result+=v1[i]*v2[i];
}
return result;
}
String toString(){
String result="{";
for(int y = 0; y<dims[1]; y++){
if(y>0)result+=" ";
result+="(";
for(int x=0; x<dims[0];x++){
result+=list[x][y];
if(x+1<dims[0]) result+=", ";
}
result+=")";
if(y+1<dims[1]) result+=",\n\n";
}
result+="}";
return result;
}
}
class RotationMatrix extends Matrix {
public float angle, scale;
int ax1, ax2;
int dim;
public RotationMatrix(int dim){
super(dim, dim);
ax1=0;
ax2=1;
this.dim=dim;
}
public RotationMatrix(int dim, int a1, int a2){
super(dim, dim);
ax1=min(a1, a2);
ax2=max(a1, a2);
this.dim=dim;
}
void Update(){
for(int y = 0; y<dim;y++){
for(int x = 0; x<dim;x++){
if(x==y) {
if(x==ax1||y==ax2) {
list[x][y] = cos(angle)*scale;
}
else {
list[x][y]=1;
}
}
else{
if(x==ax2&&y==ax1){
list[x][y]=-sin(angle)*scale;
}
if(x==ax1&&y==ax2) {
list[x][y] = sin(angle)*scale;
}
}
}
}
}
}
现在使用旋转矩阵:
m=new RotationMatrix(3);
m.Update();
并且此 Update 方法调用会引发错误“函数“Update()”不存在”。我究竟做错了什么?我对处理比较陌生。 编辑:将“更新”标记为公开并没有帮助,重新启动处理。
【问题讨论】:
-
m不应声明为Matrix(我猜是),但应声明为RotationMatrix。Matrix没有Update。 -
如果问题“已解决”,那么您应该接受解决问题的答案,不编辑您的问题以包含“已解决”。
标签: java processing