用于3D旋转的PVector标题()

时间:2014-05-25 14:46:12

标签: java vector rotation coordinates processing

我想在PVector类中使用heading()函数,但我使用的是P3D并且我的PVector有x,y和z。如何重新编写此功能以使其适用于3D空间?我的目标是做一些事情:

      size(100, 100, P3D);
      PVector v = new PVector(.2, .11, .54);
      rotate( v.heading() ); //set rotation from PVector named v
      line(0, 0, 10, 0); //draw line that will be rotated in correct direction

由于v.heading()仅适用于2D坐标空间,因此上述代码不会在3D空间中显示正确的旋转。如何使用rotateX(),rotateY(),rotateZ()实现此目的?我想尽可能避免使用四元数并使用旋转函数。

提前致谢!!

2 个答案:

答案 0 :(得分:4)

在2D中使用heading()函数将2D笛卡尔坐标(x,y)转换为2D极坐标(半径,角度)。极坐标的3D版本为spherical coordinates

spherical coordinates example

3D点P可以表示为三个线性坐标(x,y,z)或一个线性坐标和两个角度(rho,theta,phi)。 Rho是矢量的长度,θ是x-y平面中的角度,phi是进入z平面的角度的角度。这些等式描述了转换:

rho = sqrt(x^2 + y^2 + z^2)

phi = arccos(z/rho)

theta = arctan(y/x)

您应该能够使用这些角度使用rotateX()等功能。注意,这使用角度名称theta和phi的数学约定;在物理学中,这些标签与上面所示的相反。

答案 1 :(得分:0)

kevinsa5说的是什么,除了使用反正弦函数来获得高程。使用atan2函数作为方位角(或者更好,只需使用向量的2D标题方法)。并使用矢量的mag方法作为其大小。

PVector p = new PVector(v.mag(), 0, 0); // X

rotateY3D(p, phi); // Y

rotateZ3D(p, theta); // Z

向后工作,想想“X - Y - Z”并尝试:

function createSpreadsheetEditTrigger() {
  var ss = SpreadsheetApp.getActive();
  ScriptApp.newTrigger(status)
      .forSpreadsheet(ss)
      .onEdit()
      .create();    
}

function status() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  var cell = sheet.getActiveCell();  // Returns the active cell

  var currentColor = cell.getBackgroundColor();

  switch(true) {
    case currentColor === '#ffdc32':
      cell.setValue('IN PROGRESS');
      break;

    case currentColor === 'green':
      cell.setValue('COMPLETED');
      break;

    case currentColor === 'red':
      cell.setValue('ERRORS');
      break;

  }

}

然后将p与原始v。

进行比较