bit manipulation - C++ Points of Vertices in Cuboid (Bitwise AND) -
i'm trying calculate points in cuboid given centre (which vector3) , lengths of sides along x, y , z axis. found following on math.stackexchange.com: https://math.stackexchange.com/questions/107778/simplest-equation-for-drawing-a-cube-based-on-its-center-and-or-other-vertices says can use following formulae:
constructor world class is:
world::world(vector3 o, float d1, float d2, float d3) : origin(o) { // if consider edge length d, need find r such // 2r = d in order calculate positions of each vertex in world. float r1 = d1 / 2, r2 = d2 / 2, r3 = d3 / 2; (int = 0; < 8; i++) { /* sets vertices of cube. * * @see http://bit.ly/1cc2rpg */ float x = o.getx() + (std::pow(-1, i&1) * r1), y = o.gety() + (std::pow(-1, i&2) * r2), z = o.getz() + (std::pow(-1, i&4) * r3); points[i] = vector3(x, y, z); std::cout << points[i] << "\n"; } }
and passing following parameters constructor:
vector3 o(0, 0, 0); world w(o, 100.f, 100.f, 100.f);
the coordinates being output 8 vertices are:
(50, 50, 50) (-50, 50, 50) (50, 50, 50) (-50, 50, 50) (50, 50, 50) (-50, 50, 50) (50, 50, 50) (-50, 50, 50)
which cannot correct. guidance appreciated!
the problem lies in bitwise &
inside pow
calls: in y
, z
components, return 0 , 2 or 4, respectively. -1^2 = -1^4 = 1, why sign of these components positive. try (i&2)!=0
or (i&2) >> 1
y component instead. same goes z component.
Comments
Post a Comment