#ifndef _cube_h_defined_
#define _cube_h_defined_
class cube {
public:
cube(int s) {
// Create a cube centered at the origin and having sides of length s
// units.
side = s;
center_x = center_y = center_z = 0;
}
void move(int x, int y, int z) {
// Move a cube x units along the x axis, y units along the y axis and z
// units along the z axis. Because the cube is a rigid body, moving the
// center is equivalent to moving the cube.
center_x += x;
center_y += y;
center_z += z;
}
bool interferes_with(cube c) {
// Return true if cube c interferes with this cube, false otherwise.
const int s1 = side/2;
const int s2 = c.side/2;
// Two cubes overlap if and only if the projection of the two cubes
// overlap on all three axes.
return
overlap(center_x - s1, center_x + s1, c.center_x - s2, c.center_x + s2) &&
overlap(center_y - s1, center_y + s1, c.center_y - s2, c.center_y + s2) &&
overlap(center_z - s1, center_z + s1, c.center_z - s2, c.center_z + s2);
}
private:
// The length of this cube's side.
int side;
// The coordinates of this cube's center point.
int center_x, center_y, center_z;
bool overlap(int left1, int right1, int left2, int right2) const {
// Return true if and only if any part of the interval (left1, right1)
// overlaps with any part of the interval (left2, right2). This code
// assumes that left1 <= right1 and left2 <= right2.
return (left2 <= right1) && (left1 <= right2);
}
};
#endif
syntax highlighted by Code2HTML, v. 0.9