我正在尝试创建一个3d球体的动画,由该球体表面上的随机点组成。这是我的代码,我在其中创建500个随机极点,然后将这些极坐标转换为笛卡尔坐标,然后将X和Y坐标映射到屏幕。这就是我得到的......显然不对!试了一个小时看我代码中的任何错误。
谁能看到我哪里出错了?
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class Ball extends JFrame implements ActionListener{
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Ball();
}
});
}
ArrayList<Polarpoint> points = new ArrayList<>();
public Ball() {
setSize(800, 600);
setVisible(true);
// declare some polar coords..
for (int i=0; i<500; i++)
points.add(new Polarpoint(Math.random()*2-1,Math.random()*2-1,50));
Timer t = new Timer(50,this);
t.start();
}
@Override
public void paint(Graphics g) {
g.clearRect(0, 0, getWidth(), getHeight());
for (Polarpoint point: points) {
// convert to Cartesian
double x=point.length*Math.sin(point.xa)*Math.cos(point.ya);
double y=point.length*Math.sin(point.xa)*Math.sin(point.ya);
double z=point.length*Math.cos(point.xa);
// project to 2d..
int px=(int) (x+200);
int py=(int) (y+200);
g.fillRect(px, py, 1, 1);
}
}
@Override
public void actionPerformed(ActionEvent e) {
for (Polarpoint p:points) {
p.xa+=.03;
p.ya+=.02;
}
repaint();
}
private static class Polarpoint{
public Polarpoint(double xa,double ya, double length) {
this.xa=xa;
this.ya=ya;
this.length=length;
}
private double xa;
private double ya;
private double length;
}
}
答案 0 :(得分:0)
将for
循环更改为
for (int i=0; i<500; i++) {
points.add(new Polarpoint(Math.random()*2*Math.PI,
Math.random()*2*Math.PI,RADIUS ));
}
所以随机角度在0-2PI之间。
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class Ball extends JFrame implements ActionListener{
private static final double RADIUS = 50, MAX_ANGEL = 2*Math.PI;
private static final int W = 400, H = 300,
CENTER_X = 200, CENTER_Y = 200,
DELAY= 50;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Ball();
}
});
}
ArrayList<Polarpoint> points = new ArrayList<>();
public Ball() {
setSize(W, H);
setVisible(true);
// declare some polar coords..
for (int i=0; i<500; i++) {
points.add(
new Polarpoint(((Math.random()*MAX_ANGEL)),
(Math.random()*MAX_ANGEL),RADIUS));
}
Timer t = new Timer(DELAY,this);
t.start();
}
@Override
public void paint(Graphics g) {
g.clearRect(0, 0, getWidth(), getHeight());
for (Polarpoint point: points) {
// convert to Cartesian
double x=point.length*Math.sin(point.xa)*Math.cos(point.ya);
double y=point.length*Math.sin(point.xa)*Math.sin(point.ya);
double z=point.length*Math.cos(point.xa);
// project to 2d..
int px=(int) (x+CENTER_X);
int py=(int) (y+CENTER_Y);
g.fillRect(px, py, 1, 1);
}
}
@Override
public void actionPerformed(ActionEvent e) {
for (Polarpoint p:points) {
p.xa+=.03;
p.ya+=.02;
}
repaint();
}
private static class Polarpoint{
public Polarpoint(double xa,double ya, double length) {
this.xa=xa;
this.ya=ya;
this.length=length;
}
private double xa;
private double ya;
private double length;
}
}
答案 1 :(得分:0)
要获得完整的球表面,需要在-PI / 2和PI / 2之间的仰角(你称之为xa
)和方位角(你称之为ya
)在0和0之间2 * PI(或-PI和PI ...之间)。
所以你的随机点应该是:
new Polarpoint((Math.random()-0.5) * Math.PI,
Math.random() * 2 * Math.PI,
50);