# Confine particles to a space

**URL:** <https://discourse.processing.org/t/confine-particles-to-a-space/31813>\
**Category:** Coding Questions\
**Created:** [August 20, 2021, 6:03pm UTC](https://discourse.processing.org/t/confine-particles-to-a-space/31813 "2021-08-20T18:03:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![seldomcoder](https://avatars.discourse-cdn.com/v4/letter/s/e99b99/32.png) [@seldomcoder](https://discourse.processing.org/u/seldomcoder)\
**Post date:** [August 20, 2021, 6:03pm UTC](https://discourse.processing.org/t/confine-particles-to-a-space/31813/1 "2021-08-20T18:03:32Z")

</div>

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?

```auto

//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();
  }
}*/

```

---

<div class="post-metadata">

**Author:** ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)\
**Post date:** [August 20, 2021, 6:23pm UTC](https://discourse.processing.org/t/confine-particles-to-a-space/31813/2 "2021-08-20T18:23:30Z")

</div>

please format the code 🙂

[https://www.khm.de/~n.hieda/discourse-code.mp4](https://www.khm.de/~n.hieda/discourse-code.mp4)

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [August 20, 2021, 7:16pm UTC](https://discourse.processing.org/t/confine-particles-to-a-space/31813/3 "2021-08-20T19:16:34Z")

</div>

> [@seldomcoder](#):
>
> 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?

To get you started…

Trigonometry:  
[https://processing.org/tutorials/trig](https://processing.org/tutorials/trig)

Snippet:

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

```

Plot:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/6/611ad003c5618d8a597238b1b4d1d19f362ffdcf.png)

`:)`
