2011-11-02 39 views
0
//============================================================================ 
// Name  : Assignment.cpp 
// Author  : Tim Bialecki 
// Version  : 
//============================================================================ 

#include <iostream> 
#include <math.h> 
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 = 75; 
    int b = 5; 
    int c = 4; 
    int d = 26; 
    /*cout << "please enter an x coordinate for the center of the circle"; 
    cin >> x; 
    cout << "please enter a y coordinate for the center of the circle"; 
    cin >> y; 
    cout << "please enter a value for the radius of the circle"; 
    cin >> radius;*/ 

    circle(a, b, c); 
    for (int col = 80; col >= 0; col--) { 

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

void circle(int x, int y, int radius){ 
    /*if (x + radius >= 81 || y + radius >= 26 || y - radius <= 26){ 
     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; 
     } 
    } 
} 

void line(int a, int b, int c, int d){ 
    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 = 0; j < 81; j++) { 
      int newIntercept = i - (slope*j); 
      int test = abs (intercept - newIntercept); 
      if (test <= 0) 
       buffer[i][j] = true; 
      else 
       buffer[i][j] = false; 
     } 

    } 

    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; 
     } 
    } 
} 

此代码是一项正在进行的工作,但我试图写一个程序,它输入的坐标和既有线的尺寸和一个圆,并打印出来在终端窗口中,好像它是一个81x26图。我刚刚提供了样本输入来测试这一点,但由于某些原因,形状不会以适当的方向打印到应该是x和y轴的位置。我尝试了一系列不同的方法来解决这个问题,并没有运气。希望有人能帮助。由于为什么我的程序离轴

+0

看到为C++提供的这种赋值很奇怪。这种语言比这些,IMO更值得。对我来说, – Mahesh

回答

2

看起来OK对我说:

   ***  
      ** ** 
      *  * 
      *  * 
      *  * 
      *  * 
      *  * 
      ** ** 
       ***  

这不完全是圆的,因为角色比他们宽高。

编辑:这只是我输出的前几行。根据评论和第二次看代码,我认为行和列混在一起。

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

在每个“列”后都有一个换行符。交换两个for行可能会产生你想要的。

+0

y轴被打印为x轴,图的顶部是y轴 – classISover

+0

你意识到终端的左上角为0,0,在这个系统中,行向下,列向右 –

相关问题