2014-07-12 53 views
0

我有一個方形的控制檯旋轉,但我有一些洞。我如何正確填寫它?如何修復這個控制檯旋轉動畫的孔

enter image description here

#include <stdio.h> 
#include <Windows.h> 
#include <math.h> 

void moveTo (int x, int y) 
{ 
    COORD coord = { x, y }; 
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); 
} 

double round (double number) 
{ 
    return number < 0.0 ? ceil(number - 0.5) : floor(number + 0.5); 
} 

double deg2rad (double a) 
{ 
    double pi = 3.14159265358979323846; 
    return a * pi/180.0; 
} 

int main() 
{ 
    int w = 8; 
    int h = 8; 
    int cx = 20; 
    int cy = 10; 

    double a = 0; 

    while (1) 
    { 
     system("cls"); 

     for (int y = 0; y < h; y++) 
     { 
      for (int x = 0; x < w; x++) 
      { 
       double xx = x - 4; 
       double yy = y - 4; 
       double fx = xx * cos(a) - yy * sin(a); 
       double fy = xx * sin(a) + yy * cos(a); 

       int ix = cx + round(fx); 
       int iy = cy + round(fy); 

       moveTo(ix, iy); 
       printf("X"); 
      } 
     } 

     a += deg2rad(15.0); 
     Sleep(100); 
    } 
    return 0; 
} 
+0

請告訴我爲什麼倒票。 – Fabricio

+1

'system(「cls」)'是不好的做法,請考慮使用http://msdn.microsoft.com/en-us/library/windows/desktop/ms682022%28v=vs.85%29.aspx – Havenard

回答

1

不是一個實際的答案

Aparently您的代碼將始終打印相同數量的X在屏幕上,儘管這可能不是總是這樣。

我想你不應該重新計算每個預定義X的位置,而是計算,廣場周圍的線條的幾何與填充率與X S之間的空間,儘可能多的必要,直到它擊中了對面的邊界或東西。

另一種解決方案可能是加倍你方的「密度」:

int w = 8*2; 
int h = 8*2; 
int cx = 20*2; 
int cy = 10*2; 
... 
moveTo(ix/2, iy/2); 

這應該加倍打印一些小點,但也應該填補空白。