Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package nanocourse;
- import java.awt.Color;
- import nano.*;
- import java.util.Random;
- public class Exercise3_final extends javax.swing.JFrame{
- public Exercise3_final() {
- int xSizeInput=1000;
- int ySizeInput=800;
- double bacteriumRadiusInput=20;
- double neutrophilRadiusInput=40;
- double chanceSynthesizeInput=100;
- double chanceDegradeInput=0.1;
- int amountBioInput=10000;
- double aggressivenessInput=1;
- int amountRBCInput=20;
- int xSize = xSizeInput;
- int ySize = ySizeInput;
- Canvas myScreen = new Canvas(xSize, ySize);
- Pen myPen = new Pen(myScreen);
- Random random = new Random();
- int frame=0; //how many frames have passed since start program
- int amountEaten=0;
- //properties red blood cells
- int amountRBC=amountRBCInput;
- int [] xPosRBC = new int [amountRBC];
- int [] yPosRBC = new int [amountRBC];
- int M=100;
- double [] xValueRBC = new double[M];
- double [] yValueRBC = new double[M];
- int [] rbcRadius= new int [amountRBC];
- for(int i=0;i<amountRBC;i++)
- {
- rbcRadius[i]=random.nextInt(30)+20;
- }
- //red blood cells starting positions
- for(int i=0;i<amountRBC;i++) //generate random positions
- {
- xPosRBC[i]=random.nextInt(xSize);
- yPosRBC[i]=random.nextInt(ySize);
- }
- for(int i=0;i<amountRBC;i++) //make it so the rbc do not intersect
- {
- for(int j=0;j<amountRBC;j++)
- {
- while(Math.sqrt(Math.pow(Math.abs(xPosRBC[j]-xPosRBC[i]),2)+Math.pow(Math.abs(yPosRBC[j]-yPosRBC[i]), 2))<(rbcRadius[i]+rbcRadius[j]) && i!=j)
- {
- xPosRBC[i]=random.nextInt(xSize); //random starting position of bacterium
- yPosRBC[i]=random.nextInt(ySize);
- j=0;
- }
- }
- }
- //properties bacterium
- int xPosBacterium=random.nextInt(xSize); //random starting position of bacterium
- int yPosBacterium=random.nextInt(ySize);
- double bacteriumRadius=bacteriumRadiusInput; //radius of bacterium
- for(int j=0;j<amountRBC;j++)
- {
- while(Math.sqrt(Math.pow(Math.abs(xPosRBC[j]-xPosBacterium),2)+Math.pow(Math.abs(yPosRBC[j]-yPosBacterium), 2))<(rbcRadius[j]+bacteriumRadius))
- {
- xPosBacterium=random.nextInt(xSize); //random starting position of bacterium
- yPosBacterium=random.nextInt(ySize);
- j=0;
- }
- }
- int K=100; //how many points used to draw bacterium
- double [] xValueBacterium = new double[K]; //
- double [] yValueBacterium = new double[K];
- boolean bacteriumAlive=true;
- //properties biomolecules
- int amountBio=amountBioInput;
- boolean [] bioExist = new boolean[amountBio];
- int [] xPosBio = new int [amountBio];
- int [] yPosBio = new int [amountBio];
- int [] dXBio = new int [amountBio];
- int [] dYBio = new int [amountBio];
- int [] lifetimeBio = new int [amountBio];
- double chanceDegrade=chanceDegradeInput; //chance that a biomolecule gets degraded per frame
- double chanceSynthesize=chanceSynthesizeInput; //chance that a biomolecule gets synthesized per frame
- for(int i=0;i<amountBio;i++)
- {
- bioExist[i]=false; //setting existing state to false
- }
- //properties Neutrophil
- int xPosNeutrophil=random.nextInt(xSize);
- int yPosNeutrophil=random.nextInt(ySize);
- double neutrophilRadius=neutrophilRadiusInput;
- for(int j=0;j<amountRBC;j++)
- {
- while(Math.sqrt(Math.pow(Math.abs(xPosRBC[j]-xPosNeutrophil),2)+Math.pow(Math.abs(yPosRBC[j]-yPosNeutrophil), 2))<(rbcRadius[j]+neutrophilRadius))
- {
- xPosNeutrophil=random.nextInt(xSize); //random starting position of bacterium
- yPosNeutrophil=random.nextInt(ySize);
- j=0;
- }
- }
- int L=100;
- double [] xValueNeutrophil= new double[L];
- double [] yValueNeutrophil= new double[L];
- double xVector, yVector, xNormVector,yNormVector,magnitude,xSumVector,ySumVector;
- double aggressiveness=aggressivenessInput;
- while(true) //while program is running
- {
- frame++;
- //1. Simulating a moving Bacterium
- int dXBacterium=random.nextInt(11)-5; //random motion
- int dYBacterium=random.nextInt(11)-5;
- xPosBacterium=xPosBacterium+dXBacterium;
- yPosBacterium=yPosBacterium+dYBacterium;
- for(int j=0;j<amountRBC;j++)
- {
- if(Math.sqrt(Math.pow(Math.abs(xPosRBC[j]-xPosBacterium),2)+Math.pow(Math.abs(yPosRBC[j]-yPosBacterium), 2))<(rbcRadius[j]+bacteriumRadius))
- {
- dXBacterium=-dXBacterium;
- dYBacterium=-dYBacterium;
- xPosBacterium=xPosBacterium+dXBacterium;
- yPosBacterium=yPosBacterium+dYBacterium;
- }
- }
- if(xPosBacterium<(bacteriumRadius/2+2*myPen.getSize())) //boundaries bacterium,accounting for size bacterium
- {
- xPosBacterium=(int)bacteriumRadius/2+2*myPen.getSize();
- }
- else if(xPosBacterium>xSize - (bacteriumRadius/2+2*myPen.getSize()))
- {
- xPosBacterium=xSize - ((int)bacteriumRadius/2+2*myPen.getSize());
- }
- else if(yPosBacterium<(bacteriumRadius/2+2*myPen.getSize()))
- {
- yPosBacterium=((int)bacteriumRadius/2+2*myPen.getSize());
- }
- else if(yPosBacterium>ySize - (bacteriumRadius/2+2*myPen.getSize()))
- {
- yPosBacterium=ySize - ((int)bacteriumRadius/2+2*myPen.getSize());
- }
- //2. Simulating synthesis and secretion of biomolecules by the bacterium.
- for(int i=0;i<amountBio;i++)
- {
- double synthesizeNumber=Math.random()*100;
- if(synthesizeNumber<chanceSynthesize && i==frame)
- {
- bioExist[frame]=true; //make the biomolecules exist
- }
- if(bioExist[i]==true && frame!=1) //if biomolecule exist apply motion
- {
- dXBio[i]=random.nextInt(51)-25;
- dYBio[i]=random.nextInt(51)-25;
- xPosBio[i]=xPosBio[i]+dXBio[i];
- yPosBio[i]=yPosBio[i]+dYBio[i];
- for(int j=0;j<amountRBC;j++)
- {
- if(Math.sqrt(Math.pow(Math.abs(xPosRBC[j]-xPosBio[i]),2)+Math.pow(Math.abs(yPosRBC[j]-yPosBio[i]), 2))<rbcRadius[j])
- {
- dXBio[i]=-dXBio[i];
- dYBio[i]=-dYBio[i];
- xPosBio[i]=xPosBio[i]+dXBio[i];
- yPosBio[i]=yPosBio[i]+dYBio[i];
- }
- }
- }
- else //if biomolecule doesn't exist, make position equal bacterium position
- {
- xPosBio[i]=xPosBacterium;
- yPosBio[i]=yPosBacterium;
- }
- if(xPosBio[i]>xSize) //boundaries biomolecules
- {
- xPosBio[i]=xSize;
- }
- if(xPosBio[i]<0)
- {
- xPosBio[i]=0;
- }
- if(yPosBio[i]>ySize)
- {
- yPosBio[i]=ySize;
- }
- if(yPosBio[i]<0)
- {
- yPosBio[i]=0;
- }
- if(bioExist[i]==true)
- {
- lifetimeBio[i]++;
- double degradationNumber=Math.random()*100;
- if(degradationNumber<chanceDegrade)
- {
- bioExist[i]=false;
- }
- }
- if(bioExist[i]==true && lifetimeBio[i]<=100) //if biomolecule lives shorter than 100 frames==>green
- {
- myPen.setColor(Color.GREEN); //drawing biomolecules
- myPen.setShape(Shape.CIRCLE);
- myPen.setSize(5);
- }
- if(bioExist[i]==true && (lifetimeBio[i]>100 && lifetimeBio[i]<=500)) //if biomolecule lives 101-500 frames==>green
- {
- myPen.setColor(Color.yellow); //drawing biomolecules
- myPen.setShape(Shape.CIRCLE);
- myPen.setSize(5);
- }
- if(bioExist[i]==true && (lifetimeBio[i]>500 && lifetimeBio[i]<=1000)) //if biomolecule lives 501-1000 frames==>orange
- {
- myPen.setColor(Color.ORANGE); //drawing biomolecules
- myPen.setShape(Shape.CIRCLE);
- myPen.setSize(5);
- }
- if(bioExist[i]==true && (lifetimeBio[i]>1000 && lifetimeBio[i]<=1500)) //if biomolecule lives 1001-1500 frames==>red
- {
- myPen.setColor(Color.RED); //drawing biomolecules
- myPen.setShape(Shape.CIRCLE);
- myPen.setSize(5);
- }
- if(bioExist[i]==true && lifetimeBio[i]>1500) //if biomolecule lives 2001+ frames==>magenta
- {
- myPen.setColor(Color.magenta); //drawing biomolecules
- myPen.setShape(Shape.CIRCLE);
- myPen.setSize(5);
- }
- if(bioExist[i]==true)
- {
- myPen.draw(xPosBio[i],yPosBio[i]);
- }
- if(Math.sqrt(Math.pow(Math.abs(xPosBio[i]-xPosNeutrophil),2)+Math.pow(Math.abs(yPosBio[i]-yPosNeutrophil), 2))<neutrophilRadius)
- {
- bioExist[i]=false; //degrade if inside neutrophil
- }
- }
- if(bacteriumAlive==true)
- {
- for(int i = 0; i <K ; i++) //defining circle, drawing points, placed here because it needs to be on top
- {
- xValueBacterium[i] = bacteriumRadius*Math.cos(2*Math.PI*i/K);
- yValueBacterium[i] = bacteriumRadius*Math.sin(2*Math.PI*i/K);
- myPen.setColor(Color.MAGENTA);
- myPen.setShape(Shape.CIRCLE);
- myPen.setSize(5);
- myPen.draw((int)xValueBacterium[i]+xPosBacterium,(int)yValueBacterium[i]+yPosBacterium);
- }
- }
- //5. Simulating the neutrophil eating the bacteriun
- xSumVector=0;
- ySumVector=0;
- for(int i=0;i<amountBio;i++)
- {
- if(Math.abs(xPosBio[i]-xPosNeutrophil)<(30+neutrophilRadius) && Math.abs(yPosBio[i]-yPosNeutrophil)<(30+neutrophilRadius) && bioExist[i]==true)
- {
- xVector=xPosBio[i]-xPosNeutrophil;
- yVector=yPosBio[i]-yPosNeutrophil;
- magnitude=Math.sqrt(Math.pow(xVector, 2)+Math.pow(yVector, 2));
- xNormVector=xVector/magnitude;
- yNormVector=yVector/magnitude;
- xSumVector=xSumVector+xNormVector;
- ySumVector=ySumVector+yNormVector;
- }
- }
- //3. Simulating a moving neutrophil
- int dXNeutrophil=random.nextInt(11)-5+(int)aggressiveness*(int)xSumVector;
- int dYNeutrophil=random.nextInt(11)-5+(int)aggressiveness*(int)ySumVector;
- xPosNeutrophil=xPosNeutrophil+dXNeutrophil;
- yPosNeutrophil=yPosNeutrophil+dYNeutrophil;
- for(int j=0;j<amountRBC;j++)
- {
- if(Math.sqrt(Math.pow(Math.abs(xPosRBC[j]-xPosNeutrophil),2)+Math.pow(Math.abs(yPosRBC[j]-yPosNeutrophil), 2))<(rbcRadius[j]+neutrophilRadius))
- {
- dXNeutrophil=-dXNeutrophil; //random motion
- dYNeutrophil=-dYNeutrophil;
- xPosNeutrophil=xPosNeutrophil+dXNeutrophil;
- yPosNeutrophil=yPosNeutrophil+dYNeutrophil;
- }
- }
- myPen.setSize(8);
- if(xPosNeutrophil<(neutrophilRadius/2+2*myPen.getSize())) //boundaries neutrophil
- {
- xPosNeutrophil=(int)neutrophilRadius/2+2*myPen.getSize();
- }
- else if(xPosNeutrophil>xSize - (neutrophilRadius/2+2*myPen.getSize()))
- {
- xPosNeutrophil=xSize - ((int)neutrophilRadius/2+2*myPen.getSize());
- }
- else if(yPosNeutrophil<(neutrophilRadius/2+2*myPen.getSize()))
- {
- yPosNeutrophil=((int)neutrophilRadius/2+2*myPen.getSize());
- }
- else if(yPosNeutrophil>ySize - (neutrophilRadius/2+2*myPen.getSize()))
- {
- yPosNeutrophil=ySize - ((int)neutrophilRadius/2+2*myPen.getSize());
- }
- for(int i = 0; i <L ; i++) //defining circle, drawing points, placed here because it needs to be on top
- {
- xValueNeutrophil[i] = neutrophilRadius*Math.cos(2*Math.PI*i/L);
- yValueNeutrophil[i] = neutrophilRadius*Math.sin(2*Math.PI*i/L);
- myPen.setColor(Color.blue);
- myPen.setShape(Shape.CIRCLE);
- myPen.draw((int)xValueNeutrophil[i]+xPosNeutrophil,(int)yValueNeutrophil[i]+yPosNeutrophil);
- }
- if(Math.abs(xPosNeutrophil-xPosBacterium)<(neutrophilRadius+bacteriumRadius) && Math.abs(yPosNeutrophil-yPosBacterium)<(neutrophilRadius+bacteriumRadius) && bacteriumRadius >=0)
- {
- bacteriumRadius=bacteriumRadius-1;
- if(bacteriumRadius==0)
- {
- bacteriumAlive=false;
- }
- }
- if(bacteriumAlive==false)
- {
- bacteriumAlive=true;
- amountEaten++;
- xPosBacterium=random.nextInt(xSize); //random starting position of bacterium
- yPosBacterium=random.nextInt(ySize);
- bacteriumRadius=bacteriumRadiusInput;
- for(int j=0;j<amountRBC;j++)
- {
- while(Math.sqrt(Math.pow(Math.abs(xPosRBC[j]-xPosBacterium),2)+Math.pow(Math.abs(yPosRBC[j]-yPosBacterium), 2))<(rbcRadius[j]+bacteriumRadius))
- {
- xPosBacterium=random.nextInt(xSize); //random starting position of bacterium
- yPosBacterium=random.nextInt(ySize);
- j=0;
- }
- }
- }
- for(int i=0;i<amountRBC;i++)
- {
- for(int j=0;j<M;j++) //red blood cells
- {
- xValueRBC[j] = rbcRadius[i]*Math.cos(2*Math.PI*j/M);
- yValueRBC[j] = rbcRadius[i]*Math.sin(2*Math.PI*j/M);
- myPen.setColor(Color.RED);
- myPen.setShape(Shape.CIRCLE);
- myPen.setSize(5);
- myPen.draw((int)xValueRBC[j]+xPosRBC[i],(int)yValueRBC[j]+yPosRBC[i]);
- }
- }
- for(int i=0;i<amountEaten;i++)
- {
- myPen.setColor(Color.white);
- myPen.setShape(Shape.CIRCLE);
- myPen.setSize(30);
- myPen.draw(50*(i+1),ySize-50);
- }
- myScreen.update(); //updating/refreshing screen
- myScreen.pause(10);
- myScreen.clear();
- }
- }
- public static void main(String[] args) {
- Exercise3_final e = new Exercise3_final();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement