使用4个整数输入绘制带循环的三角形

时间:2014-04-21 12:50:56

标签: c++ loops geometry shapes

我有一个打印25 x 25网格的循环,并且应该使用来自用户输入的2个x坐标和2个y坐标来绘制三角形,其中x1&lt; x2和y1 < Y2。它还使用用户输入的两个字符来填充内部并勾勒出形状的外观。使用4坐标的原因是因为赋值的其他部分应该使用这些输入来绘制矩形。我的矩形工作得很完美。

我使用相同的循环来打印两个形状的网格,但我无法弄清楚如何调整if语句来确定两个字符的位置以形成三角形。

这是三角形的循环。中间的if语句绘制一个矩形。有关调整的任何想法,使其打印成三角形吗?

void Triangle::printTriangle()
{
int numrows = 25;
int numcols = 25;
int current_row = numrows; // starting row number
int current_col = 1; // starting col number
char output = '.';

for(i = 0; i < numrows; i++)        // prints 25 rows of 25 dots 
{   

    cout << current_row << '\t';    // Print out current row number, followed by a tab

    //This is our loop for each ROW
    //Print out dots in each row OR stuff for the triangle
    for(j = 1; j <= numcols; j++)
    {           
        output = '.'; // Initialize output with our default value of "."


        if ((current_col >= x1) && (current_col <= x2) && (current_row >= y1) && (current_row <= y2))
        {
            output = outer;
        }

        if ((current_col > x1) && (current_col < x2) && (current_row > y1) && (current_row < y2))
        {
            output = inner;
        }

        cout << output << "  "; // output our "output" value and a space            
        current_col++;  //Increment current column, because this is the end of this column loop iteration

    } // Close column loop

    cout << endl;       //...and a new line

    current_col = 1;    // reset column count for next iteration
    current_row--;      // decrement current row number

} // Close Row loop


//output column labels across bottom line
cout << '\t';

// put 1 -> 25 across the bottom
for (i = 1; i <= 25; i++)
{
    if(i < 10)
    {
        cout << i << "  ";
    }

    if(i > 9)
    {
        cout << i << " ";
    }
}

// Spit out a couple of blank lines
cout << endl << endl;

}

0 个答案:

没有答案