# Probabilistic based drawing working for single object, breaking for >1 objects

Hi,

I am trying to write a program to render a brick-wall like pattern. At a high level, fixed length paths will be created as the wall ‘grows,’ which new paths being added as the current ones become ‘filled’ with the probabilistic based cross hatching.

This works great for a single Path, but as soon as the ArrayList contains more than a single object, only a single path (sometimes different from any paths in the ArrayList) is drawn. This is very confusing to me. I have done some internet research and come up with little pertaining to this specific problem.

As a test case, comment out 2 of the paths in the Wall constructor and run the program. A single fuzzy path will be drawn. Now, include either of (or both of) the others and run again.

I have also ran this with several Path objects not held in an ArrayList, with the looping done in draw(), with the same effect. Further, I have tried with more simplistic display() functions and still am unable to generate the expected behaviror.

Thanks

``````//dimensions for each brick (height, width, num)
float bh = 30;
float bw = 100;
int num_bricks = 10;

Wall wall;

void setup(){
size(500, 400);
background(255);
wall = new Wall(6);
}

void draw(){
wall.update();
wall.display();
}

class Path{

PVector start;
PVector end;
boolean filled;
PVector current_loc;
float r;
PVector velocity;

Path(PVector a, PVector b, float thickness){
start=a;
end=b;
filled = false;
r = thickness;
velocity = PVector.sub(b,a).normalize();
current_loc = a;
}

void update(){
//check if the path is done yet, and if not move one more step
if(current_loc.dist(end)==0){
filled=true;
velocity.mult(0);
}
else{
}
}

void display(){
//rotate the matrix and draw a line perpendicular to direction of travel
float theta = PI/2;
strokeWeight(5);
stroke(0);
pushMatrix();
translate(current_loc.x, current_loc.y);
rotate(theta);
float len = randomGaussian()*r/5+r;
line(-len,0,len,0);
popMatrix();
}

}

class Wall{

//collection of paths to create the wall from
ArrayList<Path> paths;
PVector start;
//how thick each line of separation should be
float thickness;

Wall(float thickness){
thickness = thickness;
start = new PVector(width/2, height/2);
paths = new ArrayList<Path>();
//manually initialize a T shape composed of 3 paths
}

void update(){
for(Path p:paths){
if(p.filled == false){
p.update();
}
}
}

void display(){
for(Path p:paths){
p.display();
}
}
}

``````

You have this before setup ()

It’s confusing. Delete this.

Fair enough, forgot to clean that section up. Thanks - any thoughts on the problem itself?

1 Like

??

I think that this is correct.

But maybe try new PVector(bw/2, 0) instead

I thought it could be related…

PVector is a bit tricky sometimes

I don’t have a computer here but the way you wrote this could change start value maybe, what is bad. Maybe start.add(new PVector(… is better

Or try start1=start.copy(); repeat for start2 and start3 and work with those

You are right, something wrong with the way I was initializing, and the value for start was being changed. can you explain why that is happening? it seems the issue was actually located in the first usage of start (not the addition):

``````paths.add(new Path(start.copy(), PVector.add(start, new PVector(bw/2, 0)), thickness));
``````

and

``````paths.add(new Path(start.copy(), start.copy.add(new PVector(bw/2, 0)), thickness));
``````

worked, but not

``````paths.add(new Path(start,  PVector.add(start, new PVector(bw/2, 0)), thickness));
``````

any help in understanding why so I do not replicate the error would be much appreciated

1 Like

As I assumed when it doesn’t work, you accidentally changed the value of start permanently which affected the next path where you use start with a new value. Bad.

Obviously PVector.add() not only returns a PVector but also alters the first parameter permanently.

There is a similar class of errors where we pass the pointer to a constructor instead of its values which leads to nasty errors. Here the use of .copy() is recommended.

1 Like