gpt4 book ai didi

java - 我尝试使用处理(Java)中的旋转和投影矩阵以 2D 形式渲染 3D 对象(立方体)

转载 作者:行者123 更新时间:2023-12-01 17:30:28 24 4
gpt4 key购买 nike

我是 StackOverflow 社区的新人,这是我第一次使用处理语言(java),我有一个错误:“类型不匹配”类型 float[][]”enter image description here 与“procesing.core”不匹配.PVector""告诉你们真相我在youtube上看过一个视频https://www.youtube.com/watch?v=p4Iz0XJY-Qk&t=144s我和这个人做了同样的事情,但他没有任何错误,但我可以有人解释我做错了什么吗?我需要一些细节或代码,谢谢,我真的需要完成这个项目,谢谢:)

    //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;
}

photos of my code

最佳答案

在此处查看完整实现:https://github.com/CodingTrain/website/blob/master/CodingChallenges/CC_112_3D_Rendering/Processing/CC_112_3D_Rendering/matrix.pde

请注意,matmul 方法已更新为返回 PVector。

PVector matmul(float[][] a, PVector b) {
float[][] m = vecToMatrix(b);
return matrixToVec(matmul(a,m));
}

关于java - 我尝试使用处理(Java)中的旋转和投影矩阵以 2D 形式渲染 3D 对象(立方体),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61131947/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com