Results 1 to 4 of 4
  1. #1
    JavaInLove is offline Member
    Join Date
    Feb 2008
    Posts
    13
    Rep Power
    0

    Question Help with sphere.java - not coming out correctly

    I can't see a Java3D section plus the stuff i'm working on is 2D related so i'll post here.

    I am writing a sphere.java class representing a Sphere not using the Java3D API and logically in theory it should work due to the maths involved. I am using polar coordinates to generate the points on the sphere and using the points to construct triangle polygons which make up the sphere. I was told this would be an easy way of doing it so i opt for this. However also noted that there are many alternatives..

    Here's the class below:

    Java Code:
    //all imports are here..
    .
    .
    
    public Sphere extends JPanel{
    
    private ArrayList<Polygon3D> polygons = new ArrayList<Polygon3D>();
    private Point3D Origin = new Point3D(0, 0, 0);
     private double radius = 5;
     private int numberOfFacets = 12;
     private int bands = 12;
    
    public Sphere(){
                  generatePoints();
    
    		
    		  }
             }
    }
    
    public void generatePoints(){
    double phiInc = (double)Math.toRadians(180/bands);
    		double thetaInc = (double)Math.toRadians(360/numberOfFacets);
    		
    		for(int i=-((bands/2)+1); i < ((bands/2)+1); i++) {
    			for(int j=0; j < numberOfFacets; j++) {
    				double theta = j * thetaInc +	(i*(thetaInc/2));
    				double phi = i * phiInc;
    		
    				Point3D a = polar2Rect(radius, theta, phi);
    		        Point3D b = polar2Rect(radius, theta +(thetaInc/2), phi + phiInc);
    		        Point3D c = polar2Rect(radius, theta + thetaInc,phi);
    		        Point3D d = polar2Rect(radius, theta +((3*thetaInc)/2), phi + phiInc);
    		        Point3D e = (Point3D)b.clone();
    		        Point3D f = (Point3D)c.clone(); //make a copy of these points
         	       addThePoints(a,b,c,d,e,f);
    }
    
    public void addThePoints(Point3D a,Point3D b,Point3D c,Point3D d,Point3D e,Point3D f){
                        Polygon3D Facet = new Polygon3D();
    		       
    		        Facet.addPoint(a.plus(Origin));
    		        Facet.addPoint(c.plus(Origin));
    		        Facet.addPoint(b.plus(Origin));
    		        Facet.setColour(new Color((float)Math.random(),(float)Math.random(),(float)Math.random()));
    
    		        polygons.add(Facet);
    
    		        Polygon3D Facet2 = new Polygon3D();
    		        Facet2.addPoint(e.plus(Origin));
    		        Facet2.addPoint(f.plus(Origin));
    		        Facet2.addPoint(d.plus(Origin));
    		        polygons.add(Facet2);
    }
    
    public Point3D polar2Rect(double dis, double ang, double phi){
    		Point3D p = new Point3D(0,0,0);
    		p.setX((float) (dis * Math.sin(ang) * Math.cos(phi))); 
    		p.setY((float) (dis * Math.sin(ang) * Math.cos(phi))); 
    		p.setZ((float) (dis * Math.cos(ang)));
    		return p;
    	}
    
    public void paint(Graphics g){
       Graphics2D g2 = (Graphics2D)g;
        for(Polygon3D pol : polygons){
             g2.fill(pol.getShape());  //get the generated polygon shape
        }
    }
    
    }
    
    private Point3D{
     //an inner class which represents the a point3D
    //not shown here because it's working perfectly fine 
    //works with other shapes i'm trying to create (cuboid, prism etc) so its not this problem
    }
    notes*
    Polygon3D is just a class which represents or a mechanism to show a polygon.
    Same with Point3D class - this class is fine

    So my problem is that when i paint the Sphere it draws a flat oval slanting sideways. But i when i rotate the thing a bit about 45 deg it shows one side of the sphere (i.e. a 2D circle) - so clearly it is drawing the shape somehow and abit of the sphere..

    My bet is on the maths - something not right or i've missed something out.. Any ideas?
    Try the piece of code.

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    Sphere is a Class?
    never complie
    Last edited by mtyoung; 03-16-2009 at 04:21 AM.

  3. #3
    JavaInLove is offline Member
    Join Date
    Feb 2008
    Posts
    13
    Rep Power
    0

    Default

    What do you mean?

  4. #4
    JavaInLove is offline Member
    Join Date
    Feb 2008
    Posts
    13
    Rep Power
    0

Similar Threads

  1. Please help output not coming
    By majin_harish in forum New To Java
    Replies: 2
    Last Post: 02-05-2009, 10:45 AM
  2. Why is the answer not coming out
    By anonymous18 in forum New To Java
    Replies: 4
    Last Post: 11-12-2008, 04:10 AM
  3. Please help. Trying to display a sphere.
    By aknbad23 in forum New To Java
    Replies: 6
    Last Post: 10-07-2008, 06:37 PM
  4. [SOLVED] \t not working correctly?
    By Gakusei in forum New To Java
    Replies: 5
    Last Post: 05-06-2008, 05:45 PM
  5. How Do I Embed Java Correctly To A Web Page
    By abcd in forum Java Applets
    Replies: 7
    Last Post: 01-28-2008, 08:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •