Как сделать шар (сферу)?

 
 
 
Сообщения:1
Добрый день,
создал цилиндр, по этому же типу нужно создать шар, никак не соображу даже с чего начать...
Если кто знает, помогите плиз
Вот код цилиндра:

public class Cylinder extends Mesh implements ICylinder {

    protected float height = 4.0f;
    protected float radius = 2.0f;
    protected int segments = 16;

    /** definition of the color red */
    private static final Color3f RED = new Color3f(1.0f, 0.0f, 0.0f);

    /** definition of the color yellow */
    private static final Color3f YELLOW = new Color3f(1.0f, 1.0f, 0.0f);

    /** definition of the color green */
    private static final Color3f GREEN = new Color3f(0.0f, 1.0f, 0.0f);

    public Cylinder(String name) {
    	super(name);
        update();
    }

    public Cylinder(String name, float height, float radius, int segments) {
    	super(name);
        this.height = height;
        this.radius = radius;
        this.segments = segments;
        update();
    }

    @Override
    public void update() {
        super.clear();

        float lheight = this.height / 2;
        float lradius = this.radius ;
        int lsegments = this.segments;

        float centerX = 0;
        float centerY = 0;
        float centerZ = 0;

        for (int i = 0; i < segments; i++) {
          // Die Unterseite
          addFace(new Face(
              new Point3f[] {new Point3f((float)(centerX + lradius * Math.cos(Math.toRadians(i * 360/lsegments))),            centerY + lheight,      (float)(centerZ + lradius * Math.sin(Math.toRadians(i * 360/lsegments)))),
                             new Point3f(centerX,                                                                             centerY + lheight,       centerZ),
                             new Point3f((float)(centerX + lradius * Math.cos(Math.toRadians((i+1) * 360/lsegments))),        centerY + lheight,      (float)(centerZ + lradius * Math.sin(Math.toRadians((i+1) * 360/lsegments))))},
              new Color3f[] {RED,
                             YELLOW,
                             GREEN},
              new Vector3f[] {new Vector3f(0, 1, 0),
                              new Vector3f(0, 1, 0),
                              new Vector3f(0, 1, 0)}
          ));

          // Die Obersiete
          addFace(new Face(
              new Point3f[] {new Point3f((float)(centerX + lradius * Math.cos(Math.toRadians((i+1) * 360/lsegments))),        centerY - lheight,     (float)(centerZ + lradius * Math.sin(Math.toRadians((i+1) * 360/lsegments)))),
                             new Point3f(centerX,                                                                             centerY - lheight,      centerZ),
                             new Point3f((float)(centerX + lradius * Math.cos(Math.toRadians((i) * 360/lsegments))),          centerY - lheight,     (float)(centerZ + lradius * Math.sin(Math.toRadians((i) * 360/lsegments))))},
              new Color3f[] {RED,
                             YELLOW,
                             GREEN},
              new Vector3f[] {new Vector3f(0, -1, 0),
                              new Vector3f(0, -1, 0),
                              new Vector3f(0, -1, 0)}
          ));


          addFace(new Face(
              new Point3f[] {new Point3f((float)(centerX + lradius * Math.cos(Math.toRadians((i+1) * 360/lsegments))),        centerY + lheight,     (float)(centerZ + lradius * Math.sin(Math.toRadians((i+1) * 360/lsegments)))),
                             new Point3f((float)(centerX + lradius * Math.cos(Math.toRadians((i+1) * 360/lsegments))),        centerY - lheight,     (float)(centerZ + lradius * Math.sin(Math.toRadians((i+1) * 360/lsegments)))),
                             new Point3f((float)(centerX +lradius * Math.cos(Math.toRadians(i * 360/lsegments))),             centerY + lheight,     (float)(centerZ + lradius * Math.sin(Math.toRadians(i * 360/lsegments))))},
              new Color3f[] {RED,
                             YELLOW,
                             GREEN},
              new Vector3f[] {new Vector3f(0, 1, 0),
                              new Vector3f(0, 1, 0),
                              new Vector3f(0, 1, 0)}
          ));

          addFace(new Face(
              new Point3f[] {new Point3f((float)(centerX + lradius * Math.cos(Math.toRadians((i) * 360/lsegments))),          centerY - lheight,     (float)(centerZ + lradius * Math.sin(Math.toRadians((i) * 360/lsegments)))),
                             new Point3f((float)(centerX + lradius * Math.cos(Math.toRadians((i) * 360/lsegments))),          centerY + lheight,     (float)(centerZ + lradius * Math.sin(Math.toRadians((i) * 360/lsegments)))),
                             new Point3f((float)(centerX + lradius * Math.cos(Math.toRadians((i+1) * 360/lsegments))),        centerY - lheight,     (float)(centerZ + lradius * Math.sin(Math.toRadians((i+1) * 360/lsegments))))},
              new Color3f[] {RED,
                             YELLOW,
                             GREEN},
              new Vector3f[] {new Vector3f(0, 1, 0),
                              new Vector3f(0, 1, 0),
                              new Vector3f(0, 1, 0)}
          ));
        }
    }

    /**
     * sets the height of this cylinder.
     * @param value  the height to be set.
     */
    @Override
    public void setHeight(float value) {
        this.height = value;
        update();
    }

    /**
     * sets the radius of this cylinder.
     * @param value  the radius to be set.
     */
    @Override
    public void setRadius(float value) {
        this.radius = value;
        update();
    }

    /**
     * sets the number of segments of this cylinder.
     * @param value  the number of segments to be set.
     */
    @Override
    public void setSegments(int value) {
        this.segments = value;
        update();
    }

    /**
     * gets the height of this cylinder.
     * @return  the height of this cylinder.
     */
    @Override
    public float getHeight() {
        return this.height;
    }

    /**
     * gets the radius of this cylinder.
     * @return  the radius of this cylinder.
     */
    @Override
    public float getRadius() {
        return this.radius;
    }

    /**
     * gets the number of segments of this cylinder.
     * @return  the number of segments of this cylinder.
     */
    @Override
    public int getSegments() {
        return this.segments;
    }
}
 
 
Сообщения:6
Вот посмотри тут:

http://www.java2s.com/Code/Java/3D/AredsphereusingtheSphereutilityclass.htm

http://www.java2s.com/Code/Java/3D/Illustrateshowtodisplayaballlitbyaredlight.htm
 
Модераторы:Нет
Сейчас эту тему просматривают:Нет