Behaving Weird, need help !
in
Programming Questions
•
1 month ago
Hi,
I have wrote this code and of course it is inspired from someone's code. Here, I am facing some of these problems
1. When I increase the speed of movements of the points, they appear as a small line parallel to line of motion.
2. These points get connect via line if they come closer to a certain distance and it should happen like this with all the points but somewhat it is not happening.
please tell me reason or any logic fail. I want to learn these things..... giving or fixing code help me but wont tell me the problem so please try explaining the problem.
Thanks
- ArrayList poop;
- boolean flag=false;
- int distance=50;
- void setup()
- {
- size(600,600);
- smooth();
- poop = new ArrayList();
- for (int i=0;i<100;i++)
- {
- Particle P = new Particle();
- poop.add(P);
- }
- }
- void mousePressed()
- {
- }
- void draw()
- {
- background(20);
- for (int i=0;i<poop.size();i++)
- {
- Particle Pn1 = (Particle) poop.get(i);
- // Pn1.display();
- for (int j = i + 1; j < poop.size(); j++) {
- Particle Pn2 = (Particle) poop.get(j);
- Pn2.display();
- if (dist(Pn1.x, Pn1.y, Pn2.x, Pn2.y)< distance)
- {
- for (int k = j + 1; k < poop.size(); k++) {
- Particle Pn3 = (Particle) poop.get(k);
- Pn3.display();
- if (dist(Pn3.x, Pn3.y, Pn2.x, Pn2.y)< distance) {
- if (flag) {
- stroke(255, 10);
- fill(Pn3.c,95); // method to access the class property
- }
- else {
- noFill();
- stroke(255, 50);
- }
- beginShape(TRIANGLES);
- vertex(Pn1.x, Pn1.y);
- vertex(Pn2.x, Pn2.y);
- vertex(Pn3.x, Pn3.y);
- endShape();
- }
- }
- }
- }
- }
- }
- void keyPressed()
- {
- flag=!flag;
- }
- class Particle {
- float x, y;
- color c;
- int i=1, j=1;
- Particle( )
- {
- x = random(0, width);
- y = random(0, height);
- int j = (int)random(0,4);
- if(j==0)
- {
- c = color(#05CDE5);
- }
- if(j==1)
- {
- c = color(#FFB803);
- }
- if(j==2)
- {
- c = color(#FF035B);
- }
- if(j==3)
- {
- c = color(#3D3E3E);
- }
- }
- void display()
- {
- pushStyle();
- strokeWeight(1);
- stroke(c);
- point(x, y);
- popStyle();
- update();
- }
- void update()
- {
- x = x + j*random(0, 0.01);
- y = y + i*random(0, 0.01);
- if (y > height) i=-1;
- if (y < 0) i=1;
- if (x > width) j=-1;
- if (x < 0) j=1;
- }
- }
1