painter::painter(QWidget* parent,Qt::WFlags flags):
QWidget(parent,flags)
{
setGeometry(100,100,640,480);
setBackgroundRole(QPalette::Base);
setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
mMousePress = false;
}
painter::~painter()
{
}
void painter::paintEvent(QPaintEvent* e)
{
mPosList.clear();
int w = width();
int h = height();
float p = 3.14159265f;
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing,true);
for(int i=0;i<5;i++)
{
float delta = p*0.4*i - .5*p;
float cos = cosf(delta)*148;
float sin = sinf(delta)*148;
int x = w/2 + cos;
int y = h/2 + sin;
mPosList.push_back(QPair<int,int>(x,y));
}
renderLine(0,2);
renderLine(1,3);
renderLine(2,4);
renderLine(3,0);
renderLine(4,1);
for(int i=0;i<5;i++)
{
QPair<int,int> pos = mPosList[i];
renderCircle(painter,pos.first,pos.second,48);
}
}
void painter::clcPos(int x,int y,int r)
{
mPosList.push_back(QPair<int,int>(x,y));
}
void painter::renderCircle(QPainter& painter0,int x,int y,int r)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing,true);
painter.setPen(QPen(Qt::blue,2,Qt::SolidLine));
painter.setBrush(QBrush(Qt::green,Qt::SolidPattern));
painter.drawRoundRect(x-r/2,y-r/2,r,r,99,99);
}
void painter::mousePressEvent(QMouseEvent* e)
{
if(e->button() == Qt::LeftButton)
{
mMousePress = true;
int x = e->x();
int y = e->y();
const int off2 = 48*48;
int m = 9999;
int offset = min(x,y,m);
if(m < 36*36)
std::cout<<"here:"<<offset<<std::endl;
}
}
int painter::min(int x,int y,int& m)
{
int offset = 0;
for(int i=0;i<5;i++)
{
QPair<int,int> pos(mPosList[i]);
int len = length(x,y,pos.first,pos.second);
if(len < m)
{
m = len;
offset = i;
}
}
return offset;
}
void painter::mouseReleaseEvent(QMouseEvent* e)
{
mMousePress = false;
}
int painter::length(int x1,int y1,int x2,int y2)
{
return (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);
}
void painter::renderLine(int p1,int p2)
{
QPair<int,int> pos1 = mPosList[p1];
QPair<int,int> pos2 = mPosList[p2];
float k = pos2.second - pos1.second;
k /= (float)(pos2.first - pos1.first);
std::cout<<k<<std::endl;
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing,true);
painter.setPen(QPen(Qt::blue,2,Qt::DotLine));
painter.drawLine(pos1.first,pos1.second,pos2.first,pos2.second);
}