C++ 画星号图形——圆形(核心代码记录)

void Circle::print_char(int x1,int x2)
{
    int i;
    // 计算这一行的宽度,终端最多显示80列
    int n = max(x1, x2) + 1;
    n = n > 80 ? 80 : n;
    for (i = 0; i < n; i++)
    {        
        if (i == x1 || i == x2)
            printf("*");
        else
             printf(" ");
    }
     printf("\n");
}
void Circle::Draw()
{
    int r=(int)semidiameter;
    int x1, x2, y;
   int d = 2 * r;
    int offset;
    for(y = 0; y <= d; y++)
     {
         /*终端字符宽高比为2:1*/
        offset = (int)(0.5 + sqrt((double)(r*r - (y-r)*(y-r)))*2.0);
        x1 = d - offset;
        x2 = d + offset;
        print_char(x1, x2);
    }
}

 

posted @ 2015-06-06 23:31  Daniel_Yi  阅读(3082)  评论(0编辑  收藏  举报