Merging two sketches?
in
Programming Questions
•
1 year ago
Hi all,
I have been trying to merge two separate sketches but no success.
The first is a sketch of rises cells infecting one healthy cell, and the resulting transformation of the healthy cell.
The second is the user being able to click and drag an infected cell to create more of the pattern.
I have been trying with this for hours now, any suggestions?
I will post the two separate sketches on here (respectively), if you want me to post what I've been trying, let me know.
- Disease[] d = new Disease[55];
- HealthyCell health;
- void setup () {
- size (500, 500);
- smooth();
- for (int i = 0; i < d.length; i++) {
- float x = random(0, 15);
- float y = map(i, 0, d.length, -100, height-200);
- d[i] = new Disease(30, x, y);
- health = new HealthyCell(380, 380, 200);
- }
- }
- void draw () {
- background(0);
- health.display();
- for (int i = 0; i < d.length; i++) {
- d[i].display();
- d[i].move();
- if (d[i].intersect(health)) {
- d[i].attach();
- fill(255, 0, 0);
- }
- }
- for (Grow g: grow) {
- g.draw();
- }
- }
- //////////////////////////////
- class Disease {
- float s;
- float x;
- float y;
- float xIncrement = 5.0;
- float yIncrement = 5.01;
- float d;
- //this determines whether the cell is attached to the health cell or not
- boolean attached = false;
- Disease (float d, float tempX, float tempY) {
- d = s;
- xIncrement = tempX;
- yIncrement = tempY;
- x = 40;
- y = 40;
- }
- void move () {
- //if I am not attached to the helth cell
- if (attached == false) {
- float d = dist(mouseX, mouseY, x, y);
- d = constrain(d, 0, 50);
- float speed = map(d, 0, 49, 0.2, 0.001);
- xIncrement += speed;
- yIncrement += speed-0.01;
- x = noise(xIncrement)*width;
- y = noise(yIncrement)*height;
- }
- else {
- //get it to do the stuff that attaches it to the healthy cell
- //"constrain" it within the cell
- //move slower
- float speed = 0.005;
- xIncrement += speed;
- yIncrement += speed-0.001;
- //instead of being able to move anywhere one the screen move only around the cell area
- //i.e. move to the top of the circle then constrict the noise to the diameter of the health cell
- y = (health.y-health.diam/2) + noise(yIncrement)*health.diam;
- x = (health.x-health.diam/2) + noise(xIncrement)*health.diam;
- }
- }
- void attach() {
- //speed = 0;
- //y = 10;
- //changes the attached variable to true
- if (!attached) {
- // tell the cell it got one virus more
- health.cellCount++;
- attached = true;
- }
- }
- boolean intersect(HealthyCell tempH) {
- // Calculate distance
- float distance = dist(x, y, tempH.x, tempH.y);
- // Compare distance to sum of radius we need to divide the diameter by 2 to get the radius
- if (distance < d/2 + tempH.diam/2) {
- return true;
- }
- else {
- return false;
- }
- }
- void display () {
- stroke(random(255), 50, 100);
- fill(30);
- ellipse(random(x, x+3), random(y, y+3), s-5, s-5);
- }
- }
- /////////////////////////////
- class HealthyCell {
- float x;
- float y;
- int diam;
- float angle = 0.0;
- float offset = 60;
- float scalar = 30;
- float speed = 0.05;
- HealthyCell (float tempX, float tempY, int d) {
- x = tempX;
- y = tempY;
- diam = d;
- cellCount = 0;
- bHealthy = true;
- }
- void display() {
- strokeWeight(5);
- // if over 15 disease cells, healthy cell gets infected
- if (cellCount > 15) {
- if (bHealthy) {
- println ("infected");
- }
- bHealthy=false;
- }
- // if healthy
- if (bHealthy) {
- // if healthy border is gray
- stroke(55);
- fill(200, 50);
- }
- else
- {
- noStroke();
- int num = 40;
- float[] x= new float[num]; //x value
- float[] y= new float[num]; //y value
- float[] a= new float[num]; //rate of growth
- color[] c= new color[num]; //colour
- for (int i=0; i< num;i++) {
- x[i] = random(200);
- y[i] = i* (2/num);
- a[i] = random(3, 6);
- c[i] = color(int(random(255)), 50, 100, 50);
- }
- for (int i=0; i< num; i++) {
- stroke(100);
- fill(c[i]);
- ellipse(x, y, diam, diam);
- x[i]=x[i]+ a[i];
- if (x[i]> 200) {
- x[i] = -100;
- }
- }
- //fill(200);
- strokeWeight(5);
- stroke(random(255), 50, 50, 50);
- ellipse(x, y, diam, diam);
- }
- }
- ArrayList<NewCell> ncell;
- boolean isMoving;
- NewCell selectedNewCell;
- void setup() {
- size(400, 400);
- ncell = new ArrayList<NewCell>();
- ncell.add(new NewCell(50, 50));
- }
- void draw() {
- background(100);
- for (NewCell n: ncell) {
- n.draw();
- }
- if (isMoving & selectedNewCell!= null) {
- PVector motion = new PVector(mouseX - pmouseX, mouseY - pmouseY);
- selectedNewCell._pos.add(motion);
- }
- }
- void mousePressed() {
- PVector mousePos = new PVector(mouseX, mouseY);
- for (NewCell n: ncell) {
- if (n.isMouseOver(mousePos)) {
- ncell.add(new NewCell(n._pos.x, n._pos.y));
- selectedNewCell = ncell.get(ncell.size() - 1);
- break;
- }
- }
- isMoving = true;
- }
- void mouseReleased() {
- isMoving = false;
- selectedNewCell = null;
- }
- ///////////////////
- class NewCell {
- PVector _pos;
- int _radius = 200;
- NewCell(float x, float y) {
- _pos = new PVector(x, y);
- }
- void draw() {
- int num = 50;
- float[] x= new float[num]; //x value
- float[] y= new float[num]; //y value
- float[] a= new float[num]; //rate of growth
- color[] c= new color[num]; //colour
- for (int i=0; i< num;i++) {
- x[i] = random(200);
- y[i] = i* (5/num);
- a[i] = random(3, 6);
- c[i] = color(int(random(255)), 50, 100, 50);
- }
- for (int i=0; i< num; i++) {
- stroke(random(255), 50, 50);
- fill(c[i]);
- ellipse(_pos.x, _pos.y, _radius-x[i],_radius-x[i]);
- x[i]=x[i]+ a[i];
- if (x[i]> 200) {
- x[i] = -100;
- }
- }
- }
- boolean isMouseOver(PVector mouse) {
- if (_pos.dist(mouse) <= _radius) {
- return true;
- } else {
- return false;
- }
- }
- }
1