Results 1 to 4 of 4
 03162009, 03:07 AM #1Member
 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 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 }
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.
 03162009, 04:11 AM #2Senior Member
 Join Date
 Dec 2008
 Location
 Hong Kong
 Posts
 473
 Rep Power
 9
Sphere is a Class?
never complieLast edited by mtyoung; 03162009 at 04:21 AM.
 03162009, 06:30 PM #3Member
 Join Date
 Feb 2008
 Posts
 13
 Rep Power
 0
What do you mean?
 03172009, 09:38 PM #4Member
 Join Date
 Feb 2008
 Posts
 13
 Rep Power
 0
Similar Threads

Please help output not coming
By majin_harish in forum New To JavaReplies: 2Last Post: 02052009, 10:45 AM 
Why is the answer not coming out
By anonymous18 in forum New To JavaReplies: 4Last Post: 11122008, 04:10 AM 
Please help. Trying to display a sphere.
By aknbad23 in forum New To JavaReplies: 6Last Post: 10072008, 05:37 PM 
[SOLVED] \t not working correctly?
By Gakusei in forum New To JavaReplies: 5Last Post: 05062008, 04:45 PM 
How Do I Embed Java Correctly To A Web Page
By abcd in forum Java AppletsReplies: 7Last Post: 01282008, 08:53 AM
Bookmarks