Im essayant de créer une animation d'une sphère 3D, faite à partir de points aléatoires sur la surface de cette sphère. C'est mon code, où je crée 500 points polaires aléatoires, puis convertis ces coordonnées polaires en cartes cartésiennes, puis mappez les coordonnées X et Y à l'écran. C'est ce que j'ai ... évidemment pas bien !! J'ai essayé pendant une heure pour voir les bugs dans mon code.3D balle avec des points aléatoires sur la surface
Quelqu'un peut voir où je me trompe?
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;
}
}
Ahhh .... Je aurais dû demander «ce que je fais mal! –
le voulez-vous en 3D? –
oui .... mais, n'utilisant aucune API 3d comme OpenGL ..., juste des bases de mappage d'un point 3d défini comme un coord polaire, vers une fenêtre 2D. –