Skip to content

Instantly share code, notes, and snippets.

@paulocanedo
Created April 11, 2019 13:34
Show Gist options
  • Save paulocanedo/46a0392a59dd2a0a563cdcd2ccdd95b2 to your computer and use it in GitHub Desktop.
Save paulocanedo/46a0392a59dd2a0a563cdcd2ccdd95b2 to your computer and use it in GitHub Desktop.
reinterpret_cast.cpp
using Point = std::array<float, 2>;
int main()
{
Point a = {1.1f, 2.1f};
Point b = {1.5f, 2.5f};
Point c = {5.5f, 3.5f};
Point d = {4.5f, 1.5f};
std::vector<Point> polygon;
polygon.push_back(a);
polygon.push_back(b);
polygon.push_back(c);
polygon.push_back(d);
float *values = reinterpret_cast<float*>(polygon.data());
for(size_t i=0; i < polygon.size() * 2; i++) {
std::cout << values[i];
std::cout << (i%2 == 0 ? "," : "\n");
}
std::cout << std::endl;
}
//Output:
//1.1,2.1
//1.5,2.5
//5.5,3.5
//4.5,1.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment