Encyclopedia > User:Cyp Java

  Article Content

User:Cyp/Java

< User:Cyp

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

public class Polyhedra extends Applet{
double g=1.6180339887498948482045868343656381177203; //(1+Math.sqrt(5))/2;
double d=0.6180339887498948482045868343656381177203; //1/g;
double s=1.4142135623730950488016887242096980785696; //Math.sqrt(2);
double tetrav[]={
-1,-s,0, -1,s,0, 1,0,-s, 1,0,s};
int tetrat[]={
0,3,1, 3,2,1, 1,2,0, 3,0,2};
double hexav[]={
-1,-1,-1, -1,-1,1, -1,1,-1, -1,1,1, 1,-1,-1, 1,-1,1, 1,1,-1, 1,1,1};
int hexat[]={
0,1,3,2, 2,3,7,6, 0,2,6,4, 3,1,5,7, 0,4,5,1, 6,7,5,4};
double octav[]={
-1,0,0, 1,0,0, 0,-1,0, 0,1,0, 0,0,-1, 0,0,1};
int octat[]={
0,4,3, 3,4,1, 1,4,2, 2,4,0, 3,5,0, 0,5,2, 2,5,1, 1,5,3};
double dodecav[]={
0,-d,-g, 0,-d,g, 0,d,-g, 0,d,g, -d,-g,0, -d,g,0, d,-g,0, d,g,0, -g,0,-d, g,0,-d, -g,0,d, g,0,d, -1,-1,-1, -1,-1,1, -1,1,-1, -1,1,1, 1,-1,-1, 1,-1,1, 1,1,-1, 1,1,1};
int dodecat[]={
0,2,14,8,12, 0,16,9,18,2, 4,6,16,0,12, 2,18,7,5,14, 13,4,12,8,10, 6,17,11,9,16, 15,10,8,14,5, 18,9,11,19,7, 1,17,6,4,13, 5,7,19,3,15, 13,10,15,3,1, 19,11,17,1,3};
double icosav[]={
0,-1,-g, 0,-1,g, 0,1,-g, 0,1,g, -1,-g,0, -1,g,0, 1,-g,0, 1,g,0, -g,0,-1, g,0,-1, -g,0,1, g,0,1};
int icosat[]={
0,2,8, 2,0,9, 4,0,8, 4,6,0, 6,9,0, 5,8,2, 5,2,7, 7,2,9, 10,4,8, 10,8,5, 6,11,9, 9,11,7, 10,5,3, 10,1,4, 1,11,6, 3,7,11, 1,6,4, 3,5,7, 3,11,1, 1,10,3};
public void paint(Graphics g) {
g.setColor(new Color(0xffffff));
g.fillRect(0, 0, 200, 200);
double n=Math.random();
if(n<.2) drawfig(g, tetrav, tetrat, 3);
else if(n<.4) drawfig(g, hexav, hexat, 4);
else if(n<.6) drawfig(g, octav, octat, 3);
else if(n<.8) drawfig(g, dodecav, dodecat, 5);
else drawfig(g, icosav, icosat, 3);
}
void drawfig(Graphics g, double vs[], int ts[], int m) {
double tr[]=randmat();
for(int back=-1;back<=1;back+=2) for(int t=0;t*m<ts.length;++t) {
double f, td[];
double a1=vs[ts[t*m ]*3], a2=vs[ts[t*m ]*3+1], a3=vs[ts[t*m ]*3+2];
f=1/Math.sqrt(a1*a1+a2*a2+a3*a3); a1*=f; a2*=f; a3*=f; td=trans(a1, a2, a3, tr); a1=td[0]; a2=td[1]; a3=td[2];
double b1=vs[ts[t*m+1]*3], b2=vs[ts[t*m+1]*3+1], b3=vs[ts[t*m+1]*3+2];
f=1/Math.sqrt(b1*b1+b2*b2+b3*b3); b1*=f; b2*=f; b3*=f; td=trans(b1, b2, b3, tr); b1=td[0]; b2=td[1]; b3=td[2];
double c1=vs[ts[t*m+2]*3], c2=vs[ts[t*m+2]*3+1], c3=vs[ts[t*m+2]*3+2];
f=1/Math.sqrt(c1*c1+c2*c2+c3*c3); c1*=f; c2*=f; c3*=f; td=trans(c1, c2, c3, tr); c1=td[0]; c2=td[1]; c3=td[2];
double d1=b1-a1, d2=b2-a2, d3=b3-a3;
double e1=c1-a1, e2=c2-a2, e3=c3-a3;
double f1=e2*d3-d2*e3, f2=e3*d1-d3*e1, f3=e1*d2-d1*e2;
double da1=a1/(4+a3), da2=a2/(4+a3),
db1=b1/(4+b3)-da1, db2=b2/(4+b3)-da2,
dc1=c1/(4+c3)-da1, dc2=c2/(4+c3)-da2;
if(back* db1*dc2-dc1*db2 /*f3*/<0) {
g.setColor(new Color((float)Math.random(), (float)Math.random(), (float)Math.random(), (float)(back<0?.90:0.90)));
int xa[]=new int[m], ya[]=new int[m];
xa[0]=(int)(a1*3/(4+a3)*100+100); xa[1]=(int)(b1*3/(4+b3)*100+100); xa[2]=(int)(c1*3/(4+c3)*100+100);
ya[0]=(int)(a2*3/(4+a3)*100+100); ya[1]=(int)(b2*3/(4+b3)*100+100); ya[2]=(int)(c2*3/(4+c3)*100+100);
for(int w=3;w<m;++w) {
a1=vs[ts[t*m+w]*3]; a2=vs[ts[t*m+w]*3+1]; a3=vs[ts[t*m+w]*3+2];
f=1/Math.sqrt(a1*a1+a2*a2+a3*a3); a1*=f; a2*=f; a3*=f; td=trans(a1, a2, a3, tr); a1=td[0]; a2=td[1]; a3=td[2];
xa[w]=(int)(a1*3/(4+a3)*100+100); ya[w]=(int)(a2*3/(4+a3)*100+100);
}
g.fillPolygon(xa, ya, m);
}
}
}
double[] randmat() {
double[] r=new double[9];
double s, x, y, z;
int i, j;
for(i=0;i<3;++i) {
x=Math.random()*2-1;
y=Math.random()*2-1;
z=Math.random()*2-1;
s=x*x+y*y+z*z;
if(s>1) { --i; continue; }
for(j=0;j<i;++j) {
s=x*r[j*3 ]
+y*r[j*3+1]
+z*r[j*3+2];
x-=s*r[j*3 ];
y-=s*r[j*3+1];
z-=s*r[j*3+2];
}
s=x*x+y*y+z*z;
if(s==0) { --i; continue; }
s=Math.sqrt(s);
x/=s; y/=s; z/=s;
r[i*3 ]=x;
r[i*3+1]=y;
r[i*3+2]=z;
}
if(r[0]*r[4]*r[8]+r[1]*r[5]*r[6]+r[2]*r[3]*r[7]-r[0]*r[5]*r[7]-r[1]*r[3]*r[8]-r[2]*r[4]*r[6]<0) {
r[0]=-r[0];
r[1]=-r[1];
r[2]=-r[2];
}
return(r);
}
double[] trans(double x, double y, double z, double[] t) {
return(new double[]{x*t[0]+y*t[1]+z*t[2],
x*t[3]+y*t[4]+z*t[5],
x*t[6]+y*t[7]+z*t[8]});
}
}



All Wikipedia text is available under the terms of the GNU Free Documentation License

 
  Search Encyclopedia

Search over one million articles, find something about almost anything!
 
 
  
  Featured Article
Dennis Gabor

...     Contents Dennis Gabor Dennis Gabor (Gábor Dénes) (1900-1979) was a Hungarian physicist. He invented holography in 1947, for whic ...

 
 
 
This page was created in 22.2 ms