Confine particles to a space

I want to confine the particles and emergent lines (in the code) to a specific space like an annular ring between two concentric circles. Now, the particles and lines are all over the canvas. How do I go about that?


//import com.hamoid.*;

// create a new VideoExport-object
//VideoExport videoExport;


int nParticles = 200;
int particleSize = 200;
int maxCounter = 150;
Particle[] particles;
//ArrayList<ArrayList<PVector>> lines;
ArrayList<emergentLines> lines;


void setup() {
	background(255);
	size(2000, 2000);
	stroke(0, 80);
	fill(0,90);
	particles = new Particle[nParticles];

	for (int i = 0; i < nParticles; ++i) {
		particles[i] = new Particle();

	}
	background(0);
	/*videoExport = new VideoExport(this);
	videoExport.setFrameRate(5);
	videoExport.startMovie();*/
}

void draw() {
	
	checkCollisions();
	for (Particle p : particles) {
		p.update();
		//print(p.)
		//ellipse(p.pos.x, p.pos.y, 40, 40);
	}

	for (emergentLines l : lines) {
		stroke(l.mapColor(),5);
		l.drawLine();
	}
	//videoExport.saveFrame();

}

void checkCollisions(){
	lines = new ArrayList<emergentLines>();
	//lines = new ArrayList<ArrayList<PVector>>();
	for (Particle p1 : particles) {
		for (Particle p2 : particles) {
			if (p1 != p2) {
				float distance = PVector.dist(p1.pos, p2.pos);
			
			if (distance < particleSize) {
				if (p1.counter == 0) {
					p1.dir.rotate(radians(random(0,90)));
					p1.counter = maxCounter;
				}
				if (p2.counter == 0) {
					p2.dir.rotate(radians(random(0,90)));
					p2.counter = maxCounter;
				}
				emergentLines line = new emergentLines(p1.pos, p2.pos, distance);
				lines.add(line);
				}


			}
		}
	}
}

class emergentLines{
	PVector pt1, pt2;
	float distance;
	emergentLines(PVector point1, PVector point2, float dist){
		pt1 = point1;
		pt2 = point2;
		distance = dist;
	}

	void drawLine() {
		line(pt1.x, pt1.y, pt2.x, pt2.y);
	}

	color mapColor() {
		int greenColor = int(map(distance, 0, particleSize, 55, 65));
		color lineCOlor = color(0, greenColor, 255);
		//color lineCOlor = int(map(distance, 0, particleSize, 0, 255));
		return lineCOlor;
	}
}

class Particle{
	float x = random(width);
	float y = random(height);
	int counter = 0;
	PVector pos = new PVector(x, y);
	PVector dir = new PVector(random(-5,5), random(-5,5));


	void update(){
		pos.add(dir);
		if (pos.x > width || pos.x < 0) {
			pos.x = width/2;
		}
		if (pos.y > height || pos.y < 0) {
			pos.y = height/2;
		}
		if (counter > 0) {
			counter -= 1;
		}
	}

}
/*
void keyPressed() {
  if (key == 'q') {
    videoExport.endMovie();
    exit();
  }
}*/
1 Like

please format the code :slight_smile:

2 Likes

To get you started…

Trigonometry:

Snippet:

  float r = random(100, 200);
  float th = random(0, TAU);
  float x1 = r*cos(th); 
  float y1 = r*sin(th);

Plot:

:)

1 Like