Very long class code

I want to connect every point with a line using a neighbor system, and I’m using a lot of class variables that don’t work. I don’t know if there is a limit but here’s the code: https://www.openprocessing.org/sketch/1050682
line 62
Can someone help me figure out why it doesn’t work?

When you instantiate your net[][] 2D array within setup() the order is y & x. :goal_net:

But for unknown reasons within draw() you choose to iterate over net[][] by x & y order instead! :astonished:

Anyways, I’ve refactored your triple loop within callback draw() for more clarity: :loop:

function draw() {
  background(255);

  for (const row of net)
    for (const { x, y, neighbors } of row)
      for (const { x: nx, y: ny } of neighbors)
        line(x, y, nx, ny);
}
1 Like

I don’t think I did, when i create a new point I put it in x y order and it still renders correctly, will check the code bellow.

This is your double loop within callback setup():

	for(y = 0; y < ace.height; y = y + 1){
		for(x = 0; x < ace.width; x = x + 1){

And this is your double loop within callback draw():

	for(x = 0; x < cols; x = x + 1){
		for(y = 0; y < rows; y = y + 1){

Their order is clearly swapped! :grimacing:

oh there, will it matter though?

B/c the number of rows & columns, which are based on the p5.Image’s height & width properties, aren’t the same.

Therefore you end up accessing indices outta range b/c the number of rows is less than the number of columns for net[][].

BtW, my refactored loop version doesn’t care about the order the 2D array was created. :wink:

Thx, I dont fully understand how it works but it does, never worked like that. thx <3

Read about for..of loop & “object destructuring assignment” on the links below: :link:

1 Like