#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 :(得分:2)
答案 1 :(得分:2)
在Linux(和其他Unix)上,你也可以使用ncurses library输出到终端。
答案 2 :(得分:1)
原始海报还没有足够的代表,所以我在这里发帖给他:
我实际上有点偏离基础。我实际使用的
system("clear")
确实如此 工作,我遇到的问题是我没有重置bool
我用来绘制需要绘制的点的数组。 感谢您的帮助,我在找到自己的问题之前了解了一些关于如何清除屏幕的事情。