Я пытаюсь визуализировать 3D-объект (куб) в 2D с использованием матриц вращения и проекции в обработке (Java) - PullRequest
0 голосов
/ 10 апреля 2020

Я новичок здесь, в сообществе StackOverflow, и я впервые использую язык обработки (java). У меня ошибка: "type mismatch" type float [] [] " введите описание изображения здесь не совпадает с "processinging.core.PVector" ", чтобы сказать вам, ребята, правду, что я смотрел видео на YouTube https://www.youtube.com/watch?v=p4Iz0XJY-Qk&t=144s Я сделал то же самое, что и этот парень, но у него не было никаких ошибок, но я могу кто-нибудь объяснить, что я сделал не так, пожалуйста, мне нужны некоторые детали или код, спасибо, мне действительно нужно завершить sh этот проект. Спасибо:)

    //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 голосов
/ 10 апреля 2020

Проверьте полную реализацию здесь: 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));
}
...