import processing.core.*; 
import processing.xml.*; 

import peasy.*; 
import processing.opengl.*; 
import processing.pdf.*; 

import java.applet.*; 
import java.awt.Dimension; 
import java.awt.Frame; 
import java.awt.event.MouseEvent; 
import java.awt.event.KeyEvent; 
import java.awt.event.FocusEvent; 
import java.awt.Image; 
import java.io.*; 
import java.net.*; 
import java.text.*; 
import java.util.*; 
import java.util.zip.*; 
import java.util.regex.*; 

public class MIT4_112F12_Code_Ex1_LK extends PApplet {





boolean dosave = false;


Molec[] molecs;
float d[];
float moveX[];
float moveY[];
float moveZ[];


float addx[];
float addy[];
float addz[];

PVector[] mover;

boolean start = true;
  
public void createmolec(int amt) {

  
  molecs = new Molec[amt];
  
  for (int i = 0; i < amt; i++) {
  
    molecs[i] = new Molec(random(width), random(height), -2, 10 ,20);
   
  }
}

  PeasyCam cam;
  
public void setup() {
  size(1300, 710,OPENGL);
  createmolec(400);
   frameRate(300);
}

public void initCam(){
 cam = new PeasyCam(this, 1000);
 cam.setMinimumDistance(100);
 cam.setMaximumDistance(1500);

}
  
public void draw() {
   if(dosave) {
    PGraphicsPDF pdf = (PGraphicsPDF)beginRaw(PDF, "lines" + frameCount + ".pdf"); 
   
  }
 
  background(255);
  if (start == true){ 
  for (int i = 0; i < molecs.length; i++) {
    molecs[i].update();
    molecs[i].attract();
  molecs[i].mouseReleased();

}  

  }

    for (int j = 0; j < molecs.length; j++) {
      mover[j] = new PVector(0,0,0);
    }
    if(dosave) {
    endRaw();
    dosave=false;
  }
  }
  public void mousePressed() {
  dosave = false;
}

public void mouseReleased() {
  dosave = false;
}

//void keyPressed(){
 // start = true;
//}


class Molec
{
  float xPos, yPos, zPos, xPos2, yPos2, zPos2, mox, moy,moz, a, near, near2;
  boolean on2;
  boolean buttonon;
  boolean pmousePressed = false ;
 
  Molec(float x, float y,float z, float t, float t2) {
    xPos = x;
    yPos = y;
    zPos= z ;
    near = t;
    near2 = t2;
    a =20;
  }
 
 
  public void update() {
 
    smooth();
 
    if ((dist(xPos, yPos, mouseX, mouseY)<=20)&&(mousePressed)&& !pmousePressed) {
buttonon=!buttonon;
    }
     
     
    if (buttonon==true){
      fill(0,0,255,0);
      xPos=mouseX;
      yPos=mouseY;
      
    }
    else {
      noFill();
    }
    
         
    strokeWeight(1.0f);
    stroke(100, 50, 50, 100);
   
     pmousePressed = mousePressed;
  }
  
 
  public void attract() {
 
    mover = new PVector[molecs.length];
    addx = new float[molecs.length];
    addy = new float[molecs.length];
    addz = new float[molecs.length];
    
    moveX = new float[molecs.length];
    moveY = new float[molecs.length];
    moveZ = new float[molecs.length];
 
    for (int i = 0; i < molecs.length; i++) {
      mover[i] = new PVector(0, 0);
      near = dist(xPos, yPos,zPos, molecs[i].xPos, molecs[i].yPos ,molecs[i].zPos);
 
      if ((near<=150)) {
 
 
        moveX[i] = xPos - molecs[i].xPos;
        moveY[i] = yPos - molecs[i].yPos;
        moveZ[i] = zPos - molecs[i].zPos;
 
        mover[i].set(moveX[i], moveY[i], moveZ[i]);
 
        if (mover[i].mag()!=0) {
          
          if ((120/mover[i].mag())>=20){
            strokeWeight(0);
          }
          else{
        strokeWeight(100/mover[i].mag());
          }
        stroke(0, 0, 255, 10);
        line(xPos, yPos,zPos, molecs[i].xPos, molecs[i].yPos, molecs[i].zPos);
          addx[i] = mover[i].x/mover[i].mag();
          addy[i] = mover[i].y/mover[i].mag();
          addy[i] = mover[i].z/mover[i].mag();
        }
      }
      if (near>=10) {
        xPos-=addx[i]/100;
        yPos-=addy[i]/100;
        zPos-=addy[i]/100;
      }
      else {
        xPos+=addx[i];
        yPos+=addy[i];
        zPos+=addy[i];
      }
    }
  }
 
 
   
 public void mouseReleased(){
    
  buttonon = false;
    
 }
}
  static public void main(String args[]) {
    PApplet.main(new String[] { "--bgcolor=#FFFFFF", "MIT4_112F12_Code_Ex1_LK" });
  }
}
