我想設置類三角形的三個對象上的第二個和第三個角的值。如果明確完成的,這看起來是這樣的:C++ - for循環中的值未正確設置
triangle_mesh[0].set_vector_point2(vector_anchors[1]);
triangle_mesh[1].set_vector_point3(vector_anchors[1]);
triangle_mesh[1].set_vector_point2(vector_anchors[2]);
triangle_mesh[2].set_vector_point3(vector_anchors[2]);
triangle_mesh[2].set_vector_point2(vector_anchors[3]);
triangle_mesh[0].set_vector_point3(vector_anchors[3]);
這工作!打印這些三角形,我得到(NB第一個彎道已經設置 - 這是不是一個問題):
triangle0 is (0, 0, -1) (1, 0, 0) (-0.5, -0.866025, 0)
triangle1 is (0, 0, -1) (-0.5, 0.866025, 0) (1, 0, 0)
triangle2 is (0, 0, -1) (-0.5, -0.866025, 0) (-0.5, 0.866025, 0)
首先,雖然,這是醜陋的,其次,我有超過它必須推廣到案件三個三角形設置。我的代碼是:
for (int longitude = 0; longitude < num_longitudes; longitude++){
SurfaceVector current_anchor = vector_anchors[1 + longitude];
triangle_mesh[longitude].set_vector_point2(current_anchor);
triangle_mesh[(longitude + 1) % num_longitudes].set_vector_point3(current_anchor);
}
* n.b。 num_longitudes是3 *
我檢查一切我能想到的,但現在當我打印出我的三角形,我得到:
triangle0 is (0, 0, -1) (-0.5, -0.866025, 0) (-0.5, -0.866025, 0)
triangle1 is (0, 0, -1) (-0.5, -0.866025, 0) (-0.5, -0.866025, 0)
triangle2 is (0, 0, -1) (-0.5, -0.866025, 0) (-0.5, -0.866025, 0)
沒有人有任何想法可能是想錯了?
編輯
上的三角形vector_point變量的指針,並設置這樣的:
void set_vector_point1(SurfaceVector vector_point) { vector_point1 = &vector_point; }
啊這是有道理的。但我希望我的三角形的指針指向vector_anchors向量的內存中相同的位置,而不是它的副本(兩者在我的程序中同樣長壽,所以我不想浪費內存存儲空間一切都是兩次)。 – tiswas 2012-03-20 14:32:34
@tiswas,考慮使用一些智能指針,然後 - 像'boost :: shared_ptr' ... – Nim 2012-03-20 14:33:55
嗯,我可以不只是@maitu建議,但直接傳遞vector_anchors的元素,而不是通過中間current_anchor .. ? – tiswas 2012-03-20 14:50:51