How to draw a triangle shape using for loop from array

var door=[403, 402, 427, 646, 639, 830, 809, 844, 802, 683, 552, 629, 712, 783, 415, 561, 562, 751, 680, 626, 701, 838, 322, 468, 625];

for(b=0; b<door.length; b++) {           
  triangle(x1,y1,x2,y2,x3,y3);
}

how do I set x1,y1,x2,y2,x3,y3 above to draw triangle from array

1 Like

That depends on what those values mean?

If they are the position of the triangle, 6 values would make one triangle, so your loop can’t go on from 0 to door.length, cause that‘s 6 times as many triangles as there are coordinates.

Then you just have to set the first triangles (x1, y1 x2… and so on) manually to the corresponding index of the door[].

Now you just have to make that index increase with each loop. So you need to have the variable b as a part of your index calculation.

But just adding b would increase the index by 1 each loop, which is wrong… it would take the y1 of the first triangle and use it as x1 for the second and so on… so you need to increase by more than 1. :wink:

1 Like
for(b=0;b<door.length; b++){

triangle(door[b+4],door[b+5],door[b+4],door[b+2],door[b+2],door[b+3]);
   
// Is it gonna loop through all the elements from array     
}

First of all, explain how the door[]‘s values are related to the triangles you want to draw?
If they are the triangles vertex coordinates, then the length is wrong… it’s not divisible by 6.

Else, it might be the origin of the triangle, in that case you just can use the for loop like you had it and use the door[b] variable as the center…