2012-12-24 85 views
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); 
} 

但它不工作。任何人都可以告訴我一個替代方案嗎

+1

你有18不同的變量名'T1 .. t18'?那麼什麼是't'?任何爲什麼'tN'不是數組/矢量? – delnan

+0

我不知道如何在這種情況下使用數組:/和不太熟悉向量或... – Rizwan606

回答

2

假設噸變量都是相同類型的,並且所述類型是雙:

// 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); 
} 

Alternatevely,使用較詳細的語法(但相當複雜雖然),上面的代碼可以是表達如下:

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

欲瞭解更多信息,請檢查在線documentation和數組在c/c + +的教程。類似的語法被廣泛應用於其他語言

+0

謝謝。我已經嘗試過,但不起作用。其實t1,t2,t3等等。是另一個類的變量,而不是'double'類型。你能否看到爲什麼這現在不起作用?我必須在原始類中定義這個數組嗎? – Rizwan606

+0

是,你應該做的是,在該類定義數組如果T 變量是一個類的成員,相應地改變的類型(即,使用浮子數組聲明如果其類型是浮點) – higuaro