gpt4 book ai didi

java - 3D 坐标旋转缺乏透视 2D 精度

转载 作者:行者123 更新时间:2023-12-02 07:02:47 25 4
gpt4 key购买 nike

所以我编写了一个程序来绘制和显示 3D 立方体,使用等 Axis 测图中使用的这些简单的转换公式:

x2 = x*cos(30) - y*cos(30)
y2 = x*sin(30) + y*sin(30) + z

坐标转换很好,一切都清晰可见。

问题是旋转,大角度旋转会弄乱所有坐标并给我一个完整的形状。并且多次以小角度旋转(即 1000 次 1 度旋转或更多)会减小立方体的大小。

public void rotateX(double dg) //cube is shrinking along y and z  
{
y = (y*Math.cos(dg)-z*Math.sin(dg));
z = (y*Math.sin(dg)+z*Math.cos(dg));
}
public void rotateY(double dg) //cube is shrinking along x and z
{
x = x*Math.cos(dg)-z*Math.sin(dg);
z = x*Math.sin(dg)+z*Math.cos(dg);
}
public void rotateZ(double dg) //cube is shrinking along x and y
{
x = x*Math.cos(dg)-y*Math.sin(dg);
y = x*Math.sin(dg)+y*Math.cos(dg);
}

如何解决多次使用后 cos 和 sin 精度不足的问题?

这是用 3 个单独的类编写的完整代码:
主类:

import java.awt.*;
import javax.swing.*;
import java.util.Random;
public class Frame extends JFrame
{
private Random rnd = new Random();
private cubeGUI cube;
public Frame()
{
super();
}

public void paint(Graphics g)
{
cube = new cubeGUI(75,300.0,300.0);
cube.convertall();
double dg = 0.5; // The Smaller the degree, the less the error after long rotations.
int sl = 5;
int turns, axe;
while (1 == 1)
{
turns = rnd.nextInt(200)-100;
axe = rnd.nextInt(3);
for(int i = 0; i<turns; i++)
{
switch (axe)
{
case 0: cube.rotatx(dg); break;
case 1: cube.rotaty(dg); break;
case 2: cube.rotatz(dg); break;
}
g.clearRect(0,0,600,600);
g.drawLine(cube.a.x2,cube.a.y2,cube.b.x2,cube.b.y2);
g.drawLine(cube.a.x2,cube.a.y2,cube.c.x2,cube.c.y2);
g.drawLine(cube.c.x2,cube.c.y2,cube.d.x2,cube.d.y2);
g.drawLine(cube.b.x2,cube.b.y2,cube.d.x2,cube.d.y2);
g.drawLine(cube.e.x2,cube.e.y2,cube.f.x2,cube.f.y2);
g.drawLine(cube.e.x2,cube.e.y2,cube.g.x2,cube.g.y2);
g.drawLine(cube.g.x2,cube.g.y2,cube.h.x2,cube.h.y2);
g.drawLine(cube.f.x2,cube.f.y2,cube.h.x2,cube.h.y2);
g.drawLine(cube.a.x2,cube.a.y2,cube.e.x2,cube.e.y2);
g.drawLine(cube.b.x2,cube.b.y2,cube.f.x2,cube.f.y2);
g.drawLine(cube.c.x2,cube.c.y2,cube.g.x2,cube.g.y2);
g.drawLine(cube.d.x2,cube.d.y2,cube.h.x2,cube.h.y2);
try
{
Thread.sleep(sl); //Rotation Speed, In relation with Angle of rotation.
} catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
}
}
}
public static void main(String[] args)
{
Frame cube = new Frame();
cube.setSize(600,600);
cube.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cube.setVisible(true);
}
}

立方体类:

public class cubeGUI
{
public Point center,a,b,c,d,e,f,g,h;
private double x, y;
public cubeGUI(int m, double x, double y)
{
this.x = x;
this.y = y;
a = new Point(-m,-m,-m);
b = new Point(m,-m,-m);
c = new Point(-m,m,-m);
d = new Point(m,m,-m);
e = new Point(-m,-m,m);
f = new Point(m,-m,m);
g = new Point(-m,m,m);
h = new Point(m,m,m);
}
public void rotatx(double dg)
{
a.rotateX(Math.toRadians(dg));
b.rotateX(Math.toRadians(dg));
c.rotateX(Math.toRadians(dg));
d.rotateX(Math.toRadians(dg));
e.rotateX(Math.toRadians(dg));
f.rotateX(Math.toRadians(dg));
g.rotateX(Math.toRadians(dg));
h.rotateX(Math.toRadians(dg));
convertall();
}
public void rotaty(double dg)
{
a.rotateY(Math.toRadians(dg));
b.rotateY(Math.toRadians(dg));
c.rotateY(Math.toRadians(dg));
d.rotateY(Math.toRadians(dg));
e.rotateY(Math.toRadians(dg));
f.rotateY(Math.toRadians(dg));
g.rotateY(Math.toRadians(dg));
h.rotateY(Math.toRadians(dg));
convertall();
}
public void rotatz(double dg)
{
a.rotateZ(Math.toRadians(dg));
b.rotateZ(Math.toRadians(dg));
c.rotateZ(Math.toRadians(dg));
d.rotateZ(Math.toRadians(dg));
e.rotateZ(Math.toRadians(dg));
f.rotateZ(Math.toRadians(dg));
g.rotateZ(Math.toRadians(dg));
h.rotateZ(Math.toRadians(dg));
convertall();
}
public void convertall()
{
a.convert(x,y);
b.convert(x,y);
c.convert(x,y);
d.convert(x,y);
e.convert(x,y);
f.convert(x,y);
g.convert(x,y);
h.convert(x,y);
}
}

点类(计算所有坐标):

public class Point
{
private double x, y, z, F;
public int x2, y2;
public Point(double a, double b, double c)
{
x = a;
y = b;
z = c;
}
public int getX()
{
return (int)x;
}
public int getY()
{
return (int)y;
}
public int getZ()
{
return (int)z;
}
public void rotateX(double dg) //cube is shrinking along y and z
{
y = (y*Math.cos(dg)-z*Math.sin(dg));
z = (y*Math.sin(dg)+z*Math.cos(dg));
}
public void rotateY(double dg) //cube is shrinking along x and z
{
x = x*Math.cos(dg)-z*Math.sin(dg);
z = x*Math.sin(dg)+z*Math.cos(dg);
}
public void rotateZ(double dg) //cube is shrinking along x and y
{
x = x*Math.cos(dg)-y*Math.sin(dg);
y = x*Math.sin(dg)+y*Math.cos(dg);
}
public void convert(double xx, double yy)
{
x2 = (int)(-(Math.cos(Math.toRadians(30))*x - Math.cos(Math.toRadians(30))*y) + xx);
y2 = (int)(-(Math.sin(Math.toRadians(30))*x + Math.sin(Math.toRadians(30))*y + z) + yy);
}
public String toString()
{
return ("Y = " + y + ", Z = " + z);
}
}

最佳答案

通常的方法是将立方体表示为点配置和当前变换。旋转时,更新变换,但不更新点本身。仅当需要点坐标时(用于渲染、显示坐标值等),才应将变换应用于点。点本身不应被修改。

这将消除按顺序应用多次旋转时累积的错误。然而,重要的是变换矩阵应保持为旋转(行列式 1)。否则,转换仍会引入随机伪影(缩放、倾斜或其他扭曲)。因此,在应用每次旋转之后,应该对变换矩阵进行重新归一化,以使其保持纯变换。标准化可以像将每个条目除以行列式一样简单。

关于java - 3D 坐标旋转缺乏透视 2D 精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16446207/

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