2011-11-03 150 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