用C语言编写程序 在屏幕上画一个矩形

2024-11-16 08:44:05
推荐回答(1个)
回答1:

#include
#include
#include
#include
#include

void polygon(int n, int x, int y, int r, int color, float arg, int fillstyle);

int main()
{
int GraphDriver;
int GraphMode;
float arg = 45, argd;
int a;
int direction;
int r;
int n = 4;
FILE *fp;
char szfilename[255] = {"c:\\cube.txt"};
GraphDriver = DETECT;
printf("Input size of cube: ");
scanf("%d", &r);
printf("Input direction(0-1): ");
scanf("%d", &direction);
if (direction == 0)
{
argd = 45;
}
else
{
argd = -45;
}

initgraph(&GraphDriver, &GraphMode, "");
polygon(n, 300, 200, r, 12, arg, 0);
while(1)
{
while(kbhit())
{
a = getch();
if (a == 27)
{
if ((fp = fopen(szfilename, "wt")) != NULL)
{
fprintf(fp, "%d\n%d\n", r, direction);
fclose(fp);
}
closegraph();
return 0;
}
if (a == 0)
{
getch();
polygon(n, 300, 200, r, 0, arg, 0);
arg += argd;
polygon(n, 300, 200, r, 12, arg, 0);
}
else
{
polygon(n, 300, 200, r, 0, arg, 0);
arg += argd;
polygon(n, 300, 200, r, 12, arg, 0);
}
}
}
}

void polygon(int n, int x, int y, int r, int color, float arg, int fillstyle)
{
double pi;
int i;
float x1[10], y1[10];
setcolor(color);
pi = atan(1) * 4;
arg = atan(1) / 45 * arg;
x1[1] = x + r * cos(2 * pi / n + arg);
y1[1] = y + r * sin(2 * pi / n + arg);
moveto(x1[1], y1[1]);
for (i = 2; i <= n; i++)
{
x1[i] = x + r * cos(2 * pi * i / n + arg);
y1[i] = y + r * sin(2 * pi * i / n + arg);
lineto(x1[i], y1[i]);
}
lineto(x1[1], y1[1]);
if (fillstyle != 0)
{
setfillstyle(SOLID_FILL, color);
floodfill(x, y, color);
}
}