2017-07-23 4 views
0

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

enter image description here

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; 
} 

} 
+0

Ahhh .... Je aurais dû demander «ce que je fais mal! –

+0

le voulez-vous en 3D? –

+0

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. –

Répondre

0

Modifier la boucle for à

for (int i=0; i<500; i++) { 
    points.add(new Polarpoint(Math.random()*2*Math.PI, 
           Math.random()*2*Math.PI,RADIUS)); 
} 

angles si aléatoires seront entre 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; 
    } 
} 
+0

il vous manque un crochet avant la seconde Math.random() –

+0

yup ... .thats première erreur! Merci. MAIS, il ne tourne toujours pas correctement .... –

0

Pour obtenir une surface de balle complète, vous avez besoin d'angles d'élévation (ce que vous appelez xa) entre -PI/2 et PI/2 et des angles d'azimut (ce que vous appelez ya) entre 0 et 2 * PI (ou entre -PI et PI ...).

Ainsi, vos points aléatoires devraient être:

new Polarpoint((Math.random()-0.5) * Math.PI, 
       Math.random() * 2 * Math.PI, 
       50);