我要画10个圆点,如果没有对随机生成的点进行排序的话,画出来的点数是正常的,如果排序后画出来的点数会减少,有时画6个有时画7个有时画8个,我调试了但找不出问题,请各路大神帮忙看看,谢谢!
//随机生成点集并按横坐标排序
void CClosestPointDlg::CreateLogicalPoints(int num)
{
m_picDraw.GetClientRect(&rectPicture);
while (m_lpoints.GetCount() < num)
{
CPoint p(rand() % rectPicture.Width() ,rand() % rectPicture.Height());
if (!m_lpoints.Find(p))
{
m_lpoints.AddTail(p);
}
}
CPoint p1(0,0),p2(0,0),temp_p(0,0);
//POSITION cur_pos,late_pos;
for (int i = 0; i < m_lpoints.GetCount() - 1; i++)
{
POSITION cur_pos = m_lpoints.FindIndex(i);
p1 = m_lpoints.GetAt(cur_pos);
for (int j = i + 1; j < m_lpoints.GetCount(); j++)
{
POSITION late_pos = m_lpoints.FindIndex(j);
p2 = m_lpoints.GetAt(late_pos);
if (p1.x > p2.x)
{
temp_p = p1;
p1 = p2;
p2 = temp_p;
m_lpoints.SetAt(cur_pos,p2);
m_lpoints.SetAt(late_pos,p1);
}
}
}
}
//画点函数
void CClosestPointDlg::DrawPoints(CList *points, COLORREF color)
{
Radius = 5;
CClientDC clientDC(this);
newBrush.CreateSolidBrush(color);
// 选择新画刷,并保存旧画刷的指针到pOldBrush
pOldBrush = clientDC.SelectObject(&newBrush);
for (int i=0;i < points->GetCount(); i++)
{
POSITION pos = points->FindIndex(i);
CPoint p = points->GetAt(pos);
clientDC.Ellipse(p.x - Radius,p.y - Radius,p.x + Radius,p.y + Radius);
}
clientDC.SelectObject(pOldBrush);
// 删除新画刷
newBrush.DeleteObject();