英文:
Im trying to render a 3D object (cube) in 2D using rotation and projection matrices in Processing (Java)
问题
// 第一个选项卡
float[][] vecToMatrix(PVector v) {
float[][] m = new float[3][1];
m[0][0] = v.x;
m[1][0] = v.y;
m[2][0] = v.z;
return m;
}
PVector matrixToVec(float[][] m) {
PVector v = new PVector ();
v.x = m[0][0];
v.y = m[1][0];
if(m.length > 2) {
v.z = m[2][0];
}
return v;
}
void logMatrix(float[][] m){
int cols = m[0].length;
int rows = m.length;
println(rows + "x" + cols);
println("-------------------");
for(int i=0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
print(m[i][j] + " ");
}
println();
}
println();
}
float[][] matmul(float[][] a, PVector b) {
float[][] m = vecToMatrix(b);
return matmul(a, m);
}
float[][] matmul(float[][] a, float[][] b){
int colsA = a[0].length;
int rowsA = a.length;
int colsB = b[0].length;
int rowsB = b.length;
if (colsA != rowsB){
println("Colums of A must mach rows of B");
return null;
}
float result[][]= new float[rowsA][colsB];
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsB; j++){
float sum = 0;
for (int k= 0; k < colsA; k++) {
sum += a[i][k] * b[k][j];
}
result[i][j] = sum;
}
}
return result;
}
// 第二个选项卡
float angle = 0;
PVector[] points = new PVector[2];
float[][] projection = {
{1, 0, 0},
{0, 1, 0}
};
void setup() {
size(600, 400, P3D);
points[0] = new PVector(-50, -50, 0);
points[1] = new PVector(50, -50, 0);
points[3] = new PVector(50, 50, 0);
points[4] = new PVector(-50, 50, 0);
}
void draw() {
background(0);
translate(width/2, height/2);
stroke(255);
strokeWeight(16);
noFill();
float[][] rotationZ = {
{ cos(angle), -sin(angle), 0},
{ sin(angle), cos(angle), 0},
{ 0, 0, 1 }
};
float[][] rotationX = {
{ 1, 0, 0},
{ 0, cos(angle), cos(angle), 0},
{ 0, sin(angle), cos(angle), 0}
};
float[][] rotationY = {
{ cos(angle), 0, -sin(angle)},
{ 0, 1, 0},
{ sin(angle), 0, cos(angle)}
};
for (PVector v : points) {
PVector rotated = matmul(rotationX, v);
// 错误在此处 "类型不匹配:类型为 float[][],与“procesing.core.PVector”不匹配"
PVector projected2d = matmul(projection, rotated); // 错误在此处
// 错误在此处 "类型不匹配:类型为 float[][],与“procesing,core,PVector”不匹配"
point(projected2d.x, projected2d.y);
}
angle += 0.01;
}
英文:
Im new here in StackOverflow comunity and It is my first time using the processing languange (java) i have a error saying: "type mismatch "type float[][]" enter image description heredoes not match with "procesing.core.PVector""
to tell u guys the truth I have watched a video on youtube https://www.youtube.com/watch?v=p4Iz0XJY-Qk&t=144s I did the same thing as this guy but he didnt have any error but i do can someone axplain what have i done wrong please i need some details or code thank you i realy need to finish this project Thanks
//first tab
float[][] vecToMatrix(PVector v) {
float[][] m = new float[3][1];
m[0][0] = v.x;
m[1][0] = v.y;
m[2][0] = v.z;
return m;
}
PVector matrixToVec(float[][] m) {
PVector v = new PVector ();
v.x = m[0][0];
v.y = m[1][0];
if(m.length > 2) {
v.z = m[2][0];
}
return v;
}
void logMatrix(float[][] m){
int cols = m[0].length;
int rows = m.length;
println(rows + "x" + cols);
println("-------------------");
for(int i=0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
print(m[i][j] + " ");
}
println();
}
println();
}
float[][] matmul(float[][] a, PVector b) {
float[][] m = vecToMatrix(b);
return matmul(a,m);
}
float[][] matmul(float[][] a, float[][] b){
int colsA = a[0].length;
int rowsA = a.length;
int colsB = b[0].length;
int rowsB = b.length;
if (colsA != rowsB){
println("Colums of A must mach rows of B");
return null;
}
float result[][]= new float[rowsA][colsB];
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsB; j++){
float sum = 0;
for (int k= 0; k < colsA; k++) {
sum += a[i][k] * b[k][j];
}
result[i][j] = sum;
}
}
return result;
}
//seccond tab
float angle = 0;
PVector[] points = new PVector[2];
float[][] projection = {
{1, 0, 0},
{0, 1, 0}
};
void setup() {
size(600, 400, P3D);
points[0] = new PVector(-50, -50, 0);
points[1] = new PVector(50, -50, 0);
points[3] = new PVector(50, 50, 0);
points[4] = new PVector(-50, 50, 0);
}
void draw() {
background(0);
translate(width/2, height/2);
stroke(255);
strokeWeight(16);
noFill();
float[][] rotationZ = {
{ cos(angle), -sin(angle), 0},
{ sin(angle), cos(angle), 0},
{ 0, 0, 1 }
};
float[][] rotationX = {
{ 1, 0, 0},
{ 0, cos(angle), cos(angle), 0},
{ 0, sin(angle), cos(angle), 0}
};
float[][] rotationY = {
{ cos(angle), 0, -sin(angle)},
{ 0, 1, 0},
{ sin(angle), 0, cos(angle)}
};
for (PVector v : points) {
PVector rotated = matmul(rotationX, v);
//errorr is here "type mismatch "type float[][]" [enter image description here][5]does not match with "procesing.core.PVector""
PVector projected2d = matmul(projection, rotated); //errorr is here
//errorr is here "type mismatch "type float[][]" does not match with "procesing,core,PVector""[enter image description here][1]
point(projected2d.x, projected2d.y);
[}][3]
angle += 0.01;
}
答案1
得分: 0
Check the full implementation here: https://github.com/CodingTrain/website/blob/master/CodingChallenges/CC_112_3D_Rendering/Processing/CC_112_3D_Rendering/matrix.pde
Notice that the matmul
method has been updated to return PVector.
PVector matmul(float[][] a, PVector b) {
float[][] m = vecToMatrix(b);
return matrixToVec(matmul(a, m));
}
英文:
Check the full implementation here: https://github.com/CodingTrain/website/blob/master/CodingChallenges/CC_112_3D_Rendering/Processing/CC_112_3D_Rendering/matrix.pde
Notice that the matmul
method has been updated to return PVector.
PVector matmul(float[][] a, PVector b) {
float[][] m = vecToMatrix(b);
return matrixToVec(matmul(a,m));
}
专注分享java语言的经验与见解,让所有开发者获益!
评论