# Help with sphere.java - not coming out correctly

• 03-16-2009, 03:07 AM
JavaInLove
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:

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.
• 03-16-2009, 04:11 AM
mtyoung
Sphere is a Class?
never complie
• 03-16-2009, 06:30 PM
JavaInLove
What do you mean?
• 03-17-2009, 09:38 PM
JavaInLove
Any ideas? Anyone?