Java - Written by admin on Thursday, February 28, 2008 11:48 - 0 Comments

Orbitals B - Source Code

Tags:
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...
Demo: http://www.complexification.net/gallery/machines/orbitals/appletBl/index.html
I think u will like this source code, see demo and like it.
Source Code Here !
// Orbitals, variation B
// j.tarbell August, 2004
// Albuquerque, New Mexico
// complexification.net

// Processing 0085 Beta syntax update
// j.tarbell   April, 2005

int dim=900;
int num=500;

Orbital[] orbitals;

int maxpal = 512;
int numpal = 0;
color[] goodcolor = new color[maxpal];

void setup() {
  size(900,900,P3D);
//  size(dim,dim,P3D);
  takecolor("longConejo.gif");
  background(255);
  noFill();
  framerate(30);
  orbitals = new Orbital[num];

  resetAll();
}

void draw() {
  // k loop is accelerator
  for (int k=0;k<20;k++) {
    // orbit all orbitals
    for (int n=0;n<num;n++) {
      orbitals[n].orbit();
    }
    // draw all orbitals
    for (int n=0;n<num;n++) {
      orbitals[n].draw();
    }
  }
}

void mousePressed() {
  resetAll();
  background(255);
}

void resetAll() {
  // make orbitals
  for (int n=0;n<num;n++) {
    // pick orbit origin node
    int npid = n;
    if (n>num*0.1) {
      npid = int(random(n));
    }
    orbitals[n] = new Orbital(n,npid);
    if (n==npid) {
      orbitals[n].setPosition(dim/2,dim/2);
    }
  }
}

// OBJECTS ---------------------------------------------------------------------

class Orbital
{
  int id;
  int pid;
  float r;
  float t;
  float tv, tvd;
  float x,y;
  int d;
  color myc;

  Orbital(int Id, int Pid) {
    id=Id;
    pid=Pid;
    if (id!=pid) {
      // calculate depth
      d=orbitals[pid].d+1;
      // radius inversely proportional to depth
      r=random(1,1+0.4*dim/d);
      // angle theta
      t=-HALF_PI;
      // theta velocity
      tv=random(0.0001,0.02/(d+1));
      if (random(100)<50) tv*=-1;
      // theta differential
      tvd=random(1.001,1.010);
    } else {
      // is central node
      r = 0;
    }
    // choose arbitrary color
    myc = somecolor();
  }

  void setPosition(float X, float Y) {
    x = X;
    y = Y;
  }

  void orbit() {
    t+=tv;
    x = orbitals[pid].x+r*cos(t);
    y = orbitals[pid].y+r*sin(t);

    // heehehe
    tv*=0.99942;    // slow down time
//    r*=1.00022;    // decrease orbits
  }

  void draw() {
    // fuzz
    float fzx = random(-0.22,0.22);
    float fzy = random(-0.22,0.22);

    // draw translucent pixel
    stroke(red(myc),green(myc),blue(myc),42);
    point(x+fzx,y+fzy);

    if (sumtv()<1.00001) {
      // draw orbit path
      float o = random(TWO_PI);
      fzx = orbitals[pid].x+r*cos(o);
      fzy = orbitals[pid].y+r*sin(o);
      stroke(red(myc),green(myc),blue(myc),18);
      point(fzx,fzy);

      // draw parent line
      o = random(1.0);
      fzx = x+o*(orbitals[pid].x-x);
      fzy = y+o*(orbitals[pid].y-y);
      stroke(0,18);
      point(fzx,fzy);
    }
  }

  float sumtv() {
    if (pid!=id) {
      return (orbitals[pid].sumtv() + tv);
    } else {
      return tv+1;
    }
  }
}

// COLOR ROUTINES -----------------------------------------------------------

color somecolor() {
  // pick some random good color
  return goodcolor[int(random(numpal))];
}

void takecolor(String fn) {
  PImage b;
  b = loadImage(fn);
  image(b,0,0);

  for (int x=0;x<b.width;x++){
    for (int y=0;y<b.height;y++) {
      color c = get(x,y);
      boolean exists = false;
      for (int n=0;n<numpal;n++) {
        if (c==goodcolor[n]) {
          exists = true;
          break;
        }
      }
      if (!exists) {
        // add color to pal
        if (numpal<maxpal) {
          goodcolor[numpal] = c;
          numpal++;
        } else {
          break;
        }
      }
      if (random(10000)<100) {
        if (numpal<maxpal) {
          // pump black or white into palette
          if (random(100)<50) {
            goodcolor[numpal] = #FFFFFF;
            print("w");
          } else {
            goodcolor[numpal] = #000000;
            print("b");
          }
          numpal++;
        }
      }
    }
  }

}

// j.tarbell August, 2004
// Albuquerque, New Mexico
// complexification.net
Read more:
»» Computer Random vs. True Random
»» Web 2.0 style buttons
»» Head: Side View
»» How To Detect Internet Explorer Version With PHP
»» Did Earth once have multiple moons?


Leave a Reply

Comment

Most Popular Content

PHP, Tip & Tricks - May 30, 2008 3:20 - 0 Comments

Reuse Excel business logic with PHPExcel

More In Code


Photoshop Tips - May 31, 2008 8:20 - 0 Comments

Popular Wealth - Free Photoshop Brushes To Download

More In How to ?


Wordpress - May 31, 2008 3:34 - 0 Comments

Thinking the Way Animals Do

More In I LIKE IT !