1、源代码如下:
void Kmeans::drawPoint()
{
QPicture pp;
painterP.begin(&pp);//开始记录
pp.setBoundingRect(ui.label_2->rect());
painterP.setRenderHint(QPainter::Antialiasing, true);
Pointp p1;
p1.no = p.at(0).no;
p1.x = p.at(0).x;
p1.y = p.at(0).y;
for (int i = 1; i < p.size(); i++)
{
if (p1.x > p.at(i).x)
{
p1.x = p.at(i).x;
}
if (p1.y > p.at(i).y)
{
p1.y = p.at(i).y;
}
}
double xmin = p1.x;
double ymin = p1.y;
for (int i = 1; i < p.size(); i++)
{
if (p1.x < p.at(i).x)
{
p1.x = p.at(i).x;
}
if (p1.y < p.at(i).y)
{
p1.y = p.at(i).y;
}
}
double xmax = p1.x;
double ymax = p1.y;
int w=ui.label_2->width();
int h=ui.label_2->height();
double a = w/(xmax -xmin);
double b1 = h/(ymax -ymin);
for (int i = 0; i < k.size(); i++)
{
int r = qrand() % 256;
int g = qrand() % 256;
int b = qrand() % 256;
QColor color = QColor(r, g, b);
for (int j = 0; j < S.size(); j++)
{
if (k.at(i).no == S.at(j).noK)
{
pen.setColor(color);
painterP.setPen(pen);
int radius = 5;
double x = S.at(j).x;
double y = S.at(j).y;
x = (x - xmin)*a;
y = (y - ymin)*b1;
painterP.drawEllipse(x - radius, y - radius, radius * 2, radius * 2);
}
}
}
painterP.end();//记录绘制过程结束
ui.label_2->setPicture(pp);
}