ArrayList + background??
in
Programming Questions
•
1 year ago
Hey guys, i am learning array list. So i tested this first code that works fine , and simply transformed my normal array "mid" to an arrayList.
Code with the arrayList:
The thing is that after that is done, the background doesnt upgrade (only for my connection lines), even though the background is already (255)...
Would somebody kind help me with this annoying thing??
Thanks a lot!!!
First code:
- import peasy.*;
- PeasyCam cam;
- int number = 4;
- Agent [] ag = new Agent[number];
- int h= 50;
- void setup() {
- size(800, 600, P3D);
- smooth();
- noFill();
- float ang = TWO_PI / number;
- //CREATES AGENTS
- for (int i=0; i<ag.length; i++) {
- float dx = random(cos(ang*i), cos(ang*i+1));
- float dy = random(sin (ang*i), sin (ang*i+1));
- ag[i] = new Agent(width/2, height/2, h, dx, dy);
- }
- }
- void mousePressed() {
- camera(350.0, 800.0, 100.0, width/2, height/2, 50, 0.0, 0.0, -1.0);
- }
- void draw() {
- background(255);
- stroke(0);
- strokeWeight(1);
- int time= millis();
- int timelimit= 800;
- PVector [] vec = new PVector [ag.length] ;
- //RANDOM POINTS
- fill(255, 0, 0);
- PVector p1= new PVector (200, 100);
- PVector p2= new PVector(600, 200);
- PVector p3= new PVector(350, 400);
- ellipse (p1.x, p1.y, 5, 5);
- ellipse (p2.x, p2.y, 5, 5);
- ellipse (p3.x, p3.y, 5, 5);
- //AGENTS
- for (int i=0; i<ag.length; i++) {
- // ag[i].render();
- if (time < timelimit) {
- ag[i].move();
- }
- }
- //SHAPES
- beginShape();
- stroke(0);
- strokeWeight (1);
- noFill();
- for (int i = 0; i < ag.length; i++) {
- vertex(ag[i].pos.x, ag[i].pos.y, ag[i].pos.z);
- }
- endShape(CLOSE);
- //VECTORS
- for (int i=0 ; i < vec.length-1; i++) {
- vec [i] = new PVector (ag[i+1].pos.x-ag[i].pos.x, ag[i+1].pos.y-ag[i].pos.y);
- }
- vec[vec.length-1] = new PVector (ag[0].pos.x - ag[vec.length-1].pos.x, ag[0].pos.y - ag[vec.length-1].pos.y);
- // CREATES VECTORS IN THE MIDDLE
- PVector [] mid = new PVector [vec.length] ;
- for (int i=0; i< vec.length-1;i++) {
- mid[i]= new PVector ((ag[i].pos.x+ag[i+1].pos.x)/2, (ag[i].pos.y+ag[i+1].pos.y)/2, ag[i].pos.z);
- }
- mid[mid.length-1]= new PVector ((ag[mid.length-1].pos.x+ag[0].pos.x)/2, (ag[mid.length-1].pos.y+ag[0].pos.y)/2, ag[0].pos.z);
- //CONNECTIONS WITH POINTS
- //WITH POINT n.1
- float [] conn1 = new float [mid.length];
- PVector [] proj1 = new PVector [mid.length];
- PVector [] diag1 = new PVector [mid.length];
- PVector [] projection1 = new PVector [mid.length];
- float [] ang1 = new float [mid.length];
- for (int i=0; i< mid.length;i++) {
- conn1[i] = mid[i].dist(p1);
- proj1 [i] = new PVector (mid[i].x, mid[i].y, 0);
- diag1[i]= PVector.sub(mid[i], p1);
- projection1[i]= PVector.sub(proj1[i], p1);
- ang1[i]= PVector.angleBetween(diag1[i], projection1[i]);
- println (degrees(ang1[i]));
- if (conn1[i]<300) {
- if (ang1[i] > radians(10)) {
- strokeWeight(10);
- stroke (255, 0, 0, 100);
- line (mid[i].x, mid[i].y, mid[i].z, p1.x, p1.y, p1.z);
- }
- else {
- strokeWeight(10);
- stroke (0, 255, 0, 100);
- line (mid[i].x, mid[i].y, mid[i].z, p1.x, p1.y, p1.z);
- }
- }
- }
- }
- //======================================================================================
- class Agent {
- // Variablen
- PVector pos;
- PVector direction;
- //der Konstruktor für die Agenten-Klasse
- Agent (float theX, float theY, float theZ, float dx, float dy) {
- pos = new PVector (theX, theY, theZ);
- direction = new PVector (dx, dy);
- }
- void render() {
- for (int i=0; i<number;i++) {
- fill(255, 0, 0);
- ellipse(pos.x, pos.y, 5, 5);
- }
- }
- void move() {
- direction.normalize();
- direction.mult(random(20));
- pos.add(direction);
- }
- }
- int number = 4;
- Agent [] ag = new Agent[number];
- int h= 50;
- ArrayList mid = new ArrayList();
- void setup() {
- size(800, 600, P3D);
- smooth();
- noFill();
- float ang = TWO_PI / number;
- //CREATES AGENTS
- for (int i=0; i<ag.length; i++) {
- float dx = random(cos(ang*i), cos(ang*i+1));
- float dy = random(sin (ang*i), sin (ang*i+1));
- ag[i] = new Agent(width/2, height/2, h, dx, dy);
- }
- }
- void mousePressed() {
- camera(350.0, 800.0, 100.0, width/2, height/2, 50, 0.0, 0.0, -1.0);
- }
- a
- void draw() {
- background(255);
- stroke(0);
- strokeWeight(1);
- int time= millis();
- int timelimit= 800;
- PVector [] vec = new PVector [ag.length] ;
- //AGENTS
- for (int i=0; i<ag.length; i++) {
- // ag[i].render();
- if (time < timelimit) {
- ag[i].move();
- }
- }
- //SHAPES + POINTS
- beginShape();
- stroke(0);
- strokeWeight (1);
- noFill();
- for (int i = 0; i < ag.length; i++) {
- vertex(ag[i].pos.x, ag[i].pos.y, ag[i].pos.z);
- }
- endShape(CLOSE);
- //VECTOR RANDOM POINTS
- fill(255, 0, 0);
- PVector p1= new PVector (200, 100);
- PVector p2= new PVector(600, 200);
- PVector p3= new PVector(350, 400);
- ellipse (p1.x, p1.y, 5, 5);
- ellipse (p2.x, p2.y, 5, 5);
- ellipse (p3.x, p3.y, 5, 5);
- //VECTORS
- for (int i=0 ; i < vec.length-1; i++) {
- vec [i] = new PVector (ag[i+1].pos.x-ag[i].pos.x, ag[i+1].pos.y-ag[i].pos.y);
- }
- vec[vec.length-1] = new PVector (ag[0].pos.x - ag[vec.length-1].pos.x, ag[0].pos.y - ag[vec.length-1].pos.y);
- // CREATES VECTORS IN THE MIDDLE
- for (int i=0; i< vec.length-1;i++) {
- mid.add(new PVector((ag[i].pos.x+ag[i+1].pos.x)/2, (ag[i].pos.y+ag[i+1].pos.y)/2, ag[i].pos.z));
- }
- mid.add(new PVector((ag[ag.length-1].pos.x+ag[0].pos.x)/2, (ag[ag.length-1].pos.y+ag[0].pos.y)/2, ag[0].pos.z));
- //CONNECTIONS
- //WITH POINT n. 1
- float [] conn1 = new float [mid.size()];
- PVector [] proj1 = new PVector [mid.size()];
- PVector [] diag1 = new PVector [mid.size()];
- PVector [] projection1 = new PVector [mid.size()];
- float [] ang1 = new float [mid.size()];
- for (int i=0; i< mid.size();i++) {
- conn1[i] = ((PVector)mid.get(i)).dist(p1);
- proj1 [i] = new PVector (((PVector)mid.get(i)).x, ((PVector)mid.get(i)).y, 0);
- diag1[i]= PVector.sub((PVector)mid.get(i), p1);
- projection1[i]= PVector.sub(proj1[i], p1);
- ang1[i]= PVector.angleBetween(diag1[i], projection1[i]);
- println (degrees(ang1[i]));
- if (conn1[i]<300) {
- if (ang1[i] > radians(10)) {
- strokeWeight(10);
- stroke (255, 0, 0, 100);
- line (((PVector)mid.get(i)).x, ((PVector)mid.get(i)).y, ((PVector)mid.get(i)).z, p1.x, p1.y, p1.z);
- }
- else {
- strokeWeight(10);
- stroke (0, 255, 0, 100);
- line (((PVector)mid.get(i)).x, ((PVector)mid.get(i)).y, ((PVector)mid.get(i)).z, p1.x, p1.y, p1.z);
- }
- }
- }
- }
- //======================================================================================
- class Agent {
- PVector pos;
- PVector direction;
- Agent (float theX, float theY, float theZ, float dx, float dy) {
- pos = new PVector (theX, theY, theZ);
- direction = new PVector (dx, dy);
- }
- void render() {
- for (int i=0; i<number;i++) {
- fill(255, 0, 0);
- ellipse(pos.x, pos.y, 5, 5);
- }
- }
- void move() {
- direction.normalize();
- direction.mult(random(20));
- pos.add(direction);
- }
- }
1