用C语言编写程序 在屏幕上画一个矩形 10
要求:每按一次键盘,该矩形转动45度。矩形的大小和转动的方向有键盘输入,并将输入的数据送文件保存,按回车键程序结束。(功能延伸:改变数据文件的内容后,再根据数据文件里的数...
要求:每按一次键盘,该矩形转动45度。矩形的大小和转动的方向有键盘输入,并将输入的数据送文件保存,按回车键程序结束。(功能延伸:改变数据文件的内容后,再根据数据文件里的数据绘制矩形)
展开
1个回答
推荐于2016-10-11
展开全部
#include <math.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <graphics.h>
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);
}
}
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <graphics.h>
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);
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询