/***************************************************************************
                           cube.cpp
                           -------------------
    copyright            : (C) 2004 by Jos van den Oever
    email                : jos@vandenoever.info
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
#include "cube.h"
#include <QtGui/QPainter>
#include <QtGui/QPixmap>
#include <QtCore/QSize>
#include <Qt3Support/Q3Picture>
#include <cmath>
#include <iostream>

/*   -----> x    

     e----f     /
 |    /| /|    /z
 |  a----b|  |/
\|/  |g-|--h
 y   |  |/
    c----d */
double side1[9] = {-1,-1, 1,  1,-1, 1,  -1, 1, 1};
double side2[9] = { 1,-1, 1,  1,-1,-1,   1, 1, 1};
double side3[9] = { 1,-1,-1, -1,-1,-1,   1, 1,-1};
double side4[9] = {-1,-1,-1, -1,-1, 1,  -1, 1,-1};
double side5[9] = {-1,-1,-1,  1,-1,-1,  -1,-1, 1};
double side6[9] = {-1, 1, 1,  1, 1, 1,  -1, 1,-1};

const double Cube::magicAngle = 180/M_PI*acos(1.0/sqrt(3.0));

Cube::Cube(QWidget *parent, const char *name)
        : Object(parent, name) {
	Side s;

	s.setPoints(side1);
	addSide(s);
	s.setPoints(side2);
	addSide(s);
	s.setPoints(side5);
	addSide(s);
	s.setPoints(side6);
	addSide(s);
	s.setPoints(side4);
	addSide(s);
	s.setPoints(side3);
	addSide(s);
}
int
Cube::heightForWidth(int width) const {
	return width;
}
bool
operator==(const Q3Picture &p1, const Q3Picture &p2) {
	if (p1.size() != p2.size()) {
		return false;
	}
	return !strncmp(p1.data(), p2.data(), p1.size());
}
void
Cube::clear() {
	for (short i=0; i<6; i++) {
		getSide(i).setRotation(0);
	}
}
void
Cube::setOrientation(short o) {
	if (o < 12) {
		setOrient(magicAngle, 315-90*((o/3)%4), 30+120*(o%3));
	} else {
		setOrient(180-magicAngle, 315-90*((o/3)%4), 90+120*(o%3));
	}
}
