我需要有人帮助我。大约 10 年前,我编写了使 3d 字母 T 旋转的 Delphi 代码。最近我用java重写了那个程序,但我不知道数学是如何工作的。如果有人熟悉四元数旋转,请您解释一下程序中的数学是如何工作的。p 数组包含字母 T 每一侧的所有端点。其中一些相互重复只是为了可以在 for 循环中绘制字母 T。我只需要知道什么是常数'a',为什么我两次调用方法'qm',以及它的作用。
提前感谢您提供任何可能的帮助。
package quaternion;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class FourPoint{
public double x, y, z, w;
}
class RotationPanel extends JPanel{
private FourPoint[] p, pdraw;
private int i, j;
private FourPoint o,neo,m,h;
final double a = Math.sqrt(1/(0.25+1+1.0/9));
static int count= 0;
public RotationPanel(){
o = new FourPoint();
neo = new FourPoint();
m = new FourPoint();
h = new FourPoint();
p = new FourPoint[33];
pdraw = new FourPoint[33];
for (int i = 0; i < 33; i++){
p[i] = new FourPoint();
pdraw[i] = new FourPoint();
}
p[0].x = 0;
p[0].y = 0;
p[0].z = -40;
p[0].w = 0;
p[1].x = 0;
p[1].y = 0;
p[1].z = 0;
p[1].w = 0;
p[2].x = -100;
p[2].y = 0;
p[2].z = 0;
p[2].w = 0;
p[3].x = -100;
p[3].y = 0;
p[3].z = -40;
p[3].w = 0;
p[4].x = 0;
p[4].y = 0;
p[4].z = -40;
p[4].w = 0;
p[5].x = 0;
p[5].y = 20;
p[5].z = -40;
p[5].w = 0;
p[6].x = 0;
p[6].y = 20;
p[6].z = 0;
p[6].w = 0;
p[7].x = 0;
p[7].y = 0;
p[7].z = 0;
p[7].w = 0;
p[8].x = 0;
p[8].y = 20;
p[8].z = 0;
p[8].w = 0;
p[9].x = -40;
p[9].y = 20;
p[9].z = 0;
p[9].w = 0;
p[10].x = -40;
p[10].y = 20;
p[10].z = -40;
p[10].w = 0;
p[11].x = 0;
p[11].y = 20;
p[11].z = -40;
p[11].w = 0;
p[12].x = -40;
p[12].y = 20;
p[12].z = -40;
p[12].w = 0;
p[13].x = -40;
p[13].y = 120;
p[13].z = -40;
p[13].w = 0;
p[14].x = -60;
p[14].y = 120;
p[14].z = -40;
p[14].w = 0;
p[15].x = -60;
p[15].y = 120;
p[15].z = 0;
p[15].w = 0;
p[16].x = -40;
p[16].y = 120;
p[16].z = 0;
p[16].w = 0;
p[17].x = -40;
p[17].y = 120;
p[17].z = -40;
p[17].w = 0;
p[18].x = -40;
p[18].y = 120;
p[18].z = 0;
p[18].w = 0;
p[19].x = -40;
p[19].y = 20;
p[19].z = 0;
p[19].w = 0;
p[20].x = -40;
p[20].y = 120;
p[20].z = 0;
p[20].w = 0;
p[21].x = -60;
p[21].y = 120;
p[21].z = 0;
p[21].w = 0;
p[22].x = -60;
p[22].y = 20;
p[22].z = 0;
p[22].w = 0;
p[23].x = -100;
p[23].y = 20;
p[23].z = 0;
p[23].w = 0;
p[24].x = -100;
p[24].y = 0;
p[24].z = 0;
p[24].w = 0;
p[25].x = -100;
p[25].y = 20;
p[25].z = 0;
p[25].w = 0;
p[26].x = -100;
p[26].y = 20;
p[26].z = -40;
p[26].w = 0;
p[27].x = -100;
p[27].y = 0;
p[27].z = -40;
p[27].w = 0;
p[28].x = -100;
p[28].y = 20;
p[28].z = -40;
p[28].w = 0;
p[29].x = -60;
p[29].y = 20;
p[29].z = -40;
p[29].w = 0;
p[30].x = -60;
p[30].y = 120;
p[30].z = -40;
p[30].w = 0;
p[31].x = -60;
p[31].y = 20;
p[31].z = -40;
p[31].w = 0;
p[32].x = -60;
p[32].y = 20;
p[32].z = 0;
p[32].w = 0;
for(int i = 0; i < 32; i++){
pdraw[i].x = p[i].x;
pdraw[i].y = p[i].y;
pdraw[i].z = p[i].z;
pdraw[i].w = p[i].w;
}
}
private FourPoint qm(FourPoint q, FourPoint p){
FourPoint l = new FourPoint();
l.x = q.w*p.x-q.z*p.y+q.y*p.z+q.x*p.w;
l.y = q.z*p.x+q.w*p.y-q.x*p.z+q.y*p.w;
l.z =-q.y*p.x+q.x*p.y+q.w*p.z+q.z*p.w;
l.w =-q.x*p.x-q.y*p.y-q.z*p.z+q.w*p.w;
return l;
}
public void rotate(){
for(int j = 0; j <= 180; j++){
o.x =(a*0.5)*Math.sin(Math.PI*j/180);
neo.x =-o.x;
o.y =-(a)*Math.sin(Math.PI*j/180);
neo.y =-o.y;
o.z =(a/3)*Math.sin(Math.PI*j/180);
neo.z =-o.z;
o.w =Math.cos(Math.PI*j/180);
neo.w =o.w;
for(int i = 0; i < 33; i++){
m = qm(o, p[i]);
h = qm(m, neo);
pdraw[i].x = (int)Math.round(h.x) + 240;
pdraw[i].y = (int)Math.round(h.y) + 120;
}
//The problem is here
//When I'm trying to add delay, it does not
//repaint the canvas
//Timer timer = new Timer(10000, new TimerListener());
try{
Thread.currentThread().sleep(20);
}
catch(InterruptedException e){
}
Graphics g = this.getGraphics();
this.paintComponent(g);
//repaint();
}
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawLine(240,120, 180, 240);
for(int i = 0; i < 32; i++){
//System.out.println((count++) + " " + pdraw[i].x + " " + pdraw[i].y + " " + pdraw[i+1].x + " " + pdraw[i+1].y);
g.drawLine((int)pdraw[i].x, (int)pdraw[i].y, (int)pdraw[i+1].x, (int)pdraw[i+1].y);
}
}
}
class TimerListener implements ActionListener{
public void actionPerformed(ActionEvent e){
//repaint();
}
}
public class Quaternion {
private static RotationPanel rp;
public static void main(String[] args) {
JFrame jfMainFrame = new JFrame();
jfMainFrame.setSize(400, 400);
jfMainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfMainFrame.setLayout(new BorderLayout());
JButton jb = new JButton("Rotate");
jfMainFrame.add(jb, BorderLayout.SOUTH);
jfMainFrame.setLocationRelativeTo(null);
jfMainFrame.setVisible(true);
rp = new RotationPanel();
jfMainFrame.add(rp, BorderLayout.CENTER);
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
rp.rotate();
}
});
}
}
任何帮助表示赞赏。