基于MFC的计算机图形学之基本图形生成_画圆(2)

1.Bresenham画圆

int r,d,x,y,x0,y0;
 DCPoint->SetROP2(R2_COPYPEN);//绘图方法为直接画
 r=(int)sqrt(((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y))*1.0);
 x=0;y=r;d=3-2*r;x0=p1.x;y0=p1.y;
 while(x<y||x==y)
 {
  DCPoint->SetPixel(x+x0,y+y0,m_crColor);
  DCPoint->SetPixel(-x+x0,y+y0,m_crColor);
  DCPoint->SetPixel(x+x0,-y+y0,m_crColor);
  DCPoint->SetPixel(-x+x0,-y+y0,m_crColor);
  DCPoint->SetPixel(y+x0,x+y0,m_crColor);
  DCPoint->SetPixel(-y+x0,x+y0,m_crColor);
  DCPoint->SetPixel(y+x0,-x+y0,m_crColor);
  DCPoint->SetPixel(-y+x0,-x+y0,m_crColor);
  x=x+1;
  if(d<0||d==0)
  {
   d=d+4*x+6;
  }
  else
  {
   y=y-1;d=d+4*(x-y)+10;
  }
 };

2.正负法圆

int r,d,x,y,x0,y0;
 DCPoint->SetROP2(R2_COPYPEN);//绘图方法为直接画
 r=(int)sqrt(((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y))*1.0);
 d=0;x0=p1.x;y0=p1.y;x=x0;y=y0+r;
 while(y>y0)
 {
  DCPoint->SetPixel(x,y,m_crColor);
  DCPoint->SetPixel(-x+2*x0,y,m_crColor);
  DCPoint->SetPixel(x,-y+2*y0,m_crColor);
  DCPoint->SetPixel(-x+2*x0,-y+2*y0,m_crColor);
  if(d<0)
  {
   x++;d=d+2*(x-x0)+1;
  }
  else
  {
   y--;d=d-2*(y-y0)+1;
  }
 };

你可能感兴趣的:(基于MFC的计算机图形学之基本图形生成_画圆(2))