Java & JavaScript


Home
Applets
   3D:
 · Würfel
 · Würfel 2
 · Würfel 3
 · Tetraeder
 · Tetraeder 2
 · Dodekaeder
 · Ikosaeder
 · Fußball
 · Kugel
 · Kugel 2
   Fraktale:
 · Apfelmännchen
 · Apfelmännchen 2
 · Apfelmännchen 3
 · Apfelmännchen MA
 · Apfelmännchen Zoom
 · Apfelmännchen Zoom 2
 · Juliamenge
 · Juliamenge MA
 · Julia-Generator
 · Koch-Kurve
 · Koch-Kurve 2
 · Hilbert-Kurve
 · Sierpinski-Dreieck
 · Sierpinski-Dreieck 2
 · Sierpinski-Dreieck 3
 · Sierpinski-Teppich
 · Pythagoras-Baum
 · Lindenmayer-System
 · Lindenmayer-System 2
   Mathematik:
 · Funktionsplotter
 · Eratosthenes-Sieb
 · Miller-Rabin-Test
   Verschiedenes:
 · Morsezeichen-Ticker
 · Analoguhr
Scripts
Gäste
Kontakt



Zeppenfeld/Wolters: Lehrbuch der Grafikprogrammierung
- Anzeige -


- Applets : 3D : Würfel 2 -


Ein rotierender Würfel als Java-Applet.

[Der Würfel in Rotation als Java-Applet lässt sich allerdings nur mit aktiviertem Java betrachten !]


Wuerfel2.java

import java.awt.*;
import java.applet.*;

public class Wuerfel2 extends Applet {

    // 8 Eckpunkte 1-8
    // mit je 3 Koordinaten 1,2,3
    double p[][] = new double[9][4];

    int x=1, y=2, z=3;

    public void init() {
        setBackground(new Color(255,255,255));

        // 8 Eckpunkte im lokalen Würfel-Koordinatensystem
        // Nullpunkt = Mittelpunkt
        p[1][x] = -100; p[1][y] = -100; p[1][z] = -100;
        p[2][x] = +100; p[2][y] = -100; p[2][z] = -100;
        p[3][x] = +100; p[3][y] = -100; p[3][z] = +100;
        p[4][x] = -100; p[4][y] = -100; p[4][z] = +100;
        p[5][x] = -100; p[5][y] = +100; p[5][z] = -100;
        p[6][x] = +100; p[6][y] = +100; p[6][z] = -100;
        p[7][x] = +100; p[7][y] = +100; p[7][z] = +100;
        p[8][x] = -100; p[8][y] = +100; p[8][z] = +100;

        //       8 - - - - - 7
        //     / |         / |
        //    5 - - - - - 6  |
        //    |  |        |  |
        //    |  4 - - - -|- 3
        //    | /         | /
        //    1 - - - - - 2

        // y-Werte spiegeln
        for (int i=1;i<9;i++) {
            p[i][y] = -p[i][y];
        }
    }

    // Rotationswinkel in rad
    double angle_x = 0.01;
    double angle_y = 0.0075;
    double angle_z = 0.005;

    Image buffer;
    Graphics2D gBuffer;

    double c[] = new double[9];

    int w = 200; // -> Weltkoordinaten

    public void paint(Graphics g) {

        // Double-Buffering
        if (buffer==null) {
            buffer=createImage(this.getSize().width, this.getSize().height);
            gBuffer=(Graphics2D)buffer.getGraphics();
        }
        gBuffer.clearRect(0,0,this.getSize().width, this.getSize().height);

        // Antialiasing
        gBuffer.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);

        // Perspektive: *1+z/1000
        for (int i=1;i<9;i++) {
            c[i] = 1+p[i][z]/1000;
        }

        // Kreuzprodukt der eine Fläche aufspannenden Vektoren bilden
        // Wenn Betrag der z-Koordinate positiv: Fläche anzeigen

        if((p[1][x]*c[1]-p[2][x]*c[2])*(p[3][y]*c[3]-p[2][y]*c[2])
          -(p[1][y]*c[1]-p[2][y]*c[2])*(p[3][x]*c[3]-p[2][x]*c[2]) > 0) {
            // |2->1 x 2->3| > 0

            int xCoords1234[] = {(int)(p[1][x]*c[1])+w,(int)(p[2][x]*c[2])+w,
                                 (int)(p[3][x]*c[3])+w,(int)(p[4][x]*c[4])+w};
            int yCoords1234[] = {(int)(p[1][y]*c[1])+w,(int)(p[2][y]*c[2])+w,
                                 (int)(p[3][y]*c[3])+w,(int)(p[4][y]*c[4])+w};

            gBuffer.setColor(new Color(255,0,0));
            gBuffer.fillPolygon(new Polygon(xCoords1234,yCoords1234,4));
        }
        else if((p[7][x]*c[7]-p[6][x]*c[6])*(p[5][y]*c[5]-p[6][y]*c[6])
               -(p[7][y]*c[7]-p[6][y]*c[6])*(p[5][x]*c[5]-p[6][x]*c[6]) > 0) {
            // |6->7 x 6->5| > 0

            int xCoords5678[] = {(int)(p[5][x]*c[5])+w,(int)(p[6][x]*c[6])+w,
                                 (int)(p[7][x]*c[7])+w,(int)(p[8][x]*c[8])+w};
            int yCoords5678[] = {(int)(p[5][y]*c[5])+w,(int)(p[6][y]*c[6])+w,
                                 (int)(p[7][y]*c[7])+w,(int)(p[8][y]*c[8])+w};

            gBuffer.setColor(new Color(255,0,0));
            gBuffer.fillPolygon(new Polygon(xCoords5678,yCoords5678,4));
        }

        if((p[6][x]*c[6]-p[2][x]*c[2])*(p[1][y]*c[1]-p[2][y]*c[2])
          -(p[6][y]*c[6]-p[2][y]*c[2])*(p[1][x]*c[1]-p[2][x]*c[2]) > 0) {
            // |2->6 x 2->1| > 0

            int xCoords1265[] = {(int)(p[1][x]*c[1])+w,(int)(p[2][x]*c[2])+w,
                                 (int)(p[6][x]*c[6])+w,(int)(p[5][x]*c[5])+w};
            int yCoords1265[] = {(int)(p[1][y]*c[1])+w,(int)(p[2][y]*c[2])+w,
                                 (int)(p[6][y]*c[6])+w,(int)(p[5][y]*c[5])+w};

            gBuffer.setColor(new Color(0,255,0));
            gBuffer.fillPolygon(new Polygon(xCoords1265,yCoords1265,4));
        }
        else if((p[4][x]*c[4]-p[3][x]*c[3])*(p[7][y]*c[7]-p[3][y]*c[3])
               -(p[4][y]*c[4]-p[3][y]*c[3])*(p[7][x]*c[7]-p[3][x]*c[3]) > 0) {
            // |3->4 x 3->7| > 0

            int xCoords4378[] = {(int)(p[4][x]*c[4])+w,(int)(p[3][x]*c[3])+w,
                                 (int)(p[7][x]*c[7])+w,(int)(p[8][x]*c[8])+w};
            int yCoords4378[] = {(int)(p[4][y]*c[4])+w,(int)(p[3][y]*c[3])+w,
                                 (int)(p[7][y]*c[7])+w,(int)(p[8][y]*c[8])+w};

            gBuffer.setColor(new Color(0,255,0));
            gBuffer.fillPolygon(new Polygon(xCoords4378,yCoords4378,4));
        }

        if((p[3][x]*c[3]-p[2][x]*c[2])*(p[6][y]*c[6]-p[2][y]*c[2])-(p[3][y]*c[3]
           -p[2][y]*c[2])*(p[6][x]*c[6]-p[2][x]*c[2]) > 0) {
            // |2->3 x 2->6| > 0

            int xCoords2376[] = {(int)(p[2][x]*c[2])+w,(int)(p[3][x]*c[3])+w,
                                 (int)(p[7][x]*c[7])+w,(int)(p[6][x]*c[6])+w};
            int yCoords2376[] = {(int)(p[2][y]*c[2])+w,(int)(p[3][y]*c[3])+w,
                                 (int)(p[7][y]*c[7])+w,(int)(p[6][y]*c[6])+w};

            gBuffer.setColor(new Color(0,0,255));
            gBuffer.fillPolygon(new Polygon(xCoords2376,yCoords2376,4));
        }
        else if((p[5][x]*c[5]-p[1][x]*c[1])*(p[4][y]*c[4]-p[1][y]*c[1])
               -(p[5][y]*c[5]-p[1][y]*c[1])*(p[4][x]*c[4]-p[1][x]*c[1]) > 0) {
            // |1->5 x 1->4| > 0

            int xCoords1485[] = {(int)(p[1][x]*c[1])+w,(int)(p[4][x]*c[4])+w,
                                 (int)(p[8][x]*c[8])+w,(int)(p[5][x]*c[5])+w};
            int yCoords1485[] = {(int)(p[1][y]*c[1])+w,(int)(p[4][y]*c[4])+w,
                                 (int)(p[8][y]*c[8])+w,(int)(p[5][y]*c[5])+w};

            gBuffer.setColor(new Color(0,0,255));
            gBuffer.fillPolygon(new Polygon(xCoords1485,yCoords1485,4));
        }

        g.drawImage (buffer, 0, 0, this);

        // Verzögerung
        try {Thread.sleep(20);}
        catch (InterruptedException e) {}

        double px, py, pz;

        for (int i=1;i<9;i++) {

            px = p[i][x];
            py = p[i][y];
            pz = p[i][z];

            // Rotation um x-Achse
            p[i][y] = py*Math.cos(angle_x)-pz*Math.sin(angle_x);
            p[i][z] = py*Math.sin(angle_x)+pz*Math.cos(angle_x);

            py = p[i][y];
            pz = p[i][z];

            // Rotation um y-Achse
            p[i][x] = px*Math.cos(angle_y)+pz*Math.sin(angle_y);
            p[i][z] =-px*Math.sin(angle_y)+pz*Math.cos(angle_y);

            px = p[i][x];

            // Rotation um z-Achse
            p[i][x] = px*Math.cos(angle_z)-py*Math.sin(angle_z);
            p[i][y] = py*Math.cos(angle_z)+px*Math.sin(angle_z);
        }

        repaint();
    }

    public void update(Graphics g) {paint(g);}
}


Schema der Berechnung des Kreuzprodukts der Fläche. Dabei sind p und q die eine Fläche aufspannenden Vektoren.

[ p1 ]   [ q1 ]   [ p2*q3 - p3*q2 ]
[ p2 ] x [ q2 ] = [ p3*q1 - p1*q3 ]
[ p3 ]   [ q3 ]   [ p1*q2 - p2*q1 ]


Download  Wuerfel_2.zip (Applet und Code, ca. 4 kb)




© 2001-2004 Albert Kluge - Alle Rechte vorbehalten
Impressum | Datenschutz | Nutzung | eMail