标题:
java程序
[打印本页]
作者:
sjj
时间:
2004-5-18 11:47
标题:
java程序
/数学计算生成动画:pyramid.java
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.lang.Math;
public class pyramid extends Applet implements Runnable {
double[] x = {1.0, 1.0, -1.0, -1.0};
double[] y = {1.0, -1.0, 1.0, -1.0};
double[] z = {1.0, -1.0, -1.0, 1.0};
Thread aniThread;
private void rotateX(double sita) {
double sinx = Math.sin(sita);
double cosx = Math.cos(sita);
for (int i = 0; i < 4; i++) {
double newy = y
* cosx - z
* sinx;
double newz = y
* sinx + z
* cosx;
y
= newy;
z
= newz;
}
}
private void rotateY(double sita) {
double siny = Math.sin(sita);
double cosy = Math.cos(sita);
for (int i = 0; i < 4; i++) {
double newz = z
* cosy - x
* siny;
double newx = z
* siny + x
* cosy;
z
= newz;
x
= newx;
}
}
private void rotateZ(double sita) {
double sinz = Math.sin(sita);
double cosz = Math.cos(sita);
for (int i = 0; i < 4; i++) {
double newx = x
* cosz - y
* sinz;
double newy = x
* sinz + y
* cosz;
x
= newx;
y
= newy;
}
}
public void init() {
aniThread = new Thread(this);
aniThread.start();
}
public void run() {
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while (Thread.currentThread() == aniThread) {
switch ((int) (Math.random() * 3)) {
case 0:
rotateX(Math.PI / 36);
break;
case 1:
rotateY(Math.PI / 36);
break;
case 2:
rotateZ(Math.PI / 36);
break;
}
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
}
}
public void paint(Graphics g) {
int[][] tri = {{0, 1, 2}, {0, 2, 3}, {0, 3, 1}, {3, 2, 1}};
Color clFace = Color.red;
double factor;
for (int i = 0; i < 4; i++) {
double x1 = x[tri
[1]] - x[tri
[0]];
double x2 = x[tri
[2]] - x[tri
[0]];
double y1 = y[tri
[1]] - y[tri
[0]];
double y2 = y[tri
[2]] - y[tri
[0]];
double z1 = z[tri
[1]] - z[tri
[0]];
double z2 = z[tri
[2]] - z[tri
[0]];
if (x1 * y2 > x2 * y1) {
int[] tx = new int[3];
int[] ty = new int[3];
factor = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)) /
Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) +
(z2 - z1) * (z2 - z1));
for (int j = 0; i < 3; j++) {
tx[j] = (int) (x[tri
[j]] * 50) + 100;
ty[j] = (int) (y[tri
[j]] * 50) + 100;
}
switch (i) {
case 0:
clFace = new Color((int) (255 * factor), 0, 0);
break;
case 1:
clFace = new Color(0, (int) (255 * factor), 0);
break;
case 2:
clFace = new Color(0, 0, (int) (255 * factor));
break;
case 3:
clFace = new Color((int) (255 * factor), (int) (255 * factor), 0);
break;
}
g.setColor(clFace);
g.fillPolygon(tx, ty, 3);
}
}
欢迎光临 黑色海岸线论坛 (http://bbs.thysea.com/)
Powered by Discuz! 7.2