2011-11-03 329 views
2
#include <iostream> 
#include <math.h> 
#include <cstdlib> 
using namespace std; 

void circle(int x, int y, int radius); 
void line(int a, int b, int c, int d); 
bool buffer[26][81]; 
char drawSpace[26][81]; 

int main() { 
    int a = 0; 
    int b = 0; 
    int c = 0; 
    int d = 0; 
    int x = 0; 
    int y = 0; 
    int radius = 0; 
    char choice; 

    cout << "Type 'c' to draw a circle or type 'l' to draw a line." << endl; 
    cin >> choice; 

    if (choice == 'c'){ 
     cout << "please enter an x coordinate for the center of the circle \n"; 
     cin >> x; 
     cout << "please enter a y coordinate for the center of the circle \n"; 
     cin >> y; 
     cout << "please enter a value for the radius of the circle \n"; 
     cin >> radius; 
     int moves = (x - radius)/10; 
     for (int s = 0; s < moves; s++){ 
      circle(x, y, radius); 
      system("clear"); 
      x = x -10; 
     } 
    } 

    else if (choice == 'l'){ 
     cout << "Please enter the x coordinate for the first point on the line \n"; 
     cin >> a; 
     cout << "Please enter the y coordinate for the first point on the line \n"; 
     cin >> b; 
     cout << "Please enter the x coordinate for the end point on the line \n"; 
     cin >> c; 
     cout << "Please enter the y coordinate for the end point on the line \n"; 
     cin >> d; 
    } 

    else 
     cout << "you did not enter an appropriate letter, please restart the program and try again."<< endl; 

    return 0; 
} 

void circle(int x, int y, int radius){ 
    if (x + radius >= 81|| x - radius <= 0 || y + radius >= 26 || y - radius <= 0){ 
     cout << "the coordinates provided for the circle will not fit on the screen" << endl; 
     return; 
    } 

    for (int i = 0; i < 26; i++) { 
     for(int j = 0; j < 81; j++) { 
      int a = abs (x - j); 
      int b = abs (y - i); 
      int distance = pow(a, 2) + pow(b, 2); 
      int realDistance = pow(radius, 2); 
      if (abs(realDistance - distance) <= 3){ 
       buffer[i][j] = true; 
      } 
     } 
    } 

    for (int m = 0; m < 26; m++){ 
     for(int n = 0; n < 81; n++){ 
      if (buffer[m][n]){ 
       drawSpace[m][n] = 42; 
      } 
      else 
       drawSpace[m][n] = 32; 
     } 
    } 

    for (int row = 25; row >= 0; row--) { 
     for (int col = 0; col < 81; col++) { 
      cout << drawSpace[row][col]; 
     } 
     cout << "\n"; 
    } 
} 

void line(int a, int b, int c, int d){ 
    if (a >= 81 || c >= 81 || a <= 0 || c <= 0 || b >= 26 || d >= 26 || b <= 0 || d <= 0){ 
     return; 
    } 
    int intercept = 0; 
    double rise = d - b; 
    double run = c - a; 
    double slope = rise/run; 
    intercept = b - (slope*a); 
    for (int i = 0; i < 26; i++) { 
     for(int j = 21; j < 81; j++) { 
      if (slope > 0){ 
       if (j > a && j < c){ 
        int newIntercept = i - (slope*j); 
        int test = abs (intercept - newIntercept); 
        if (test <= 0) 
         buffer[i][j] = true; 
       else 
        buffer[i][j] = false; 
       } 
      } 
      else if (slope < 0){ 
       if (j < a && j > c){ 
        int newIntercept = i - (slope*j); 
        int test = abs (newIntercept - intercept); 
        if (test <= 0) 
         buffer[i][j] = true; 

       } 
       else 
        break; 
      } 
     } 
    } 

    for (int m = 0; m < 26; m++){ 
     for(int n = 0; n < 81; n++){ 
      if (buffer[m][n]) 
       drawSpace[m][n] = 42; 
      else 
       drawSpace[m][n] = 32; 
     } 
    } 

    for (int row = 25; row >= 0; row--) { 
     for (int col = 0; col < 81; col++) { 
      cout << drawSpace[row][col]; 
     } 
     cout << "\n"; 
    } 
} 

我已經寫了編程任務這代碼,目標,其中是把用於圓形或線的座標和尺寸的輸入,並且將它們打印出來到終端,好像它是一個圖表。第二步是讓形狀從屏幕右側移動到左側。我已經開始爲圓圈編寫代碼,但由於某些原因,系統(「清除」)調用似乎不能清除屏幕,並且它只是打印多餘的圓圈而不會刪除較舊的圓圈。如果有人能幫助,我會非常感激。清除終端屏幕

+0

您是否在尋找'system(「cls」)''也許?無論如何,你真的不應該使用這樣的事情 - 儘管我認爲,因爲這是一項任務,所以這樣的事情是預料之中的。 –

回答

1

樓主沒有足夠的代表,所以我在這裏張貼這是爲他

我其實是有點不對頭。 system("clear")我實際上用的是 工作,我遇到的問題是我沒有重置我用來繪製需要繪製的點的數組。 感謝您的幫助,在我發現自己的問題之前,我學到了一些關於如何清除屏幕的信息。

2

在Linux(和其他Unix)上,您還可以使用ncurses library輸出到終端。

+0

這是正確的答案。每個人建議使用'system()'或寫入'cout'都是錯誤的。 – Swiss

+0

@Swiss如果它僅僅用於小遊戲或家庭作業,那麼它有什麼問題呢? –

+0

@Shredder'system()'即使是最簡單的項目,對於性能,安全性和可移植性問題都是一個可怕的想法。使用'cout'來清除終端緩衝區會更好一些,但是避免使用ncurses對於僅僅使用ncurses沒有實際的好處。 – Swiss