⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 galaxy.java

📁 Processing軟件製作出的聖誕動態圖像
💻 JAVA
字号:
import processing.core.*; import processing.xml.*; import java.applet.*; import java.awt.*; import java.awt.image.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.text.*; import java.util.*; import java.util.zip.*; import java.util.regex.*; public class Galaxy extends PApplet {// Spiral Galaxy Simulation// author : Philippe Guglielmetti (Dr. Goulu www.goulu.net)// 2008.04.03 PhG : my first processing program !//                  converted from my LUA/Demoniak 3D demo V 1.0//                  see http://3dmon.wordpress.com/2007/08/26/simulation-de-galaxie-spirale/// 2008.04.04 PhG : much faster in P3D mode : now ok with 10'000 stars !//                  keyboard interaction : use arrow keys to alter the galaxy//import gifAnimation.*; // http://www.extrapixel.ch/processing/gifAnimation/public float gauss(float x){ return exp(-x*x/2.0f) / sqrt(2*PI); }public float gaussI(float z){ // integrated gauss [0..1]  if(z<-8.0f) return 0.0f;  if(z> 8.0f) return 1.0f;  float sum=0.0f, term=z;  for (int i=3; sum+term!=sum; i+=2){    sum =sum+term;    term=term*z*z/i;  }  return 0.5f+sum*gauss(z);}public float gaussE(float z){ return gaussI(z)*2-1; }// gauss error func==> [-1..0..1]public float randomGauss(){ float x=0,y=0,r,c;  do{ x=random(-1.0f,1.0f);      y=random(-1.0f,1.0f);      r=x*x+y*y;  }while(r==0 || r>1);  c=sqrt(-2.0f*log(r)/r);  return x*c; //return [x*c, y*c];}public float randomGaussIn(float L, float H, float mul){ return constrain( randomGauss()*(H-L)*mul + (L+H)/2.0f  ,L,H);  }public float randomGaussAt(float L, float H, float mul){ return            randomGauss()*(H-L)*mul + (L+H)/2.0f;  }float pi=4*atan(1);int stars=10000; // only ...int Rmax=200; // galaxy radiusfloat speed=0.02f;  // rotation speed// stars follow elliptic orbits around the centerfloat eratio=.85f; // ellipse ratiofloat etwist=8.0f/Rmax; // twisting factor (orbit axes depend on radius)float []angle=new float[stars];float []radius=new float[stars];float cx; float cy; //centerPImage img;public void setup(){  size(PApplet.parseInt(Rmax*3), PApplet.parseInt(Rmax*2),P3D);  background(0); // back to black  speed=speed/frameRate;  // begin in the center  cx = width/2;  cy = height/2;  // itit stars  for (int i=0; i< stars; i++){    angle[i]= random(0,2*pi);    //radius[i]=random(1,Rmax);    radius[i]=((abs(randomGauss())))*Rmax*0.6f+0.0f;  }  // gifExport = new gifAnimation.GifMaker(this, "galaxy.gif");  // gifExport.setRepeat(0);}public void draw(){  //smooth();  noSmooth();  img=get();  img.resize(round(width*0.5f),round(height*0.5f));  img.resize(width-2,height-2);  tint(245,250,255);  image(img,0,0);  //fill(0,8); rect(0,0,width,height);  noStroke();  float r,a,x,y,b,s,c,xx,yy,dd;  for (int i=0; i< stars; i++){    r=radius[i];    a=angle[i]+speed*(Rmax/r)*3.0f; // increment angle    angle[i]=a;    x=r*sin(a);    y=r*eratio*cos(a);    b=r*etwist;    s=sin(b);    c=cos(b);    xx=cx + s*x + c*y; // a bit of trigo    yy=cy + c*x - s*y;    //dd=(r-50.0)*7.0;    dd=40000.0f/r;    fill(color(dd,dd,dd*0.9f,32));    rect(xx-1.5f,yy-1.5f,3.0f,3.0f);  }  // gifExport.setDelay(1); gifExport.addFrame();}public void keyPressed() {  if(keyCode == UP) { eratio=eratio*1.02f; }  if(keyCode == DOWN) { eratio=eratio/1.02f; }  if(keyCode == LEFT) { etwist=etwist+0.001f; }  if(keyCode == RIGHT) { etwist=etwist-0.001f; }   //println("eratio="+eratio+" etwist="+etwist);}  static public void main(String args[]) {    PApplet.main(new String[] { "Galaxy" });  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -