# Thread: Help with sphere.java - not coming out correctly

1. Member Join Date
Feb 2008
Posts
13
Rep Power
0

## 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 int numberOfFacets = 12;
private int bands = 12;

public Sphere(){
generatePoints();

}
}
}

public void generatePoints(){

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
}

public void addThePoints(Point3D a,Point3D b,Point3D c,Point3D d,Point3D e,Point3D f){
Polygon3D Facet = new Polygon3D();

Facet.setColour(new Color((float)Math.random(),(float)Math.random(),(float)Math.random()));

Polygon3D Facet2 = new Polygon3D();
}

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.  Reply With Quote

2. Senior Member Join Date
Dec 2008
Location
Hong Kong
Posts
473
Rep Power
12

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

3. Member Join Date
Feb 2008
Posts
13
Rep Power
0

## What do you mean?  Reply With Quote

4. Member Join Date
Feb 2008
Posts
13
Rep Power
0

## Any ideas? Anyone?  Reply With Quote

#### Posting Permissions

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