1

At the moment I have this:

Shape* shapeArray[12];
for (int i = 0; i < vertices.size(); i += 3) {
    for (int j = 0; j < sizeof(shapeArray) / sizeof(shapeArray[0]); j++) {
        shapeArray[j] = new Triangle(vertices[i], vertices[i + 1], vertices[i + 2], vec3(0.8, 0.0, 1.0));
    }
}

I'm trying to create a cube out of triangles. I want to increment the j value by 1 each time and increment the i value by 3 each time resulting in something like this (if it were hard coded):

Shape* shapeArray[12];
shapeArray[0] = new Triangle(vertices[0], vertices[1], vertices[2], vec3(0.8, 0.0, 1.0));       
shapeArray[1] = new Triangle(vertices[3], vertices[4], vertices[5], vec3(0.8, 0.0, 1.0));       
shapeArray[2] = new Triangle(vertices[6], vertices[7], vertices[8], vec3(0.8, 0.0, 1.0));
shapeArray[3] = new Triangle(vertices[9], vertices[10], vertices[11], vec3(0.8, 0.0, 1.0));
shapeArray[4] = new Triangle(vertices[12], vertices[13], vertices[14], vec3(0.8, 0.0, 1.0));
shapeArray[5] = new Triangle(vertices[15], vertices[16], vertices[17], vec3(0.8, 0.0, 1.0));
shapeArray[6] = new Triangle(vertices[18], vertices[19], vertices[20], vec3(0.8, 0.0, 1.0));
shapeArray[7] = new Triangle(vertices[21], vertices[22], vertices[23], vec3(0.8, 0.0, 1.0));
shapeArray[8] = new Triangle(vertices[24], vertices[25], vertices[26], vec3(0.8, 0.0, 1.0));
shapeArray[9] = new Triangle(vertices[27], vertices[28], vertices[29], vec3(0.8, 0.0, 1.0));
shapeArray[10] = new Triangle(vertices[30], vertices[31], vertices[32], vec3(0.8, 0.0, 1.0));
shapeArray[11] = new Triangle(vertices[33], vertices[34], vertices[35], vec3(0.8, 0.0, 1.0));

However, it isn't giving me the desired result. I am getting 3 lots of the j value hence not drawing the cube properly.

How could I solve this issue with for loops? Or would I need to change from using an array to store the triangles. Thank you.

1
  • 1
    i doesn't change in your j loop; looks like you don't really need the inner loop, but can get away with just incrementing the index which you want to set Commented Oct 17, 2016 at 10:40

3 Answers 3

2
for (int i= 0, j= 0; i < vertices.size(); i+= 3, ++j) {
...
}

Something like this?

Sign up to request clarification or add additional context in comments.

2 Comments

Removing i from loop control, and just int i=j*3; might be cleaner.
I don't think that the price for less human readable code is paid by this "optimisation".
2

As pointed out in the comment , your inner loop is not necessary.

you need to iterate over the array and compute the vertice index acordingly when you create a new Triangle.

Shape* shapeArray[12];
for (int i = 0; i < vertices.size(); i += 3) {
        shapeArray[i] = new Triangle(vertices[i * 3], vertices[i * 3 + 1], vertices[i * 3 + 2], vec3(0.8, 0.0, 1.0));
}

Would be a simplier and working implementation.

Comments

1

You can change your for loop so that shapeArray indexes correspond accurately to vertices indexes. I deduced this on basis of your sample output.

Shape* shapeArray[12];
for (int i = 0; i < vertices.size(); i += 3)
{

    shapeArray[(int)i/3] = new Triangle(vertices[i * 3], vertices[i * 3 + 1], vertices[i * 3 + 2], vec3(0.8, 0.0, 1.0));

}

If you increment i by 3 then you will need to divide by 3 for shapeArray index. You do not need an inner loop in this scenario.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.