2

OpenGLでゲームを作成しようとしていますが、カメラを動かしたいと思っています。私はこのコードを使用してそれを行いました:

t.calculations(&t1, 5.54, 1.54, 10, 10, 1);
t.calculations(&t2, 5.54, 1.54, 10, 10, 1);
t.calculations(&t3, 5.54, 1.54, 10, 10, 1);
t.calculations(&t4, 5.54, 1.54, 10, 10, 1);
t.calculations(&t5, 5.54, 1.54, 10, 10, 1);
t.calculations(&t6, 5.54, 1.54, 10, 10, 1);
t.calculations(&t7, 5.54, 1.54, 10, 10, 1);

t.calculations(&t8, 5.54, 1.54, 10, 10, 1);
t.calculations(&t9, 5.54, 1.54, 10, 10, 1);
t.calculations(&t10, 5.54, 1.54, 10, 10 ,1);
t.calculations(&t11, 5.54, 1.54, 10, 10, 1);
t.calculations(&t12, 5.54, 1.54, 10, 10, 1);
t.calculations(&t13, 5.54, 1.54, 10, 10, 1);
t.calculations(&t14, 5.54, 1.54, 10, 10, 1);
t.calculations(&t15, 5.54, 1.54, 10, 10, 1);
t.calculations(&t16, 5.54, 1.54, 10, 10, 1);
t.calculations(&t17, 5.54, 1.54, 10, 10, 1);
t.calculations(&t18, 5.54, 1.54, 10, 10, 1);

しかし、ご覧のとおり、これはコードの過度の繰り返しのように見えます。上記の方法の代わりに次の方法を使用しようとしました。

for (int i = 1; i < 19; i++) {
   t.calculations(&t+i, 5.54, 1.54, 10, 10, 1);
}

しかし、それは機能していません。誰かが私に別の解決策を教えてもらえますか?

4

1 に答える 1

2

t i変数がすべて同じ型であり、型がdoubleであると仮定すると、次のようになります。

// The following sentence declares an array initialized with the 18 t variables
// think of this array as a slot container of values, the following is just syntax
// to declare and initialize the array 
// IMPORTANT: Once the array is initialized, you can't modify its structure, you can 
// replace the content of every cell, but, you can add neither remove elements from it
double t[] = { t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16, t17, t18 };

// Then, you can read every cell of the array using the [] operator like this:
// (Another important hint, arrays starts from '0')
for (int 0 = 1; i < 18; i++) {
   // You take the address of every ti variable stored in each "cell" of the array 
   t.calculations(&t[i], 5.54, 1.54, 10, 10, 1);
}

または、冗長性の低い構文を使用すると(ただし、かなり複雑になります)、上記のコードは次のように表すことができます。

for (int i = 0; i < 18; i++) {
   t.calculations(t + i, 5.54, 1.54, 10, 10, 1);
}

詳細については、c /c++のアレイのオンラインドキュメントとチュートリアルを確認してください。同様の構文が他の言語で広く使用されています

于 2012-12-24T02:15:31.140 に答える