behroozifar_s
08-11-2007, 14:16
سلام
این یه برنامه هستش که نمی دونم به چه زبانیه ( فکر کنم جاوا ) اما می دونم خروجی هایپر کیوب می دهد .
اگه متخصص کامپوتر داریم و تونست بفهمه چی طوری کار می کنه و چه برنامه ای هست به منم خبر بده .
import java.applet.Applet;
import java.awt.*;
/*
**
** Program: Hypercube3.class
** Author: Jimmy Mohr
** Version: 0.1
**
** Description: Right now, draws a silly hypercube rotating in 3space.
** Soon it will be colored, shaded, have a shadow,
** accept user input, tell jokes, and get you a beer.
**
**
*/
public class Hypercube3 extends Applet implements Runnable {
final static double CUBESCALE = 50.0;
final static long FRAMEDELAY = 50;
final static int APPLETWIDTH = 350;
final static int APPLETHEIGHT = 400;
final static int NUMVERTEXPOINTS = 16;
final static int NUMBRIDGEPOINTS = 32;
final static int NUMLINES = 64;
final static double WARP4TO3 = 250.0;
final static double WARP3TO2 = 350.0;
final static double LINERADIUS = 4.0; //how thick the lines are
final static double VERTEXRADIUS = 7.0;
final static int CUBEOFFSETX = APPLETWIDTH >> 1;
final static int CUBEOFFSETY = 150;
final static int FLOORY = 150;
final static int FLOORSTARTWIDTH = 150;
final static int FLOORSTARTDEPTH = 60;
final static int FLOORSTARTY = 20;
final static int FLOORSTEPS = 5;
final static int FLOORCOLCHANGE = 5;
final static int FLOORCOLSTART = 30;
final static int COLRANGE = 150;
final static double COLEDGE = 1.7;
final static double[][] STARTVERTEXCOORDS = { { 1.0, 1.0, 1.0,-1.0},
{ 1.0,-1.0, 1.0,-1.0},
{-1.0,-1.0, 1.0,-1.0},
{-1.0, 1.0, 1.0,-1.0},
{ 1.0, 1.0, 1.0, 1.0},
{ 1.0,-1.0, 1.0, 1.0},
{-1.0,-1.0, 1.0, 1.0},
{-1.0, 1.0, 1.0, 1.0},
{ 1.0, 1.0,-1.0, 1.0},
{ 1.0,-1.0,-1.0, 1.0},
{-1.0,-1.0,-1.0, 1.0},
{-1.0, 1.0,-1.0, 1.0},
{ 1.0, 1.0,-1.0,-1.0},
{ 1.0,-1.0,-1.0,-1.0},
{-1.0,-1.0,-1.0,-1.0},
{-1.0, 1.0,-1.0,-1.0} };
final static double[][] STARTBRIDGECOORDS = { { 1.0, 0.0, 1.0,-1.0},
{ 0.0,-1.0, 1.0,-1.0},
{-1.0, 0.0, 1.0,-1.0},
{ 0.0, 1.0, 1.0,-1.0},
{ 1.0, 1.0, 1.0, 0.0},
{ 1.0,-1.0, 1.0, 0.0},
{-1.0,-1.0, 1.0, 0.0},
{-1.0, 1.0, 1.0, 0.0},
{ 1.0, 0.0, 1.0, 1.0},
{ 0.0,-1.0, 1.0, 1.0},
{-1.0, 0.0, 1.0, 1.0},
{ 0.0, 1.0, 1.0, 1.0},
{ 1.0, 1.0, 0.0, 1.0},
{ 1.0,-1.0, 0.0, 1.0},
{-1.0,-1.0, 0.0, 1.0},
{-1.0, 1.0, 0.0, 1.0},
{ 1.0, 1.0, 0.0,-1.0},
{ 1.0,-1.0, 0.0,-1.0},
{-1.0,-1.0, 0.0,-1.0},
{-1.0, 1.0, 0.0,-1.0},
{ 1.0, 0.0,-1.0, 1.0},
{ 0.0,-1.0,-1.0, 1.0},
{-1.0, 0.0,-1.0, 1.0},
{ 0.0, 1.0,-1.0, 1.0},
{ 1.0, 1.0,-1.0, 0.0},
{ 1.0,-1.0,-1.0, 0.0},
{-1.0,-1.0,-1.0, 0.0},
{-1.0, 1.0,-1.0, 0.0},
{ 1.0, 0.0,-1.0,-1.0},
{ 0.0,-1.0,-1.0,-1.0},
{-1.0, 0.0,-1.0,-1.0},
{ 0.0, 1.0,-1.0,-1.0} };
final static int[] STARTLINESV = {0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,
5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,
10,10,10,10,11,11,11,11,12,12,12,12,
13,13,13,13,14,14,14,14,15,15,15,15};
final static int[] STARTLINESB = {0,3,4,16,0,1,5,17,1,2,6,18,3,2,7,19,
4,8,11,12,5,8,9,13,6,9,10,14,7,10,11,15,
12,20,23,24,13,20,21,25,14,21,22,26,15,23,22,27,
16,24,28,31,17,25,28,29,18,26,29,30,19,27,30,31};
final static int X = 0;
final static int Y = 1;
final static int Z = 2;
final static int W = 3;
Image bufImage;
Graphics bufGraphics;
Thread holmes;
double[][] cubevertexcoords;
double[][] cubebridgecoords;
double[][] shadowbridgecoords;
double[][] shadowvertexcoords;
int[] linesb, linesv;
int[] linecount; //each element starts at 0, increments each time a line is drawn out of that vertex
double angle_wx, angle_wy, angle_wz, angle_xy, angle_xz, angle_yz;
double spin_wx, spin_wy, spin_wz, spin_xy, spin_xz, spin_yz;
long lastframe;
public void init() {
bufImage = createImage(APPLETWIDTH, APPLETHEIGHT);
bufGraphics = bufImage.getGraphics();
cubevertexcoords = new double[NUMVERTEXPOINTS][4];
cubebridgecoords = new double[NUMBRIDGEPOINTS][4];
shadowvertexcoords = new double[NUMVERTEXPOINTS][4];
shadowbridgecoords = new double[NUMBRIDGEPOINTS][4];
linesb = new int[NUMLINES];
linesv = new int[NUMLINES];
linecount = new int[NUMVERTEXPOINTS];
for (int i=0; i<NUMLINES; i++) {
linesv[i] = STARTLINESV[i];
linesb[i] = STARTLINESB[i];
}
} //init
public void start() {
if (holmes == null)
holmes = new Thread(this);
holmes.start();
} //start
public void gen_cube_points() {
double[] vertex, startvertex, bridge, startbridge;
for (int i=0; i<NUMVERTEXPOINTS; i++) {
vertex = cubevertexcoords[i];
startvertex = STARTVERTEXCOORDS[i];
vertex[0] = startvertex[0]*CUBESCALE;
vertex[1] = startvertex[1]*CUBESCALE;
vertex[2] = startvertex[2]*CUBESCALE;
vertex[3] = startvertex[3]*CUBESCALE;
} //for i
for (int i=0; i<NUMBRIDGEPOINTS; i++) {
bridge = cubebridgecoords[i];
startbridge = STARTBRIDGECOORDS[i];
bridge[0] = startbridge[0]*CUBESCALE;
bridge[1] = startbridge[1]*CUBESCALE;
bridge[2] = startbridge[2]*CUBESCALE;
bridge[3] = startbridge[3]*CUBESCALE;
}
} //gen_cube_points
public void rotate(double[][] structure, int a1, int a2, double angle) {
int size = structure.length;
double temp1, temp2;
double sinangle = Math.sin(angle);
double cosangle = Math.cos(angle);
for (int i=0; i<size; i++) {
temp1 = structure[i][a1];
temp2 = structure[i][a2];
structure[i][a1] = temp1*cosangle - temp2*sinangle;
structure[i][a2] = temp1*sinangle + temp2*cosangle;
}
} //rotate
public void rotate_cube() {
angle_wx += spin_wx;
angle_wy += spin_wy;
angle_wz += spin_wz;
angle_xy += spin_xy;
angle_xz += spin_xz;
angle_yz += spin_yz;
if (angle_wx != 0) {
rotate(cubevertexcoords, W, X, angle_wx);
rotate(cubebridgecoords, W, X, angle_wx);
}
if (angle_wy != 0) {
rotate(cubevertexcoords, W, Y, angle_wy);
rotate(cubebridgecoords, W, Y, angle_wy);
}
if (angle_wz != 0) {
rotate(cubevertexcoords, W, Z, angle_wz);
rotate(cubebridgecoords, W, Z, angle_wz);
}
if (angle_xy != 0) {
rotate(cubevertexcoords, X, Y, angle_xy);
rotate(cubebridgecoords, X, Y, angle_xy);
}
if (angle_xz != 0) {
rotate(cubevertexcoords, X, Z, angle_xz);
rotate(cubebridgecoords, X, Z, angle_xz);
}
if (angle_yz != 0) {
rotate(cubevertexcoords, Y, Z, angle_yz);
rotate(cubebridgecoords, Y, Z, angle_yz);
}
} //rotate_cube
public void four_to_three(double[][] structure) {
int size = structure.length;
double warp;
double[] thepoint;
for (int i=0; i<size; i++) {
thepoint = structure[i];
warp = WARP4TO3 / (WARP4TO3 + thepoint[W]);
thepoint[X] *= warp;
thepoint[Y] *= warp;
thepoint[Z] *= warp;
}
} //four_to_three
public void three_to_two(double[][] structure) {
//also stores the point's vertex radius in the 4th column
int size = structure.length;
double warp;
double[] thepoint;
for (int i=0; i<size; i++) {
thepoint = structure[i];
warp = WARP3TO2 / (WARP3TO2 + thepoint[Z]);
thepoint[X] *= warp;
thepoint[Y] *= warp;
thepoint[W] = LINERADIUS * warp; //the radius of the vertex stored here
}
} //three_to_two
public void gen_shadow() {
for (int i=0; i<NUMVERTEXPOINTS; i++) {
shadowvertexcoords[i][X] = cubevertexcoords[i][X];
shadowvertexcoords[i][Y] = FLOORY;
shadowvertexcoords[i][Z] = cubevertexcoords[i][Z];
} //for i
for (int i=0; i<NUMBRIDGEPOINTS; i++) {
shadowbridgecoords[i][X] = cubebridgecoords[i][X];
shadowbridgecoords[i][Y] = FLOORY;
shadowbridgecoords[i][Z] = cubebridgecoords[i][Z];
} //for i
three_to_two(shadowvertexcoords);
three_to_two(shadowbridgecoords);
} //gen_shadow
private double line_z(int numline) {
//returns the average z coordinate of the line indexed (in linesv and linesb) by numline
return (cubevertexcoords[linesv[numline]][Z] + cubebridgecoords[linesb[numline]][Z]);
} //line_z
public void sort_lines() {
//sorts linesv and linesb according to cubevertexcoords and cubebridgecoords
//uses insertion sort
int i,j,k,m,tempv,tempb;
for (i=1; i<NUMLINES; i++) {
if (line_z(i-1) < line_z(i)) {
for (j=i; j>0; j--)
if (line_z(j-1) > line_z(i))
break;
tempv = linesv[i];
tempb = linesb[i];
for (k=i; k>j; k--) {
linesv[k] = linesv[k-1];
linesb[k] = linesb[k-1];
} //for k
linesv[j] = tempv;
linesb[j] = tempb;
} //if
} //for i
} //sort_lines
public void run() {
lastframe = System.currentTimeMillis();
spin_xz = Math.PI/450;
spin_wz = Math.PI/225;
spin_yz = Math.PI/150;
for (;;) {
gen_cube_points();
rotate_cube();
four_to_three(cubevertexcoords);
four_to_three(cubebridgecoords);
sort_lines();
gen_shadow();
three_to_two(cubevertexcoords);
three_to_two(cubebridgecoords);
recycle();
}
} //run
public void pause(long wait) {
if (wait > 0)
try {
Thread.currentThread().sleep(wait);
} catch (InterruptedException e) {}
} //pause
public void recycle() { //recycle goddammit!
//called after a loop has done the majority of its work. will repaint and ensure a steady fps
repaint();
long wait = FRAMEDELAY - (System.currentTimeMillis() - lastframe);
pause(wait);
lastframe = System.currentTimeMillis();
} //recycle
public void update(Graphics g) {
paint(g);
} //update
public void paint(Graphics g) {
bufGraphics.setColor(Color.black);
bufGraphics.fillRect(0, 0, APPLETWIDTH, APPLETHEIGHT);
draw_floor(bufGraphics);
draw_shadow(bufGraphics);
draw_cube(bufGraphics);
g.drawImage(bufImage, 0, 0, this);
} //paint
private int depth_color(double depth) {
int col = 130 - (int)(depth*COLRANGE/(2*CUBESCALE*COLEDGE));
return col;
} //depth_color
public void draw_line(Graphics g, double xa, double ya, double xb, double yb, double ra, double rb) {
double line_width, line_height, line_length;
double height_div_length, width_div_length;
int[] polly_x = new int[4];
int[] polly_y = new int[4];
Polygon polly;
line_width = xb - xa;
line_height = yb - ya;
line_length = Math.sqrt(line_width*line_width + line_height*line_height);
height_div_length = line_height / line_length;
width_div_length = line_width / line_length;
polly_x[0] = (int)(xa + ra*height_div_length);
polly_y[0] = (int)(ya - ra*width_div_length);
polly_x[1] = (int)(xb + rb*height_div_length);
polly_y[1] = (int)(yb - rb*width_div_length);
polly_x[2] = (int)(xb - rb*height_div_length);
polly_y[2] = (int)(yb + rb*width_div_length);
polly_x[3] = (int)(xa - ra*height_div_length);
polly_y[3] = (int)(ya + ra*width_div_length);
polly = new Polygon(polly_x, polly_y, 4);
g.fillPolygon(polly);
} //draw_line
public void draw_cube(Graphics g) {
double vertex_x, vertex_y, vertex_r;
double bridge_x, bridge_y, bridge_r;
int vertex, bridge;
g.translate(CUBEOFFSETX, CUBEOFFSETY);
g.setColor(Color.black);
for (int i=0; i<NUMVERTEXPOINTS; i++)
linecount[i] = 0;
for (int i=0; i<NUMLINES; i++) {
vertex = linesv[i];
linecount[vertex]++;
vertex_x = cubevertexcoords[vertex][X];
vertex_y = cubevertexcoords[vertex][Y];
vertex_r = cubevertexcoords[vertex][W];
bridge = linesb[i];
bridge_x = cubebridgecoords[bridge][X];
bridge_y = cubebridgecoords[bridge][Y];
bridge_r = cubebridgecoords[bridge][W];
g.setColor(new Color(0,depth_color(cubebridgecoords[bridge][Z]),0));
draw_line(g, vertex_x, vertex_y, bridge_x, bridge_y, vertex_r, bridge_r);
if (linecount[vertex]>3) {
int corner_x = (int)(vertex_x - VERTEXRADIUS);
int corner_y = (int)(vertex_y - VERTEXRADIUS);
int diameter = (int)(VERTEXRADIUS*2);
int col = depth_color(cubevertexcoords[vertex][Z]) + 20;
g.setColor(new Color(col,col,col));
g.fillOval(corner_x, corner_y, diameter, diameter);
}
} //for i
g.translate(-CUBEOFFSETX, -CUBEOFFSETY);
} //draw_cube
public void draw_floor(Graphics g) {
int width, height;
for (int i=0; i<FLOORSTEPS; i++) {
g.setColor(new Color(0,FLOORCOLSTART+i*FLOORCOLCHANGE,0));
width = FLOORSTARTWIDTH*(FLOORSTEPS-i)/FLOORSTEPS;
height = FLOORSTARTDEPTH*(FLOORSTEPS-i)/FLOORSTEPS;
g.fillOval(CUBEOFFSETX - width, CUBEOFFSETY + FLOORY + FLOORSTARTY - height - i*2,
width << 1, (height << 1) - i*4);
} //for i
} //draw_floor
public void draw_shadow(Graphics g) {
double vertex_x, vertex_y, vertex_r;
double bridge_x, bridge_y, bridge_r;
int vertex, bridge;
g.translate(CUBEOFFSETX, CUBEOFFSETY);
for (int i=0; i<NUMVERTEXPOINTS; i++)
linecount[i] = 0;
for (int i=0; i<NUMLINES; i++) {
vertex = linesv[i];
linecount[vertex]++;
vertex_x = shadowvertexcoords[vertex][X];
vertex_y = shadowvertexcoords[vertex][Y];
vertex_r = shadowvertexcoords[vertex][W];
bridge = linesb[i];
bridge_x = shadowbridgecoords[bridge][X];
bridge_y = shadowbridgecoords[bridge][Y];
bridge_r = shadowbridgecoords[bridge][W];
g.setColor(Color.black);
draw_line(g, vertex_x, vertex_y, bridge_x, bridge_y, vertex_r, bridge_r);
if (linecount[vertex]>3) {
int corner_x = (int)(vertex_x - VERTEXRADIUS);
int corner_y = (int)(vertex_y - VERTEXRADIUS);
int diameter = (int)(VERTEXRADIUS*2);
g.fillOval(corner_x, corner_y, diameter, diameter);
}
} //for i
g.translate(-CUBEOFFSETX, -CUBEOFFSETY);
} //draw_shadow
public void stop() {
holmes.stop();
holmes = null;
} //stop
public void destroy() {
} //destroy
} //class Hypercube3
پیروز باشید
vBulletin , Copyright ©2000-2024, Jelsoft Enterprises Ltd.