|
- Applets : 3D : Tetraeder 2 -
Ein rotierender Tetraeder als Java-Applet.
Tetraeder2.java
import java.awt.*;
import java.applet.*;
public class Tetraeder2 extends Applet {
// 4 Eckpunkte 1-4
// mit je 3 Koordinaten 1,2,3
double p[][] = new double[5][4];
int x=1, y=2, z=3;
public void init() {
setBackground(new Color(255,255,255));
// Tetraeder-Höhe
double th = Math.sqrt(2/3.0)*300;
// Höhe des Tetraeder-Mittelpunkts
double tm = Math.sqrt(1/24.0)*300;
// Strecke Ecke-Mittelpunkt eines Dreiecks
double em = Math.sqrt(1/3.0)*300;
// Strecke Seite-Mittelpunkt eines Dreiecks
double sm = Math.sqrt(1/12.0)*300;
// 4 Eckpunkte im lokalen Tetraeder-Koordinatensystem
// Nullpunkt = Mittelpunkt
p[1][x] = -150; p[1][y] = -tm; p[1][z] = +sm;
p[2][x] = 0; p[2][y] = -tm; p[2][z] = -em;
p[3][x] = +150; p[3][y] = -tm; p[3][z] = +sm;
p[4][x] = 0; p[4][y] = +th-tm; p[4][z] = 0;
// 4
// / | \
// / | \
// / | \
// 1 - -|- - 3
// ` 2 ´
// y-Werte spiegeln
for (int i=1;i<5;i++) {
p[i][y] = -p[i][y];
}
}
// Rotationswinkel in rad
double angle_x = 0.01;
double angle_y = 0.007;
double angle_z = 0.001;
Image buffer;
Graphics2D gBuffer;
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);
// Kreuzprodukt der eine Fläche aufspannenden Vektoren bilden
// Wenn Betrag der z-Koordinate positiv: Fläche anzeigen
if((p[1][x]-p[2][x])*(p[3][y]-p[2][y])-(p[1][y]-p[2][y])*(p[3][x]-p[2][x]) > 0) {
// |2->1 x 2->3| > 0
int xCoords123[] = {(int)(p[1][x])+w,(int)(p[2][x])+w,(int)(p[3][x])+w};
int yCoords123[] = {(int)(p[1][y])+w,(int)(p[2][y])+w,(int)(p[3][y])+w};
gBuffer.setColor(new Color(255,0,0));
gBuffer.fillPolygon(new Polygon(xCoords123,yCoords123,3));
}
if((p[3][x]-p[2][x])*(p[4][y]-p[2][y])-(p[3][y]-p[2][y])*(p[4][x]-p[2][x]) > 0) {
// |2->3 x 2->4| > 0
int xCoords324[] = {(int)(p[3][x])+w,(int)(p[2][x])+w,(int)(p[4][x])+w};
int yCoords324[] = {(int)(p[3][y])+w,(int)(p[2][y])+w,(int)(p[4][y])+w};
gBuffer.setColor(new Color(0,255,0));
gBuffer.fillPolygon(new Polygon(xCoords324,yCoords324,3));
}
if((p[4][x]-p[2][x])*(p[1][y]-p[2][y])-(p[4][y]-p[2][y])*(p[1][x]-p[2][x]) > 0) {
// |2->4 x 2->1| > 0
int xCoords421[] = {(int)(p[4][x])+w,(int)(p[2][x])+w,(int)(p[1][x])+w};
int yCoords421[] = {(int)(p[4][y])+w,(int)(p[2][y])+w,(int)(p[1][y])+w};
gBuffer.setColor(new Color(0,0,255));
gBuffer.fillPolygon(new Polygon(xCoords421,yCoords421,3));
}
if((p[3][x]-p[4][x])*(p[1][y]-p[4][y])-(p[3][y]-p[4][y])*(p[1][x]-p[4][x]) > 0) {
// |4->3 x 4->1| > 0
int xCoords341[] = {(int)(p[3][x])+w,(int)(p[4][x])+w,(int)(p[1][x])+w};
int yCoords341[] = {(int)(p[3][y])+w,(int)(p[4][y])+w,(int)(p[1][y])+w};
gBuffer.setColor(new Color(255,255,0));
gBuffer.fillPolygon(new Polygon(xCoords341,yCoords341,3));
}
g.drawImage (buffer, 0, 0, this);
// Verzögerung
try {Thread.sleep(10);}
catch (InterruptedException e) {}
double px, py, pz;
for (int i=1;i<5;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);}
}
Download Tetraeder_2.zip (Applet und Code, ca. 3,5 kb)
© 2001-2004 Albert Kluge - Alle Rechte vorbehalten
Impressum | Datenschutz | Nutzung | eMail
|