Keep coordinates within a vector shape
in
Contributed Library Questions
•
2 years ago
I'm working on a script where some coordinates are moving randomly around checking if other points are close to them and if so draws a line to the those points. This is the first thing I make in processing so the code isn't beautiful but it works.
The next step i would like to introduce to this script to keep these points within a vector shape. My guess is that I would need to use some kind of collision detection to make that work. But I have not been able to find any library or guide that I can understand. Would anyone be so kind and nudge me in the right direction?
Thanks.
Here is my code as it is now:
- //import fullscreen.*;
- import processing.opengl.*;
- //FullScreen fs;
- import controlP5.*;
- ControlP5 controlP5;
- Slider2D s;
- float[] pointx = new float[2000];
- float[] pointy = new float[2000];
- float[] rand1 = new float[2000];
- float[] rand2 = new float[2000];
- float intense;
- float limiter;
- float state;
- float counter;
- int amount;
- float move1;
- float move2;
- float move3;
- float move4;
- float swtop;
- float q;
- int sw;
- int l;
- int e;
- int f;
- int g;
- float fader;
- int opac;
- int divider;
- void setup() {
- controlP5 = new ControlP5(this);
- controlP5.addSlider("fader",0,800,128,20,100,10,100);
- controlP5.addSlider("swtop",1,80,20,40,100,10,100);
- controlP5.addSlider("opac",0,80,3,60,100,10,100);
- smooth();
- size(1500, 1200, OPENGL);
- stroke(0, 80);
- background(255);
- frameRate(20);
- divider = 7;
- opac = 3;
- amount = divider*divider;
- q = 300;
- l = 1;
- e = 1;
- g = 1;
- // fs = new FullScreen(this);
- for(int k = 0; k < (amount); k = k+1) {
- pointx[k] = width/(divider+1)*l;
- if(e < divider) { e = e+1;}
- else
- {
- e = 1;
- l = l+1;
- }
- }
- for(int h = 0; h < (amount); h = h+1) {
- pointy[h] = height/(divider+1)*g;
- if(g < divider) { g = g+1;}
- else
- {
- g = 1;
- }
- }
- }
- void draw(){
- stroke(255, 0);
- fill(255, opac);
- if(sw < swtop) {
- sw = sw + 1;
- }
- else if(sw > swtop) {
- sw = 0;
- }
- else {
- rect(0, 0, width, height);
- sw = 0;
- }
- for (int i = 0; i < amount; i = i+1) {
- for (int n = 0; n < amount; n = n+1) {
- if(q == 300) {
- for (int v = 1; v < amount; v = v + 1) {
- rand1[i] = random(0,1);
- rand2[i] = random(0,1);
- }
- q = 0;
- }
- else
- {
- q = q+1;
- }
- limiter = dist(pointx[i], pointy[i], pointx[n], pointy[n]);
- if (limiter > fader) {
- limiter = fader;
- }
- intense = 100-limiter/(fader/100);
- stroke(0, intense);
- if (i != n) {
- line(pointx[i], pointy[i], pointx[n], pointy[n]);
- }
- if (rand2[i] < 0.5 && pointx[i] < width) {
- pointx[i] = pointx[i] + random(0, 1)/4;
- }
- else if(pointx[i] > 0) {
- pointx[i] = pointx[i] - random(0,1)/4;
- }
- else
- {
- pointx[i] = pointx[i] + random(0, 1)/4;
- }
- if (rand1[i] < 0.5 && pointy[i] < height) {
- pointy[i] = pointy[i] + random(0, 1)/4;
- }
- else if(pointy[i] > 0)
- {
- pointy[i] = pointy[i] - random(0, 1)/4;
- }
- else
- {
- pointy[i] = pointy[i] + random(0, 1)/4;
- }
- };
- };
- }
1