#include "widget.h"
#include "mainwindow.h"
#include <QPainter>
#include <QPen>
#include <QColor>
#include <Qt>
#include "matrix.h"
#define o m.mObject
//#define d double
Widget::Widget(QWidget *parent):QWidget(parent)
{
this->resize(700,700);
this->firstHeight=this->height();
this->firstWidth=this->width();
isBanY=isBanX=isIsometric=false;
isVisible=true;
prevPoint.setSize(4);
center.setSize(4);
currPoint.setSize(4);
double R= this->width()/6;
for(int i=0;i<13;i++){
o[i][0]=R*sin(2*M_PI*i/13);//+this->width()/2;
o[i][1]=0;
o[i][2]=R*cos(2*M_PI*i/13);
o[i][3]=1;
};
o[13][0]=0;//this->width()/2;
o[13][1]=this->height()/2;
o[13][2]=0;
o[13][3]=1;
/*
m.mObject[0][0]=this->width()/3;
m.mObject[0][1]=this->height()*2/3;
m.mObject[1][0]=this->width()*2/3;
m.mObject[1][1]=this->height()*2/3;
m.mObject[2][0]=this->width()*2/3;
m.mObject[2][1]=this->height()/3;
m.mObject[3][0]=this->width()/3;
m.mObject[3][1]=this->height()/3;
m.mObject[0][2]=0;
m.mObject[1][2]=0;
m.mObject[2][2]=0;
m.mObject[3][2]=0;
m.mObject[4][0]=this->width()/3;
m.mObject[4][1]=this->height()*2/3;
m.mObject[5][0]=this->width()*2/3;
m.mObject[5][1]=this->height()*2/3;
m.mObject[6][0]=this->width()*2/3;
m.mObject[6][1]=this->height()/3;
m.mObject[7][0]=this->width()/3;
m.mObject[7][1]=this->height()/3;
m.mObject[4][2]=this->width()/3;
m.mObject[5][2]=this->width()/3;
m.mObject[6][2]=this->width()/3;
m.mObject[7][2]=this->width()/3;
for(int i=0;i<8;i++)
m.mObject[i][3]=1;
*/
center[0]=o[13][0];
center[1]=o[13][1]/2;//(o[0][1]+o[3][1])/2;
center[2]=0;
center[3]=1;
m.Init_mchangecoord(this->height());
long t1=center[0];
long t2=center[1];
long t3=center[2];
QPointF point(1,1);
currPoint=center;
pen.setColor(Qt::red);
pen.setWidth(5);
//painter->setPen(*pen);
curr_direct= 1;
mNorm.setSize(14,4);
// CVector normal(4);
for(int i=0;i<12;i++){
mNorm[i]=m.VProizv(o[i],o[i+1]) + m.VProizv(o[13],o[i]) + m.VProizv(o[i+1],o[13]);
};
mNorm[12]=m.VProizv(o[12],o[0]) + m.VProizv(o[13],o[12]) + m.VProizv(o[0],o[13]);
mNorm[13]=m.VProizv(o[2],o[1]) + m.VProizv(o[1],o[0]) + m.VProizv(o[0],o[2]);
}
void Widget::mousePressEvent(QMouseEvent* mouse){
QPointF point(mouse->x(),mouse->y());
prevPoint=m.Change_coord(point);
};
void Widget::mouseReleaseEvent(QMouseEvent* mouse){
};
void Widget::mouseMoveEvent(QMouseEvent* mouse){
QPointF point(mouse->x(),mouse->y());
currPoint=m.Change_coord(point);
m.Move((-1) * center,m.mObject,1);
{
double a1=m.mObject[0][0];//=this->width()/3;
double a2=m.mObject[0][1];//=this->height()*2/3;
double b1=m.mObject[1][0];//=this->width()*2/3;
double b2=m.mObject[1][1];//=this->height()*2/3;
double c1=m.mObject[2][0];//=this->width()*2/3;
double c2=m.mObject[2][1];//=this->height()/3;
double d1=m.mObject[3][0];
double d2=m.mObject[3][1];
double a3=m.mObject[0][2];
double b3=m.mObject[1][2];
double c3=m.mObject[2][2];
double d3=m.mObject[3][2];
double e1=m.mObject[4][0];//=this->width()/3;
double e2=m.mObject[4][1];//=this->height()*2/3;
double f1=m.mObject[5][0];//=this->width()*2/3;
double f2=m.mObject[5][1];//=this->height()*2/3;
double g1=m.mObject[6][0];//=this->width()*2/3;
double g2=m.mObject[6][1];//=this->height()/3;
double h1=m.mObject[7][0];//=this->width()/3;
double h2=m.mObject[7][1];//=this->height()/3;
double e3=m.mObject[4][2];//=this->width()/3;
double f3=m.mObject[5][2];//=this->width()/3;
double g3=m.mObject[6][2];//=this->width()/3;
double h3=m.mObject[7][2];//=this->width()/3;
double i1=m.mObject[8][0];//=this->width()/3;
double i2=m.mObject[8][1];//=this->height()*2/3;
double j1=m.mObject[9][0];//=this->width()*2/3;
double j2=m.mObject[9][1];//=this->height()*2/3;
double k1=m.mObject[10][0];//=this->width()*2/3;
double k2=m.mObject[10][1];//=this->height()/3;
double l1=m.mObject[11][0];
double l2=m.mObject[11][1];
double i3=m.mObject[8][2];
double j3=m.mObject[9][2];
double k3=m.mObject[10][2];
double l3=m.mObject[11][2];
double m1=m.mObject[12][0];//=this->width()/3;
double m2=m.mObject[12][1];//=this->height()*2/3;
double n1=m.mObject[13][0];//=this->width()*2/3;
double n2=m.mObject[13][1];
double m3=m.mObject[12][2];//=this->width()/3;
double n3=m.mObject[13][2];//=this->width()/3;
double na1=mNorm[0][0];//=this->width()/3;
double na2=mNorm[0][1];//=this->height()*2/3;
double nb1=mNorm[1][0];//=this->width()*2/3;
double nb2=mNorm[1][1];//=this->height()*2/3;
double nc1=mNorm[2][0];//=this->width()*2/3;
double nc2=mNorm[2][1];//=this->height()/3;
double nd1=mNorm[3][0];
double nd2=mNorm[3][1];
double na3=mNorm[0][2];
double nb3=mNorm[1][2];
double nc3=mNorm[2][2];
double nd3=mNorm[3][2];
double ne1=mNorm[4][0];//=this->width()/3;
double ne2=mNorm[4][1];//=this->height()*2/3;
double nf1=mNorm[5][0];//=this->width()*2/3;
double nf2=mNorm[5][1];//=this->height()*2/3;
double ng1=mNorm[6][0];//=this->width()*2/3;
double ng2=mNorm[6][1];//=this->height()/3;
double nh1=mNorm[7][0];//=this->width()/3;
double nh2=mNorm[7][1];//=this->height()/3;
double ne3=mNorm[4][2];//=this->width()/3;
double nf3=mNorm[5][2];//=this->width()/3;
double ng3=mNorm[6][2];//=this->width()/3;
double nh3=mNorm[7][2];//=this->width()/3;
double ni1=mNorm[8][0];//=this->width()/3;
double ni2=mNorm[8][1];//=this->height()*2/3;
double nj1=mNorm[9][0];//=this->width()*2/3;
double nj2=mNorm[9][1];//=this->height()*2/3;
double nk1=mNorm[10][0];//=this->width()*2/3;
double nk2=mNorm[10][1];//=this->height()/3;
double nl1=mNorm[11][0];
double nl2=mNorm[11][1];
double ni3=mNorm[8][2];
double nj3=mNorm[9][2];
double nk3=mNorm[10][2];
double nl3=mNorm[11][2];
double nm1=mNorm[12][0];//=this->width()/3;
double nm2=mNorm[12][1];//=this->height()*2/3;
double nn1=mNorm[13][0];//=this->width()*2/3;
double nn2=mNorm[13][1];
double nm3=mNorm[12][2];//=this->width()/3;
double nn3=mNorm[13][2];//=this->width()/3;
ni3=mNorm[8][2];
}
// m.prSIsometric(m.mObject,0);
if(!isBanY){
m.Rotate('y',m.mObject,(currPoint[0]-prevPoint[0])/150,1);
};
if(!isBanX){
m.Rotate('x',m.mObject,(currPoint[1]-prevPoint[1])/150,0);
};
// m.prSIsometric(m.mObject,1);
m.Move((-1) * center,m.mObject,0);
for(int i=0;i<12;i++){
mNorm[i]=m.VProizv(o[i],o[i+1]) + m.VProizv(o[13],o[i]) + m.VProizv(o[i+1],o[13]);
};
mNorm[12]=m.VProizv(o[12],o[0]) + m.VProizv(o[13],o[12]) + m.VProizv(o[0],o[13]);
mNorm[13]=m.VProizv(o[2],o[1]) + m.VProizv(o[1],o[0]) + m.VProizv(o[0],o[2]);
prevPoint=currPoint;
this->update();
};
void Widget::paintEvent(QPaintEvent*)
{
// for(int i=)
typedef double d;
double k,k1;
if(this->height()>this->width()){
k=(double)((double)this->width()/(double)this->firstWidth);
k1=(double)((double)this->width()/(double)this->firstWidth);
}
else{
k=(double)((double)this->height()/(double)this->firstHeight);
k1=(double)((double)this->height()/(double)this->firstHeight);
};
//if(((this->height()==this->firstHeight)&&(this->width()==this->firstWidth))){
double W=this->firstHeight;
double H=this->height();
QPainter painter(this);
m.Change_coord(1,center);
painter.setRenderHint(QPainter::Antialiasing,1);
painter.setPen(pen);
//painter.drawPoint(center[0]+this->width()/2,center[1]-this->height()/4);
if(!isIsometric){
m.ChCoord(o,1);
int G=o[0][1];
int Y=center[1];
int H=this->height();
//for(int i=0;i<m.mObject.getHeight();i++)
//m.mObject[i][1]=-center[1];
for(int i=1;i<13;i++){
if(isVisible || (mNorm[i-1][2]>0)){
}
else{
pen.setColor(Qt::black);
painter.setPen(pen);
painter.drawLine(o[i-1][0]*k1+(d)this->width()/(d)2,o[i-1][1]*k+(d)this->height()/(d)4,o[i][0]*k1+(d)this->width()/(d)2,o[i][1]*k+(d)this->height()/(d)4);
painter.drawLine(o[i][0]*k1+(d)this->width()/(d)2,o[i][1]*k+(d)this->height()/(d)4,o[13][0]*k1+(d)this->width()/(d)2,o[13][1]*k+(d)this->height()/(d)4);
painter.drawLine(o[13][0]*k1+(d)this->width()/(d)2,o[13][1]*k+(d)this->height()/(d)4,o[i-1][0]*k1+(d)this->width()/(d)2,o[i-1][1]*k+(d)this->height()/(d)4);
pen.setColor(Qt::red);
painter.setPen(pen);
};
};
if(isVisible || (mNorm[12][2]>0)){
}
else{
pen.setColor(Qt::black);
painter.setPen(pen);
painter.drawLine(o[12][0]*k1+(d)this->width()/(d)2,o[12][1]*k+(d)this->height()/(d)4,o[0][0]*k1+(d)this->width()/(d)2,o[0][1]*k+(d)this->height()/(d)4);
painter.drawLine(o[0][0]*k1+(d)this->width()/(d)2,o[0][1]*k+(d)this->height()/(d)4,o[13][0]*k1+(d)this->width()/(d)2,o[13][1]*k+(d)this->height()/(d)4);
painter.drawLine(o[13][0]*k1+(d)this->width()/(d)2,o[13][1]*k+(d)this->height()/(d)4,o[12][0]*k1+(d)this->width()/(d)2,o[12][1]*k+(d)this->height()/(d)4);
pen.setColor(Qt::red);
painter.setPen(pen);
};
if(isVisible || (mNorm[13][2]>0)){
}
else{
pen.setColor(Qt::black);
painter.setPen(pen);
for(int i=0;i<12;i++)
painter.drawLine(o[i][0]*k1+(d)this->width()/(d)2,o[i][1]*k+(d)this->height()/(d)4,o[i+1][0]*k1+(d)this->width()/(d)2,o[i+1][1]*k+(d)this->height()/(d)4);
painter.drawLine(o[12][0]*k1+(d)this->width()/(d)2,o[12][1]*k+(d)this->height()/(d)4,o[0][0]*k1+(d)this->width()/(d)2,o[0][1]*k+(d)this->height()/(d)4);
pen.setColor(Qt::red);
painter.setPen(pen);
};
for(int i=1;i<13;i++){
if(isVisible || (mNorm[i-1][2]>0)){
painter.drawLine(o[i-1][0]*k1+(d)this->width()/(d)2,o[i-1][1]*k+(d)this->height()/(d)4,o[i][0]*k1+(d)this->width()/(d)2,o[i][1]*k+(d)this->height()/(d)4);
painter.drawLine(o[i][0]*k1+(d)this->width()/(d)2,o[i][1]*k+(d)this->height()/(d)4,o[13][0]*k1+(d)this->width()/(d)2,o[13][1]*k+(d)this->height()/(d)4);
painter.drawLine(o[13][0]*k1+(d)this->width()/(d)2,o[13][1]*k+(d)this->height()/(d)4,o[i-1][0]*k1+(d)this->width()/(d)2,o[i-1][1]*k+(d)this->height()/(d)4);
}
else{
}
};
if(isVisible || (mNorm[12][2]>0)){
painter.drawLine(o[12][0]*k1+(d)this->width()/(d)2,o[12][1]*k+(d)this->height()/(d)4,o[0][0]*k1+(d)this->width()/(d)2,o[0][1]*k+(d)this->height()/(d)4);
painter.drawLine(o[0][0]*k1+(d)this->width()/(d)2,o[0][1]*k+(d)this->height()/(d)4,o[13][0]*k1+(d)this->width()/(d)2,o[13][1]*k+(d)this->height()/(d)4);
painter.drawLine(o[13][0]*k1+(d)this->width()/(d)2,o[13][1]*k+(d)this->height()/(d)4,o[12][0]*k1+(d)this->width()/(d)2,o[12][1]*k+(d)this->height()/(d)4);
}
else{
};
if(isVisible || (mNorm[13][2]>0)){
for(int i=0;i<12;i++)
painter.drawLine(o[i][0]*k1+(d)this->width()/(d)2,o[i][1]*k+(d)this->height()/(d)4,o[i+1][0]*k1+(d)this->width()/(d)2,o[i+1][1]*k+(d)this->height()/(d)4);
painter.drawLine(o[12][0]*k1+(d)this->width()/(d)2,o[12][1]*k+(d)this->height()/(d)4,o[0][0]*k1+(d)this->width()/(d)2,o[0][1]*k+(d)this->height()/(d)4);
}
else{
};
//for(int i=0;i<m.mObject.getHeight();i++)
// m.mObject[i][1]=+center[1];
m.ChCoord(o,0);
}
else{
CMatrix mIsoMetric(m.prIsometric(o));
CMatrix mNormIso(mNorm);
for(int i=0;i<12;i++){
mNormIso[i]=m.VProizv(mIsoMetric[i],mIsoMetric[i+1]) + m.VProizv(mIsoMetric[13],mIsoMetric[i])
+ m.VProizv(mIsoMetric[i+1],mIsoMetric[13]);
};
mNormIso[12]=m.VProizv(mIsoMetric[12],mIsoMetric[0]) + m.VProizv(mIsoMetric[13],mIsoMetric[12])
+ m.VProizv(mIsoMetric[0],mIsoMetric[13]);
mNormIso[13]=m.VProizv(mIsoMetric[2],mIsoMetric[1]) + m.VProizv(mIsoMetric[1],mIsoMetric[0])
+ m.VProizv(mIsoMetric[0],mIsoMetric[2]);
m.ChCoord(mIsoMetric,1);
for(int i=1;i<13;i++){
if(isVisible || (mNormIso[i-1][2]>0)){
painter.drawLine(mIsoMetric[i-1][0]+this->width()/2,mIsoMetric[i-1][1]-this->height()/4,mIsoMetric[i][0]+this->width()/2,mIsoMetric[i][1]-this->height()/4);
painter.drawLine(mIsoMetric[i][0]+this->width()/2,mIsoMetric[i][1]-this->height()/4,mIsoMetric[13][0]+this->width()/2,mIsoMetric[13][1]-this->height()/4);
painter.drawLine(mIsoMetric[13][0]+this->width()/2,mIsoMetric[13][1]-this->height()/4,mIsoMetric[i-1][0]+this->width()/2,mIsoMetric[i-1][1]-this->height()/4);
};
};
if(isVisible || (mNormIso[12][2]>0)){
painter.drawLine(mIsoMetric[12][0]+this->width()/2,mIsoMetric[12][1]-this->height()/4,mIsoMetric[0][0]+this->width()/2,mIsoMetric[0][1]-this->height()/4);
painter.drawLine(mIsoMetric[0][0]+this->width()/2,mIsoMetric[0][1]-this->height()/4,mIsoMetric[13][0]+this->width()/2,mIsoMetric[13][1]-this->height()/4);
painter.drawLine(mIsoMetric[13][0]+this->width()/2,mIsoMetric[13][1]-this->height()/4,mIsoMetric[12][0]+this->width()/2,mIsoMetric[12][1]-this->height()/4);
};
if(isVisible || (mNormIso[13][2]>0)){
for(int i=0;i<12;i++)
painter.drawLine(mIsoMetric[i][0]+this->width()/2,mIsoMetric[i][1]-this->height()/4,mIsoMetric[i+1][0]+this->width()/2,mIsoMetric[i+1][1]-this->height()/4);
painter.drawLine(mIsoMetric[12][0]+this->width()/2,mIsoMetric[12][1]-this->height()/4,mIsoMetric[0][0]+this->width()/2,mIsoMetric[0][1]-this->height()/4);
};
};
m.Change_coord(0,center);
//};
}