我需要使用数组和函数显示带'*'的水平和垂直线。 这是问题描述:
您将创建一个可以将线条文字绘制到的程序 二维数组字符。数组中的每个字符都会 表示要在控制台输出上显示的一个字符。这是 基于角色的图形系统。一条线将由一系列组成 “*”字符垂直或水平写在 显示。由于我们不能直接写入屏幕,我们会写 将图形输出到二维数组然后写入显示 通过cout到控制台。
这是我到目前为止的代码。我需要帮助的部分是放在功能中的内容。谢谢你的帮助!
#include <iostream>
using namespace std;
// global constants
const int SCREEN_WIDTH = 79;
const int SCREEN_HEIGHT = 74;
const char ON = '*';
const char OFF = ' ';
const char ERROR = '?';
const char NONE = '\0';
//function prototypes
void reset(char []);
void display(char []);
char getLine();
char getPixel();
char drawLine();
char drawPixel();
int main()
{
// variables use below for the graphics
int minX = 0;
int maxX = SCREEN_WIDTH - 1;
int midX = SCREEN_WIDTH / 2;
int minY = 0;
int maxY = SCREEN_HEIGHT - 1;
int midY = SCREEN_HEIGHT / 2;
const int COORD = 2;
int point1;
int newX, newY;
char ch;
char null = '\0';
const int STR_PTS = 16;
string string1 = "1{2}3[4]5@6#7%8^";
int points[STR_PTS][COORD] = { {minX + 2, minY + 2}, { minX + 3, minY + 2 }, { midX - 3, minY + 2 }, { midX - 2, minY + 2 },
{ minX + 2, midY - 2 }, { minX + 3, midY - 2 }, { midX - 3, midY - 2 }, { midX - 2, midY - 2 },
{ midX - 3, midY }, { minX + 4, maxY + 1 },
{ midX - 1, midY }, { maxX + 2, minY + 4 },
{ midX + 1, midY }, { minX + 1, minY - 1 },
{ midX + 3, midY }, { minX + 3, minY - 4 } };
// graphics "screen". Memory to be displayed on the console
char screen[SCREEN_WIDTH][SCREEN_HEIGHT];
// clear out the graphics screen
reset(screen);
// Test of part 1
// We are going to test drawPixel in the upper left corner of the output
for (point1 = 0; point1 < STR_PTS; point1++)
{
// draw a char at the x, y coord in points
drawPixel(screen, points[point1][0], points[point1][1], string1[point1]);
}
// Test of part 2
// Next we will test getPixel and drawLine
newX = midX;
newY = minY + 2;
// for all of the character position (points) we wrote to before we will
// try and read from those positions now. Only 4 of them are valid.
for (point1 = 0; point1 < STR_PTS; point1++)
{
// draw a char at the x, y coord in points
ch = getPixel(screen, points[point1][0], points[point1][1]);
if (ch == null)
{
ch = '-';
}
drawPixel(screen, newX, newY, ch);
newY++;
}
// Now test 4 valid lines. Left to right, right to left, top to bottom and bottom to top
drawLine(screen, minX + 10, midY / 3, midX - 10, midY / 3);
drawPixel(screen, minX + 9, midY / 3, 'A');
drawLine(screen, midX - 10, midY / 3 + 3, minX + 10, midY / 3 + 3);
drawPixel(screen, midX - 9, midY / 3 + 3, 'B');
drawLine(screen, maxX - 10, minY + 3, maxX - 10, midY - 3);
drawPixel(screen, maxX - 10, midY - 2, 'C');
drawLine(screen, maxX - 12, midY - 3, maxX - 12, minY + 3);
drawPixel(screen, maxX - 12, minY + 2, 'D');
// Now test a line that is not vertical or horizontal
drawLine(screen, minX, minY + 3, minX + 3, minY + 6);
drawPixel(screen, minX + 1, minY + 4, 'E');
// Test clipped lines - one that is clipped on the right, one is clipped on the left,
// one is clipped on the top, one is clipped on the bottom and one is entirely off of the screen
drawLine(screen, maxX - 5, midY / 2, maxX + 10, midY / 2);
drawPixel(screen, maxX - 6, midY / 2, 'F');
drawLine(screen, minX - 10, midY / 2, minX + 5, midY / 2);
drawPixel(screen, minX + 6, midY / 2, 'G');
drawLine(screen, maxX - 15, minY - 10, maxX - 15, minY + 10);
drawPixel(screen, maxX - 15, minY + 11, 'H');
drawLine(screen, maxX - 2, maxY - 10, maxX - 2, maxY + 10);
drawPixel(screen, maxX - 2, maxY - 11, 'I');
drawLine(screen, maxX + 2, midY, maxX + 20, midY);
drawPixel(screen, maxX - 2, maxY - 13, 'J');
dispaly(screen); // output to the console via cout
return 0;
}
void reset(char screen[SCREEN_WIDTH][SCREEN_HEIGHT])
{
for (int row = 0; row < SCREEN_WIDTH; row++)
{
for (int col = 0; col < SCREEN_HEIGHT; col++)
{
screen[row][col] = OFF;
}
}
}
void dispaly(char screen[SCREEN_WIDTH][SCREEN_HEIGHT])
{
for (int row = 0; row < SCREEN_WIDTH; row++)
{
for (int col = 0; col < SCREEN_HEIGHT; col++)
{
screen[row][col];
}
}
}
char getPixel(char screen[SCREEN_WIDTH][SCREEN_HEIGHT], int x, int y)
{
for (int index = x; index < 5; index++)
{
screen[index][y] = ON;
}
}
char drawPixel(char screen[SCREEN_WIDTH][SCREEN_HEIGHT], int x, int y, char charValue)
{
getPixel;
if (x >= 0 && x < 79 && y >= 0 && y < 74)
{
cout << ON;
}
else
{
cout << NONE;
}
return charValue;
}
char getLine()
{
}
char drawLine(char screen[SCREEN_WIDTH][SCREEN_HEIGHT], int startX, int startY, int endX, int endY)
{
drawPixel;
if (startX == endX)
{
startY <= endY;
startY >= endY;
}
else if (startY == endY)
{
startX <= endX;
startX >= endX;
}
else if (startX > endX)
{
temp = startX;
startX = endX;
endX = temp;
}
}