[英]storing r, g and b components in a 2D array in c++
說,我正在掃描三角形列表,每個三角形都有固定的r,g,b值(顏色)。 我想將每個三角形的r,g,b分量存儲在2D數組中,以后再檢索它們。 如果屏幕的寬度和高度分別為w和h,則可以聲明尺寸為w * h的數組,但是如何在c ++中的某個位置存儲3個分量(r,g,b)?
constexpr std::size_t w = /* some constant */;
constexpr std::size_t h = /* some constant */;
struct Color {
int r, g, b;
};
Color color[w][h];
存儲在結構中
有幾種方法可以使用...可以使用位掩碼和位標志來存儲和檢索2d int數組的值,可以創建一個名為color的自定義結構或類,如下所示:
struct Color
{
float r;
float g;
float b;
};
然后將您的數組聲明為:
Color screenColors[1024][768];
您始終可以將1024和768值聲明為常量整數,並使用它們而不是對值進行硬編碼。
如果您想了解有關位屏蔽的更多信息,我建議您閱讀: http : //www.learncpp.com/cpp-tutorial/3-8a-bit-flags-and-bit-masks/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.